【BZOJ3451正常な(点線ルール+ FFT)

【BZOJ3451正常な(点線ルール+ FFT)

フェイス質問

Nあなたのツリーのポイントを与え、この木はランダム点分割統治、毎回中央パーティションとしてランダム点です。かかるシーク時間希望、各パーティションとサブツリーのサイズを消費する時間として定義されます。

分析

所望の線形性は、答えは^ $ \ sum_ {i = 1}であり、N Iにおける(Iの所望のサブツリーの大きさ)= \ sum_ {i = 1} ^ n個の\ sum_ {J = 1} ^ N [J、晴子は$]ツリー内に点在しました

サブツリーの分割点Iにおいて考慮J条件は、明らかにI Jの経路上の全ての点に、私はパーティションの中心として選択されるべき最初のものです。ポイントがない場合にはそうでなければ、私はその後、i、jは二つのサブツリーに割り当てられている、選択します。選択された最初のものは、1}が{{DIST(I、J)} + +1 $($のDIST(i、j)の確率の$ \のFRACである\(iは距離jを表す)。次に式は書くことができます。 \) \ sum_。1} ^ {N-I = \ sum_ 1} = {J ^ N- \ FRAC {} {DIST。1(I、J)} $ + +1

クロック、および$ CNT [D] $が表す$のDIST(i、j)は=設け D $ $(I、J)\(番号は、その後、答えは\である) \ sum_ {D} = 0 ^ \ {CNTのFRACを[D]}、{D + 1 } $。$ CNT [k]は$を求めて検討する方法

我々は、パーティション間指し、深さはノードIのCD [I]の数であるDFS。この後答えを見つけ、それがルートノード$ CNT [I] = \ sum_ {J = 0} ^ I CD [J] CD [IJ] $である。これは容易CD直接コンボリューションの形で見られる、とされていますFFT畳み込みは、自分自身を見つけることができます。

最後に、原則として同じと除外について点在していることに注意してください。

第二主定理によれば、答えは($ Oである。再帰時間複雑さの$ T(N)= 2T(\ FRAC {2})+ \ FRAC {1} {2} N \ログN $を満たしますn個の\ログ^ 2のn)$

コード

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define maxn 200000
using namespace std;
typedef long double db;
typedef long long ll;
const db pi=acos(-1.0);
struct com{//复数类
    double real;
    double imag;
    com(){

    } 
    com(double _real,double _imag){
        real=_real;
        imag=_imag;
    }
    com(double x){
        real=x;
        imag=0;
    }
    void operator = (const com x){
        this->real=x.real;
        this->imag=x.imag;
    }
    void operator = (const double x){
        this->real=x;
        this->imag=0;
    }
    friend com operator + (com p,com q){
        return com(p.real+q.real,p.imag+q.imag);
    }
    friend com operator + (com p,double q){
        return com(p.real+q,p.imag);
    }
    void operator += (com q){
        *this=*this+q;
    }
    void operator += (double q){
        *this=*this+q;
    }
    friend com operator - (com p,com q){
        return com(p.real-q.real,p.imag-q.imag);
    }
    friend com operator - (com p,double q){
        return com(p.real-q,p.imag);
    }
    void operator -= (com q){
        *this=*this-q;
    }
    void operator -= (double q){
        *this=*this-q;
    }
    friend com operator * (com p,com q){
        return com(p.real*q.real-p.imag*q.imag,p.real*q.imag+p.imag*q.real);
    }
    friend com operator * (com p,double q){
        return com(p.real*q,p.imag*q);
    } 
    void operator *= (com q){
        *this=(*this)*q;
    }
    void operator *= (double q){
        *this=(*this)*q;
    }
    friend com operator / (com p,double q){
        return com(p.real/q,p.imag/q);
    } 
    void operator /= (double q){
        *this=(*this)/q;
    } 
    void print(){
        printf("%lf + %lf i ",real,imag);
    }
};
void fft(com *x,int n,int type){
	static int rev[maxn+5];
	int dn=1,k=0;
	while(dn<n){
		dn*=2;
		k++;
	}
	for(int i=0;i<n;i++) rev[i]=(rev[i>>1]>>1)|((i&1)<<(k-1));
	for(int i=0;i<n;i++) if(i<rev[i]) swap(x[i],x[rev[i]]);
	for(int len=1;len<n;len*=2){
		int sz=len*2;
		com wn1=com(cos(2*pi/sz),sin(2*pi/sz)*type);
		for(int l=0;l<n;l+=sz){
			int r=l+len-1;
			com wnk=1;
			for(int i=l;i<=r;i++){
				com tmp=x[i+len];
				x[i+len]=x[i]-wnk*tmp;
				x[i]=x[i]+wnk*tmp;
				wnk*=wn1;
			}
		}
	}
	if(type==-1) for(int i=0;i<n;i++) x[i]/=n;
}
void mul(com *a,com *b,com *ans,int n){//封装多项式乘法
	fft(a,n,1);
	if(a!=b) fft(b,n,1);
	for(int i=0;i<n;i++) ans[i]=a[i]*b[i];
	fft(ans,n,-1);
}


struct edge{
	int from;
	int to;
	int next;
}E[maxn*2+5];
int head[maxn+5];
int esz=1;
void add_edge(int u,int v){
	esz++;
	E[esz].from=u;
	E[esz].to=v;
	E[esz].next=head[u];
	head[u]=esz;
}

bool vis[maxn+5];
int sz[maxn+5],f[maxn+5];
int root;
int tot_sz;
void get_root(int x,int fa){
	sz[x]=1;
	f[x]=0;
	for(int i=head[x];i;i=E[i].next){
		int y=E[i].to;
		if(y!=fa&&!vis[y]){
			get_root(y,x);
			sz[x]+=sz[y];
			f[x]=max(f[x],sz[y]);
		}
	}
	f[x]=max(f[x],tot_sz-sz[x]);
	if(f[x]<f[root]) root=x;
}

int maxd;
com ff[maxn+5];//当前子树中深度为x的节点个数
com res[maxn+5];
ll cnt[maxn+5];

void get_deep(int x,int fa,int d){
	ff[d]+=1;
	maxd=max(maxd,d);
	for(int i=head[x];i;i=E[i].next){
		int y=E[i].to;
		if(y!=fa&&!vis[y]){
			get_deep(y,x,d+1);
		}
	}
}

void calc(int x,int d,int type){
	maxd=0;
	get_deep(x,0,d);
	int dn=1,k=0;
	while(dn<=maxd*2){
		dn*=2;
		k++;
	}
	mul(ff,ff,res,dn);//卷积
	for(int i=0;i<=maxd*2;i++) cnt[i]+=(ll)(res[i].real+0.5)*type;//用卷积结果更新cnt
	for(int i=0;i<=dn;i++) ff[i]=0;
}

void solve(int x){
	vis[x]=1;
	calc(x,0,1);
	for(int i=head[x];i;i=E[i].next){
		int y=E[i].to;
		if(!vis[y]){
			calc(y,1,-1);//容斥,减去一条边经过两次的答案
			root=0;
			tot_sz=sz[y];
			get_root(y,0);
			solve(root);
		}
	}
}

int n;
int main(){
	int u,v;
	scanf("%d",&n);
	for(int i=1;i<n;i++){
		scanf("%d %d",&u,&v);
		u++;
		v++;
		add_edge(u,v);
		add_edge(v,u);
	}
	f[0]=n+1;
	root=0;
	tot_sz=n;
	get_root(1,0);
	solve(root);
	db ans=0;
	for(int i=0;i<=n-1;i++){
		ans+=(db)cnt[i]*1/(i+1);
	}
	printf("%.4Lf\n",ans);
}

おすすめ

転載: www.cnblogs.com/birchtree/p/11665804.html