I have to say it's a divine question.
%%% http://blog.csdn.net/samjia2000/article/details/51762811
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5#include <Set>6 #defineINF 0x3f3f3f3f7 #defineLL Long Long8 #defineEPS 1e-89 #defineN 100005Ten using namespacestd; OneInlineintRA () A { - intx=0, f=1;CharCh=GetChar (); - while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();} the while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} - returnx*F; - } - + Const intMod=1e+9+9; - Const intPrime=12589; + A LL Tim[n],q[n]; at intK; - structnode{intto,x; LL v;} U[n]; - BOOLCMP (Node A, Node B) {returna.v<B.V;} - - structTree - { in intCnt,head[n]; - structedge{intTo,next;} e[n<<1]; to intN,r[n],fa[n],size[n]; + LL V[n],rt[n],f[n]; - LL Suf[n],pre[n]; the * voidInsertintXintY) {e[++cnt].next=head[x]; e[cnt].to=y; head[x]=CNT;} $ voidInit ()Panax Notoginseng { -Cnt=0; the for(intI=1; i<=n; i++) head[i]=r[i]=f[i]=fa[i]=rt[i]=0; + for(intI=1; i<n; i++) A { the intX=ra (), y=RA (); + Insert (x, y); insert (y,x); -r[x]++; r[y]++; $ } $ } - - voidDFS1 (intx) the { -size[x]=1;Wuyi for(intI=head[x];i;i=e[i].next) the { - if(E[i].to==fa[x])Continue; Wufa[e[i].to]=x; - DFS1 (e[i].to); Aboutsize[x]+=size[e[i].to]; $ } -k=0; - for(intI=head[x];i;i=e[i].next) - if(fa[e[i].to]==x) q[++k]=v[e[i].to]; ASort (q+1, q+k+1); +v[x]=0; the for(intI=1; i<=k; i++) v[x]= (V[x]*prime%mod+q[i])%MoD; -V[x]= (V[x]*prime%mod+size[x])%MoD; $ } the the voidDFS2 (intx) the { thek=0; - if(x>1) u[++k].v=f[x],u[k].to=Fa[x]; in for(intI=head[x];i;i=e[i].next) the if(fa[e[i].to]==x) the { Aboutu[++k].to=e[i].to; theu[k].v=v[e[i].to]; the } theSort (u+1, u+1+k,cmp); + for(intI=1; i<=k; i++) pre[i]= (pre[i-1]*PRIME%MOD+U[I].V)%MoD; -suf[k+1]=0; the for(intI=k; i>=1; i--) suf[i]= (suf[i+1]+U[I].V*TIM[K-I]%MOD)%MoD;Bayi for(intI=1; i<=k; i++) the if(u[i].to!=Fa[x]) the { -F[u[i].to]= (pre[i-1]*tim[k-i]%mod+suf[i+1])%MoD; -F[u[i].to]= (f[u[i].to]*prime%mod+n-size[u[i].to])%MoD; the } the for(intI=head[x];i;i=e[i].next) the if(fa[e[i].to]==x) DFS2 (e[i].to); the } - voidcal () the { theDFS1 (1); DFS2 (1); the for(intx=1; x<=n; X + +)94 { thek=0; the for(intI=head[x];i;i=e[i].next) the if(fa[e[i].to]==x) q[++k]=v[e[i].to];98 if(Fa[x]) q[++k]=F[x]; AboutSort (q+1, q+k+1); -rt[x]=0;101 for(intI=1; i<=k; i++) rt[x]= (Rt[x]*prime%mod+q[i])%MoD;102rt[x]= (rt[x]*prime%mod+n)%MoD;103 }104 } the }a,b;106 107 intm;108 Set<LL>s;109 the intMainintargcChar Const*argv[])111 { thetim[0]=1; S.clear ();113m=RA (); the for(intI=1; i<=m+2; i++) tim[i]=tim[i-1]*prime%MoD; theA.n=m; A.init (); b.n=m+1; B.init (); a.cal (); b.cal (); the for(intI=1; i<=m; i++) S.insert (A.rt[i]);117 for(intI=1; i<=m+1; i++)118 if(b.r[i]==1&& (i!=1&& S.find (B.f[i])!=s.end ()) | | (i==1&& S.find (b.v[b.e[b.head[i]].to])! =s.end () )))119 { -printf"%d\n", i);return 0;121 }122 return 0;123}
Bzoj 4754: [Jsoi2016] unique leaves