Apple Tree
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 26762 |
|
Accepted: 7947 |
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 the integers u and v , which means fork u and fork v are connected by a branch.
The next line contains an integer m ( m ≤100,000).
the following M lines each contain a message which be either
" c x strong> "which means the existence of the apple on fork x has 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 F ork x , including the apple (if exists) on the fork x
Note The tree was 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 continue to continue the DFS sequence well the original understanding is actually Tarjan DFN and the low array to derive DFN and low after Dfn[i] is the starting point, Low[i] is the end, The middle section is the sub-tree of point I so that the DFS sequence can be "convenient maintenance of the subtree" and for most of the DFS sequence maintenance sequence of the problem, and the tree array is quite good combination of the problem is almost naked, open an array that represents the current node state can be
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 intap[100010],bit[100010];5 intbg[100010],ed[100010],cnt=0;6 intN;7typedefstruct{8 intto,nxt;9 }edge;TenEdge gra[200010]; One inthead[100010],num=0; A intAddintfrmintTo ) { -gra[++num].nxt=Head[frm]; -gra[num].to=to ; thehead[frm]=num; - return 0; - } - intDfsintUintFA) { +bg[u]=++CNT; - intJ; + for(j=head[u];j;j=gra[j].nxt) { A if(gra[j].to!=FA) Dfs (gra[j].to,u); at } -ed[u]=CNT; - return 0; - } - intlbintx) { - returnx& (-x); in } - intCintx) { to intnum=Ap[x]; +x=Bg[x]; - while(x<=N) { thebit[x]+=num; *x+=lb (x); $ }Panax Notoginseng return 0; - } the intQintx) { + inta1=0, a2=0; A intE=Ed[x]; the while(e) { +a1+=Bit[e]; -e-=lb (e); $ } $ intb=bg[x]-1; - while(b) { -a2+=Bit[b]; theb-=lb (b); - }Wuyi returnA1-A2; the } - intMain () { Wuscanf"%d",&n); - for(intI=1; i<n;i++){ About intx, y; $scanf"%d%d",&x,&y); - Add (x, y); - Add (y,x); - } ADfs1,0); + for(intI=1; i<=n;i++){ theap[i]=1; - c (i); $ } the intm; thescanf"%d",&m); the for(intI=1; i<=m;i++){ the Char inch[2]; - intx; inscanf"%s%d",inch,&x); the if(inch[0]=='C'){ theap[x]*=-1; About c (x); the } the Elseprintf"%d\n", Q (x)); the } + return 0; -}
View Code
Go to bed
[poj3321] Apple Tree (Dfs sequence + tree array)