1228 apple treestime limit: 1 sspace limit: 128000 KBtitle level: Diamonds DiamondTitle Description Description
Outside the Kaka's house, there is an apple tree. Every spring, there is always a lot of apples on the tree. Kaka was very fond of apples, so he always cared for the apple tree. We know that there are a lot of fork points in the tree, Apple will be on the fork point of the branches, and no more than two apples knot together. Kaka would like to know the number of apples on the subtree represented by a fork point, so as to study which branches of the apple tree have a stronger result.
What Kaka knows is that some of the apples will be on some forks at some point, but what Kaka doesn't know is that there are always some naughty kids to pick some apples off the tree.
So we define two operations:
|
C x |
Indicates that the state of the fork point with the number x is changed (the original is an apple, it is removed, the original is not, the knot an apple) |
|
G x |
How many apples are there in the subtree represented by a fork point that has a number x? |
We assume that at the outset, all the trees were apples, and also included the fork 1 as the root node.
Enter a description input Description
First row one number n (n<=100000)
Next n-1 line, 2 number u,v per line, indicates that the fork point U and Fork Point v are directly connected.
Next line a number M, (m<=100000) indicates the number of queries
The next M-line, which represents the query, asks the format as described in the question Q X or C x
outputs description output Description
For each q x query, output the corresponding result, each line output a
sample input to sample
3
1 2
1 3
3
Q 1
C 2
Q 1
Sample output Sample outputs
3
2
Analytical analysis
First, the theoretical basis of the problem: Dfs sequence (tree) + line tree
Fortunately, the long Apple is not every second, or I will ctrl+w the QWQ
First read in and then the DFS sequence-based linear structure
So according to the DFS sequence, this problem can be attributed to: the tree on the single-point modification + subtrees
If you haven't read it yet, please complete the theoretical part of the DFS sequence QWQ (and then how to do super-obvious)
-------------------------------------------------------------------------
The first time I got this problem, I really didn't know how to write it.
At Czl summer camp, help me with the tree.
Orzczl
Code
1#include <cstdio>2#include <iostream>3 #defineMid (L+r)/24 #defineLC (RT<<1)5 #defineRC (rt<<1|1)6 #defineMAXN 10000007 using namespacestd;8 9 intDfn[maxn],tim =1, DFN_T[MAXN],N,A,B,C,M,POS,TL[MAXN];Ten CharCtr; One A structedge{ - int from, V; - }E[MAXN]; the - structnode{ - intsum; - }TREE[MAXN]; + - voidMaintain (intRT) { +Tree[rt].sum = tree[lc].sum+tree[rc].sum; A } at - voidBuildintRtintLintR) { - if(L = = R) Tree[rt].sum =1; - Else{ - build (Lc,l,mid); -Build (rc,mid+1, R); in - maintain (RT); to } + } - the voidAddintRtintLintRintPOS) { * if(L = = R) Tree[rt].sum ^=1; $ Else{Panax Notoginseng if(Pos <=mid) Add (lc,l,mid,pos); - ElseAdd (rc,mid+1, R,pos); the + maintain (RT); A } the } + - intQueryintRtintLintRintQlintQR) { $ if(QL <= L && R <= QR)returntree[rt].sum; $ Else{ - intAns =0; - if(QL <= mid) ans + =query (LC,L,MID,QL,QR); the if(QR > mid) ans + = query (rc,mid+1, R,QL,QR); - returnans;Wuyi } the } - Wu voidPRINT (intRtintLintR) { - if(L = = R) printf ("%d", tree[rt].sum); About Else{ $ PRINT (lc,l,mid); -PRINT (rc,mid+1, R); - } - } A + intTOT,FIRST[MAXN]; the voidInsertintUintv) { -tot++; $E[tot]. from=First[u]; theE[TOT].V =v; theFirst[u] =tot; the } the - voidDfsintNowintpre) { inDfn[now] = tim++; theTl[dfn[now]] =Now ; the //tree[dfn[now]].sum = 1; About for(inti = First[now];i;i = E[i]. from){ the intv =e[i].v; the if(V! =pre) { the DFS (v,now); + } -}dfn_t[now] = tim++; theTl[dfn_t[now]] =Now ;Bayi } the the intMain () { -scanf"%d",&n); - for(inti =1; I < n;i++){ thescanf"%d%d",&a,&b); the Insert (A, b); the Insert (b,a); the } - the theDfs1,-1); thetim--;94 //build (1,1,tim-1); the for(inti =1; I <= n;i++){ theAdd1,1, Tim,dfn[i]); the }98 Aboutscanf"%d",&m); - for(inti =1; I <= m;i++){101Cin >> Ctr >>Pos;102 if(Ctr = ='C') Add (1,1, Tim,dfn[pos]);103 Elseprintf"%d\n", Query (1,1, Tim,dfn[pos],dfn_t[pos]));104 //PRINT (1,1,tim-1); cout << Endl; the }106 107 //for (int i = 1;i <= n;i++) {108 //printf ("%d", Dfn[i]);109 //}cout << "# #"; the 111 //for (int i = 1;i <= tim;i++) printf ("%d", Tl[i]); the 113 return 0; the}low Mood = =
[Codevs] 1228 apple tree