Apple Tree
| Time Limit: 2000MS |
|
Memory Limit: 65536K |
| Total Submissions: 27092 |
|
Accepted: 8033 |
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 and fo RK 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 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, I Ncluding 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, Jinsong test instructions: An apple tree, each node has a maximum of one apple, each node initially has an apple, a non-binary tree, there are two operations, Q X, which represents the X-node subtree total of several apples, C X, Indicates an X node take off an apple or grow an apple (with a pick, no long) code:
1 /*2 Very good one problem, the subject to re-establish the tree array, with DFS from the root of the search, each search to a point his left value record the new serial number of the point, his3 The right value records the maximum depth in the subtree of the pity Dorado so that each point becomes a C array reference in the tree array, and then the template that sets the sum of the tree array interval is OK. 4 pay attention to the subject if the vector will be time-consuming, Vector<int>a[max] will timeout,vector<vector<int> > a (MAX) 985ms past (the followingwhat the hell!). 6 It's not as good as when you make your own achievements. 7 */8#include <iostream>9#include <cstdio>Ten#include <vector> One#include <cstring> A using namespacestd; - Const intmax=100005; - intC[max]; the intLef[max],rig[max]; - intn,m; - introt; - intHead[max]; + intLne; - //vector<int>a[max]; + //vector<vector<int> > A (MAX); A structnode at { - intTo,next; - }a[max]; - voidTaddintUintv) - { -a[lne].to=v; ina[lne].next=Head[u]; -head[u]=lne++; to } + intLowbit (intx) - { the returnx& (-x); * } $ voidAddintIdintval)Panax Notoginseng { - while(id<=N) the { +c[id]+=Val; Aid+=lowbit (ID); the } + } - intSumintID) $ { $ intsum=0; - while(id>0) - { thesum+=C[id]; -id-=lowbit (ID);Wuyi } the returnsum; - } Wu voidDfsintx) - { Aboutlef[x]=rot; $ //for (int i=0;i<a[x].size (); i++) - for(inti=head[x];i!=-1; i=a[i].next) - { -rot++; A //DFS (A[x][i]); + DFS (a[i].to); the } -rig[x]=rot; $ } the intMain () the { the intx, y; the Charch[5]; - intFlag[max]; inscanf"%d",&n); the for(intI=1; i<=n;i++) the { Aboutflag[i]=1; theAdd (I,1); the } theLne=0; +memset (head,-1,sizeof(head)); - for(intI=1; i<n;i++) the {Bayiscanf"%d%d",&x,&y); the //a[x].push_back (y); the Tadd (x, y); - } -rot=1; theDfs1); thescanf"%d",&m); the while(m--) the { -scanf"%s%d",ch,&x); the if(ch[0]=='C') the { the if(Flag[x])94Add (lef[x],-1); the ElseAdd (Lef[x],1); theflag[x]=!Flag[x]; the }98 Else About { -printf"%d\n", SUM (rig[x])-sum (lef[x]-1));101 }102 }103 return 0;104}
POJ 3321 tree-like array (+dfs+ re-achievement)