"Title description"
Jiajia and Wind are a loving couple, and they have a lot of children. One day, Jiajia, wind and children decided to play hide-and-seek games at home. Their home is large and oddly constructed, consisting of n-houses and N-1 two-way corridors, and the distribution of the N-1 corridors allows any two rooms to reach each other.
The game is carried out, the children are in charge of hiding, Jiajia is responsible for finding, and wind is responsible for manipulating the lights of these n rooms. At first, all the lights were not turned on. Each time, the children will only hide in the room without the lights, but in order to increase irritation, the children will be asked to open a room lights or turn off a room lights. To assess the complexity of a particular game, Jiajia wants to know the distance from the farthest two children (i.e. the distance from the farthest two lights-out room).
We will define each operation as follows:
C (hange) I change the lighting status of room I, if it turns on, then turn it off, or turn it on if it turns off.
G (AME) Start a game and check the distance of the two off-lights room farthest.
"Input Format"
The first line contains an integer n, indicating the number of rooms, the room will be numbered ... An integer of N. The next line of N-1 lines is two integers a, B, which indicates a corridor connecting room A to room B. The next line contains an integer q that represents the number of operations. Then the Q line, one operation per line, as shown above.
"Output Format"
For each action game, output a non-negative integer to Hide.out, which represents the distance from the farthest two lights-out room. If only one room is turned off, the output is 0; if all the lights in the room are open, output-1.
"Sample Input"
8
1 2
2 3
3 4
3 5
3 6
6 7
6 8
7
G
C 1
G
C 2
G
C 1
G
"Sample Output"
4
3
3
4
Prompted
For 20% of data, n≤50, m≤100;
For 60% of data, n≤3000, m≤10000;
For 100% of data, n≤100000, m≤500000.
There are three ways of doing this problem.
Here I maintain a sequence of parentheses with a line segment tree.
1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 Const intmaxn=100010;6 Const intinf=1000000000;7 intcnt,fir[maxn],nxt[maxn*2],to[maxn*2];8 voidAddedge (intAintb) {9nxt[++cnt]=Fir[a];Tenfir[a]=CNT; Oneto[cnt]=b; A } - intC[MAXN]; - intid[maxn],rid[maxn*3],tot; the structnode{ - intA,b,l1,l2,r1,r2,dis; - voidInit (intp) { -dis=-inf;a=b=0; + if(rid[p]==-2) b=1; - if(rid[p]==-3) a=1; + if(rid[p]>0&&C[rid[p]]) AL1=l2=r1=r2=0; at Else -l1=l2=r1=r2=-INF; - } - voidpush_up (Node l,node r) { - intA1=l.a,b1=l.b,a2=r.a,b2=r.b; - if(B1>=A2) a=a1,b=b1+b2-A2; in Elsea=a1+a2-b1,b=B2; - todis=Max (L.dis,r.dis); +Dis=max (Dis,max (l.r1+r.l2,l.r2+r.l1)); - theR1=max (R.r1,max (l.r1+b2-a2,l.r2+a2+B2)); *R2=max (r.r2,l.r2+a2-B2); $ Panax NotoginsengL1=max (L.l1,max (a1-b1+r.l1,a1+b1+r.l2)); -L2=max (l.l2,r.l2+b1-A1); the } +}tr[(maxn*3) <<2]; A the voidDFS (intXintFA) { +rid[++tot]=-2; -rid[++tot]=x; $id[x]=tot; $ - for(intI=fir[x];i;i=Nxt[i]) - if(to[i]!=FA) the DFS (to[i],x); -rid[++tot]=-3; Wuyi } the - voidBuild (intXintLintR) { Wu if(l==R) { - Tr[x]. Init (l); About $ return; - } - intMid= (l+r) >>1; -Build (x<<1, l,mid); ABuild (x<<1|1, mid+1, R); +TR[X]. PUSH_UP (tr[x<<1],tr[x<<1|1]); the } - $ voidModify (intXintLintRintg) { the if(l==S) { the Tr[x]. Init (l); the return; the } - intMid= (l+r) >>1; in if(mid>=g) Modify (x<<1, l,mid,g); the ElseModify (x<<1|1, mid+1, r,g); theTR[X]. PUSH_UP (tr[x<<1],tr[x<<1|1]); About } the intn,q,x; the Charop[Ten]; the intMain () { + #ifndef Online_judge -Freopen ("hide.in","R", stdin); theFreopen ("Hide.out","W", stdout);Bayi #endif thescanf"%d",&n); the for(intI=1; i<=n;i++) c[i]=1; - for(intI=1, a,b;i<n;i++){ -scanf"%d%d",&a,&b); the Addedge (A, b); the Addedge (b,a); the } the -DFS (1,1); theBuild (1,1, tot); the thescanf"%d",&Q);94 while(q--){ thescanf"%s", op); the if(op[0]=='C'){ thescanf"%d",&x);98(C[x])? n--:n++;c[x]^=1; AboutModify (1,1, tot,id[x]); - }101 Else{102 if(n==0) printf ("-1\n");103 Else if(n==1) printf ("0\n");104 Elseprintf"%d\n", tr[1].dis); the }106 }107 return 0;108}
Data structure (bracket sequence, segment tree): Zjoi 2007 Hide and Seek