A very good tree on the team board problem, unfortunately there is no copyright can be submitted.
Here is the code for your reference, the block operation is similar to the Royal Alliance, converted into a sequence and the same as the ordinary team.
By: Big Brother
1#include <bits/stdc++.h>2 using namespacestd;3 Const intn=1e5+5;4 intd[n],fa[n][ -],bel[n],size[n],dfn[n],col[n],ans[n],p[n],head[n];5 intN,m,sum,idx,cnt,b,num,top,s[n];6 BOOLV[n];7 structnode{8 intTo,nex;9}e[n<<1];Ten voidAddintXinty) One { Ae[++cnt].nex=head[x];e[cnt].to=y;head[x]=CNT; - } - structquer{ the intX,y,a,b,id; - BOOL operator< (ConstQuer &b)Const{ - returnbel[x]<bel[b.x]| | (bel[x]==bel[b.x]&&dfn[y]<dfn[b.y]); - } + }q[n]; - voidDfsintx) + { Adfn[x]=++idx;s[++top]=x; at for(intI=1; i<= -;++i) -fa[x][i]=fa[fa[x][i-1]][i-1]; - for(intI=head[x];i;i=E[i].nex) - { - inty=e[i].to; - if(y==fa[x][0])Continue; infa[y][0]=x;d[y]=d[x]+1; - dfs (y); tosize[x]+=Size[y]; + if(size[x]>=B) - { thenum++; * for(intk=1; k<=size[x];++k) $bel[s[top--]]=num;Panax Notoginsengsize[x]=0; - } the } +size[x]++; A } the intLcaintXinty) + { - if(d[x]<D[y]) swap (x, y); $ inttmp=d[x]-D[y]; $ for(intI=0; i<= -;++i) - if(tmp& (1<<i)) x=Fa[x][i]; - for(intI= -; i>=0;--i) the { - if(fa[x][i]!=Fa[y][i])Wuyi { thex=Fa[x][i]; -y=Fa[y][i]; Wu } - } About returnx==y?x:fa[x][0]; $ } - voidReverseintx) - { - if(!v[x]) {v[x]=1;p [col[x]]++;if(p[col[x]]==1) sum++;} A Else{v[x]=0;p [col[x]]--;if(p[col[x]]==0) sum--;} + return; the } - voidSolveintXinty) $ { the while(x!=y) the { the if(d[x]<D[y]) swap (x, y); thereverse (x); x=fa[x][0]; - } in return; the } the intMain () About { thescanf"%d%d",&n,&m); theB=SQRT (n);intX,y,rt; the for(intI=1; i<=n;++i) scanf ("%d",&col[i]); + for(intI=1; i<=n;++i) - { thescanf"%d%d",&x,&y);Bayi if(!x) rt=y;Else if(!y) rt=x; the ElseAdd (x, y), add (y,x); the } -DFS (RT); num++; - while(top) bel[s[top--]]=num; the for(intI=1; i<=m;++i) the { thescanf"%d%d",&q[i].x,&q[i].y); the if(dfn[q[i].x]>dfn[q[i].y]) swap (Q[I].X,Q[I].Y); -Q[i].id=i; thescanf"%d%d",&q[i].a,&q[i].b); the } theSort (q+1, q+1+m);94 intZ=lca (q[1].x,q[1].y); theSolve (q[1].x,q[1].y); theReverse (z); ans[q[1].id]=sum; the if(p[q[1].a]&&p[q[1].b]&&q[1].a!=q[1].B) ans[q[1].id]--;98 reverse (z); About for(intI=2; i<=m;++i) - {101 intz=LCA (Q[I].X,Q[I].Y);102Solve (q[i-1].x,q[i].x);103Solve (q[i-1].y,q[i].y);104Reverse (z); ans[q[i].id]=sum; the if(p[q[i].a]&&p[q[i].b]&&q[i].a!=q[i].b) ans[q[i].id]--;106 reverse (z);107 }108 for(intI=1; i<=m;++i) printf ("%d\n", Ans[i]);109 return 0; the}
BZOJ3757 Apple Tree