bzoj 3451: Tyvj1953 Normal [fft 点分治 期望]
3451: Tyvj1953 Normal
题意:
N 个点的树,点分治时等概率地随机选点,代价为当前连通块的顶点数量,求代价的期望值
百年难遇的点分治一遍AC!!!
今天又去翻了一下《具体数学》上的离散概率,对期望有了一点新认识吧。
本题根据期望的线性性质,计算每个点的代价期望加起来。
一个点v产生了代价,它在u选为中心时所在的cc里,并且(u,v)路径上没有其他点已经被选择。概率为\(\frac{1}{(u,v)之间包含u,v点的个数}\)
统计每种长度的路径有多少个
点分治+生成函数统计就好了
值得注意的是,要用总-同一个子树的做法,否则fft的次数界会与最大子树深度相关,可以被卡成n^2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <ctime>
using namespace std;
typedef long long ll;
const int N = (1<<16) + 5, M = 1e5+5;
const double PI = acos(-1.0);
inline int read() {
char c=getchar(); int x=0,f=1;
while(c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
return x*f;
}
struct meow{
double x, y;
meow(double a=0, double b=0):x(a), y(b){}
};
meow operator +(meow a, meow b) {return meow(a.x+b.x, a.y+b.y);}
meow operator -(meow a, meow b) {return meow(a.x-b.x, a.y-b.y);}
meow operator *(meow a, meow b) {return meow(a.x*b.x-a.y*b.y, a.x*b.y+a.y*b.x);}
meow conj(meow a) {return meow(a.x, -a.y);}
typedef meow cd;
namespace fft {
int maxlen = 1<<16, rev[N];
cd omega[N], omegaInv[N];
void init(int lim) {
maxlen = 1; while(maxlen < lim) maxlen<<=1;
for(int i=0; i<maxlen; i++) {
omega[i] = cd(cos(2*PI/maxlen*i), sin(2*PI/maxlen*i));
omegaInv[i] = conj(omega[i]);
}
}
void dft(cd *a, int n, int flag) {
cd *w = flag == 1 ? omega : omegaInv;
int k = 0; while((1<<k) < n) k++;
for(int i=0; i<n; i++) {
rev[i] = (rev[i>>1]>>1) | ((i&1)<<(k-1));
if(i < rev[i]) swap(a[i], a[rev[i]]);
}
for(int l=2; l<=n; l<<=1) {
int m = l>>1;
for(cd *p = a; p != a+n; p += l)
for(int k=0; k<m; k++) {
cd t = w[maxlen/l*k] * p[k+m];
p[k+m] = p[k] - t;
p[k] = p[k] + t;
}
}
if(flag == -1) for(int i=0; i<n; i++) a[i].x /= n;
}
void sqr(cd *a, int n) {
dft(a, n, 1);
for(int i=0; i<n; i++) a[i] = a[i] * a[i];
dft(a, n, -1);
}
}
int n;
struct edge{int v, ne;} e[N<<1];
int cnt, h[N];
inline void ins(int u, int v) {
e[++cnt] = (edge){v, h[u]}; h[u] = cnt;
e[++cnt] = (edge){u, h[v]}; h[v] = cnt;
}
int rt, f[N], vis[N], size[N], all;
void dfs_rt(int u, int fa) {
size[u] = 1; f[u] = 0;
for(int i=h[u];i;i=e[i].ne) {
int v = e[i].v;
if(vis[v] || v == fa) continue;
dfs_rt(v, u);
size[u] += size[v];
f[u] = max(f[u], size[v]);
}
f[u] = max(f[u], all-size[u]);
if(f[u] < f[rt]) rt = u;
}
int d[N], c[N], lim;
void dfs_get(int u, int fa) {
for(int i=h[u];i;i=e[i].ne) {
int v = e[i].v;
if(vis[v] || v == fa) continue;
d[v] = d[u] + 1; c[d[v]]++;
lim = max(lim, d[v]+1);
dfs_get(v, u);
}
}
cd a[N];
int g[N], t[N];
void cal(int lim, int flag) {
int n = 1; while(n<lim<<1) n<<=1; //printf("cal------- %d %d %d\n", lim, n, flag);
//for(int i=0; i<lim; i++) printf("%d ", c[i]); puts(" c");
for(int i=0; i<lim; i++) a[i] = cd(c[i]), c[i] = 0;
for(int i=lim; i<n; i++) a[i] = cd();
fft::sqr(a, n);
//for(int i=0; i<lim<<1; i++) printf("%.0lf ", a[i].x); puts(" a");
for(int i=1; i<lim<<1; i++) t[i] += flag * (int) floor(a[i-1].x + 0.5);
//for(int i=0; i<lim<<1; i++) printf("%d ", t[i]); puts(" t");
}
void dfs(int u) { //printf("\n-----------dfs---------- %d\n", u);
vis[u] = 1;
d[u]=0; c[0]++; lim=1; dfs_get(u, 0);
for(int i=0; i<lim<<1; i++) t[i] = 0;
cal(lim, 1);
int _lim = lim;
for(int i=h[u];i;i=e[i].ne) {
int v = e[i].v;
if(vis[v]) continue;
d[v] = 1; c[1]++; lim = 2; dfs_get(v, u);
cal(lim, -1);
}
for(int i=0; i<_lim<<1; i++) g[i] += t[i];// printf("final %d %d\n", i, t[i]);
for(int i=h[u];i;i=e[i].ne) {
int v = e[i].v;
if(vis[v]) continue;
all = size[v]; rt=0; dfs_rt(v, u);
dfs(rt);
}
}
int main() {
freopen("in", "r", stdin);
n=read();
fft::init(n<<1);
for(int i=1; i<n; i++) ins(read()+1, read()+1);
all=n; rt=0; f[0]=1e9; dfs_rt(1, 0);
dfs(rt);
double ans = 0;
//for(int i=1; i<=n; i++) printf("%d ", g[i]); puts("");
for(int i=1; i<=n; i++) if(g[i]) ans += (double) g[i] / i;
printf("%.4lf", ans);
}
Copyright:http://www.cnblogs.com/candy99/