Description
You is given a tree (an acyclic undirected connected graph) with n nodes. The tree nodes is numbered from 1 to n.
Each node have a color, white or black. All the nodes is black initially.
We'll ask you to perfrom some instructions of the following form:
- 0 u : Ask for how many nodes is connected to u, and the other nodes is connected IFF all the node on the Path from u to v (inclusive u and v) has a same color.
- 1 u : Toggle the color of u(that's, from black to white, or from white to black).
Input
The first line contains a number n denoted How many nodes in the tree (1≤ n ≤105). The Next n -1 lines, each line has a double numbers ( u , v ) describe a edge of the tree (1≤ u , v ≤ n ) . The next line contains a number m denoted How many operations we is going to PR Ocess (1≤ m ≤105). The Next m lines, each line describe a operation ( t , u ) as we mentioned above (0≤ t ≤1, 1≤ u ≤ n ).
Output
For each query operation, output the corresponding result.
Sample Input5
1 2
1 3
1 4
1 5
3
0 1
1 1
0 1Sample Output5
1
Exercises
First set the tree root, and then set up black and white two trees LCT
For black trees, always meet if there is an edge u-v, then v must be a black point (U is not necessarily a black dot)
And for some point on the black tree, record how many black dots are connected by the imaginary edge, and maintain the sum of the chain on the LCT, the same as the white tree
For a single point colour, if it is a black spot, it should be disconnected from its Father node on a black tree, and then connected to its Father node on a white tree, with the same white dots.
For a single point of query, if it is a black spot, take it to the top of the chain to determine whether the point is not a black dot, not to output the top point of the son node, white spot the same way
Code
1#include <cstdio>2#include <iostream>3#include <cmath>4#include <cstring>5#include <algorithm>6 using namespacestd;7 Charch;8 BOOLOK;9 voidReadint&x) {Ten for(ok=0, Ch=getchar ();! IsDigit (CH); Ch=getchar ())if(ch=='-') ok=1; One for(x=0; isdigit (ch); x=x*Ten+ch-'0', ch=GetChar ()); A if(OK) x=-x; - } - Const intmaxn=100005; the Const intmaxm=200005; - intN,Q,OP,A,B,TOT,NOW[MAXN],SON[MAXM],PRE[MAXM],COL[MAXN],FA[MAXN]; - BOOLFLAG[MAXN]; - structlct{ + intid,fa[maxn],son[maxn][2],TOT[MAXN],SUM[MAXN]; - voidInitintXintOP) {tot[x]+=op,sum[x]+=op;} + intWhich (intx) {returnson[fa[x]][1]==x;} A BOOLIsRootintx) {returnson[fa[x]][0]!=x&&son[fa[x]][1]!=x;} at voidUpdateintx) { -sum[x]=Tot[x]; - if(son[x][0]) sum[x]+=sum[son[x][0]]; - if(son[x][1]) sum[x]+=sum[son[x][1]]; - } - voidRotateintx) { in intY=fa[x],z=fa[y],d=which (x), dd=which (y); -fa[son[x][d^1]]=y,son[y][d]=son[x][d^1],fa[x]=Fa[y]; to if(!isroot (y)) son[z][dd]=x; +fa[y]=x,son[x][d^1]=y,update (y), update (x); - } the voidSplay (intx) { * while(!isroot (x)) { $ if(IsRoot (fa[x])) rotate (x);Panax Notoginseng Else if(which (fa[x]) = =which (x)) rotate (fa[x]), rotate (x); - Elserotate (x), rotate (x); the } + } A voidAccessintx) { the for(intp=0; x;x=Fa[x]) { + splay (x); - if(son[x][1]) tot[x]+=sum[son[x][1]]; $ if(p) tot[x]-=Sum[p]; $son[x][1]=p,update (p=x); - } - } the voidCutintXinty) { - if(!y)return;WuyiAccess (x), splay (x), fa[son[x][0]]=0, son[x][0]=0, update (x); the } - voidLinkintXinty) { Wu if(!y)return; -Access (y), splay (y), splay (x), fa[x]=y,son[y][1]=x,update (y); About } $ intFind_left (intx) { for(Access (x), splay (x); son[x][0];x=son[x][0]);returnx;} - voidQueryintx) { - intt=find_left (x); splay (t); -printf"%d\n", col[t]==id?sum[t]:sum[son[t][1]]); A } +}t[2]; the voidPutintAintb) {pre[++tot]=now[a],now[a]=tot,son[tot]=b;} - voidAddintAintb) {Put (A, b), put (b,a);} $ voidDfsintu) { the for(intP=now[u],v=son[p];p; p=pre[p],v=Son[p]) the if(V!=fa[u]) fa[v]=u,t[1].link (v,u), DFS (v); the } the intMain () { -Read (n), t[0].id=0, t[1].id=1; in for(intI=1; i<n;i++Read (a), read (b), add (a); the for(intI=1; i<=n;i++) col[i]=1; the for(intI=1; i<=n;i++) t[1].init (I,1); AboutDfs1); the for(Read (q); q;q--){ the Read (OP), read (a); the if(OP) T[col[a]].cut (A,fa[a]), T[col[a]].init (a,-1), col[a]^=1, T[col[a]].init (A,1), T[col[a]].link (A,fa[a]); + ElseT[col[a]].query (a); - } the return 0;Bayi}
Bzoj3637:query on a tree VI