POJ 3321 Apple Tree

眼前多少难甘事,自古男儿当自强。这篇文章主要讲述POJ 3321 Apple Tree相关的知识,希望能为你提供帮助。
题目链接:http://poj.org/problem?id=3321
解题思路:dfs加时间戳然后简单树状数组单点更新区间查询即可。
代码:

1 const int maxn = 1e5 + 5; 2 struct Edge{ 3int to, next; 4 }; 5 Edge edges[maxn]; 6 int head[maxn], tot; 7 int st[maxn], ed[maxn], cnt; 8 int bit[maxn], status[maxn], n, m; 9 10 void init(){ 11memset(head, -1, sizeof(head)); 12memset(st, 0, sizeof(st)); 13memset(ed, 0, sizeof(ed)); 14memset(status, 0, sizeof(status)); 15cnt = tot = 0; 16 } 17 void addEdge(int u, int v, int w){ 18edges[tot].to = v; 19edges[tot].next = head[u]; 20head[u] = tot++; 21 } 22 void dfs(int u){ 23cnt++; 24st[u] = cnt; 25for(int i = head[u]; i != -1; i = edges[i].next){ 26dfs(edges[i].to); 27} 28ed[u] = cnt; 29 } 30 void add(int i, int x){ 31while(i < = cnt){ 32bit[i] += x; 33i += i & (-i); 34} 35 } 36 int sum(int i){ 37int ans = 0; 38while(i > 0){ 39ans += bit[i]; 40i -= i & (-i) ; 41} 42return ans; 43 } 44 45 int main(){ 46scanf("%d", & n); 47init(); 48for(int i = 1; i < n; i++){ 49int u, v; 50scanf("%d %d", & u, & v); 51addEdge(u, v, 0); 52} 53dfs(1); 54for(int i = 1; i < = cnt; i++){ 55add(i, 1); 56} 57scanf("%d", & m); 58while(m--){ 59char ch; 60int x; 61scanf(" %c %d", & ch, & x); 62if(ch == ‘C‘){ 63if(status[st[x]] == 0){ 64status[st[x]] = 1; 65add(st[x], -1); 66} 67else{ 68status[st[x]] = 0; 69add(st[x], 1); 70} 71} 72else{ 73int ans = sum(ed[x]) - (st[x] == 1? 0: sum(st[x] - 1)); 74printf("%d\n", ans); 75} 76} 77 }

题目:
Apple Tree
Time Limit:  2000MS   Memory Limit:  65536K
Total Submissions:  30438   Accepted:  9091
Description
There is an apple tree outside of kaka‘s house. Every autumn, a lot of apples will 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 are connected by branches. Kaka numbers the forks by 1 to  N  and the root is always numbered by 1. Apples will grow on the forks and two apple won‘t grow on the same fork. kaka wants to know how many apples are there in a sub-tree, for his study of the produce ability of the apple tree.
The trouble is that a new apple may grow on an empty fork some time and kaka may pick an apple from the tree for his dessert. Can you help kaka?
 
POJ 3321 Apple Tree

文章图片
 
Input
The first line contains an integer  N  (N  ≤ 100,000) , which is the number of the forks in the tree.
The following  N  - 1 lines each contain two 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 is either
"C  x" which means the existence of the apple on fork  x  has been changed. i.e. if there is an apple on the fork, 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, including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning
Output
For every inquiry, output the correspond answer per line.Sample Input
3 1 2 1 3 3 Q 1 C 2 Q 1

Sample Output
3 2

【POJ 3321 Apple Tree】Source
POJ Monthly--2007.08.05, Huang, Jinsong







    推荐阅读