Apple Tree
| Time Limit: 2000MS |
|
Memory Limit: 65536K |
| Total Submissions: 26989 |
|
Accepted: 8004 |
Description
There is a apple tree outside of Kaka ' s house. Every autumn, a lot of apples would grow in the tree. Kaka likes Apple very much, so he has been carefully nurturing the Big Apple tree.
The tree has N forks which is connected by branches. Kaka numbers the forks by 1 to N and the root are always numbered by 1. Apples'll grow on the forks and both Apple won ' t grow on the same fork. Kaka wants to know how many apples is there in a sub-tree, for his study of the produce ability of the apple tree.
The trouble is, a new Apple may grow in an empty fork some time and Kaka could pick an apple from the tree for his Desse Rt. Can you help Kaka?
Input
The first line contains a integer n (n ≤100,000), which is the number of the forks in the tree.
the following N -1 lines each contain double integers u and v, which means fork u an D Fork v is connected by a branch.
The next line contains an integer m (m ≤100,000).
The following M lines each contain a message which is either
"C x" which means the existence of the Apple on Fork x have been changed. i.e. if there is a n Apple on the fork and then Kaka pick it; Otherwise a new Apple has grown on the empty fork.
or
"Q x" which means an inquiry for the number of apples in the sub-tree above the fork X, Inc. Luding the apple (if exists) on the fork x
Note The tree is full of apples at the beginning
Output
for every inquiry, the output of the correspond answer per line.
Sample Input
33Q 1C 2Q 1
Sample Output
32
Source
POJ monthly--2007.08.05, Huang, JinsongFirst, the DFS sequence is processed, and the number of apples in a subtree is queried, and the number of apples between the point of origin and the point of entry of the root node is calculated. Open a BOOL record a location there is no Apple, when the changes are deleted, not added.
1 /**/2#include <iostream>3#include <cstdio>4#include <cmath>5#include <cstring>6#include <algorithm>7 using namespacestd;8 Const intmxn=400020;9 structedge{Ten intV,next; One }E[MXN]; A intHd[mxn],mcnt=0; - voidAdd_edge (intUintv) { -e[++mcnt].next=hd[u];e[mcnt].v=v;hd[u]=mcnt; the return; - } - intn,m; - intT[MXN]; + BOOLF[MXN]; - intLowbit (intx) {returnx&-x;} + voidAddintPintv) { A while(P<MXN) {t[p]+=v;p+=lowbit (P);} at } - intAskintp) { - intres=0; - while(p) {Res+=t[p];p-=lowbit (P);} - returnRes; - } in int inch[MXN], out[MXN]; - intCnt=0; to voidDfsintUintFA) { + inch[u]=++CNT; - for(intI=hd[u];i;i=E[i].next) { the intv=e[i].v; * if(V==FA)Continue; $ DFS (v,u);Panax Notoginseng } - out[u]=++CNT; the return; + } A intMain () { thescanf"%d",&n); + inti,j; - intu,v; $ for(i=1; i<n;i++){ $scanf"%d%d",&u,&v); - Add_edge (u,v); - Add_edge (v,u); the } -Dfs1,0);Wuyi for(i=1; i<=n;i++){ thef[i]=1; -Addinch[I],1); Wu } -scanf"%d",&m); About Charch[2]; $ for(i=1; i<=m;i++){ -scanf"%s%d",ch,&u); - if(ch[0]=='Q') -printf"%d\n", Ask ( out[u]) -ask (inch[u]-1)); A Else{ + if(F[u]) { thef[u]=0; -Addinch[u],-1); $ } the Else{ thef[u]=1; theAddinch[u],1); the } - } in } the return 0; the}
POJ3321 Apple Tree