【POJ3321】Apple Tree

今天药忘吃喽~ 2022-09-25 09:20 280阅读 0赞

Apple Tree














Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 26622   Accepted: 7900

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?

3321_1.gif

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

  1. 3
  2. 1 2
  3. 1 3
  4. 3
  5. Q 1
  6. C 2
  7. Q 1

Sample Output

  1. 3
  2. 2

Source

POJ Monthly—2007.08.05, Huang, Jinsong

树状数组+DFS

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<vector>
  6. #include<algorithm>
  7. #define LL long long
  8. using namespace std;
  9. const int N = 1e5+10;
  10. int Left[N],Right[N],C[N];
  11. typedef vector<int >IN;
  12. vector<IN>graph(N);
  13. bool vis[N];
  14. int n,key;
  15. int lowbit(int x) {
  16. return x&(-x);
  17. }
  18. void add(int x,int y) {
  19. while(x<=n) {
  20. C[x]+=y;
  21. x+=lowbit(x);
  22. }
  23. }
  24. int Sum(int x) {
  25. int s=0;
  26. while(x>0) {
  27. s+=C[x];
  28. x-=lowbit(x);
  29. }
  30. return s;
  31. }
  32. void dfs(int x) {
  33. Left[x]=key;
  34. for(int i=0; i<graph[x].size(); i++) {
  35. key++;
  36. dfs(graph[x][i]);
  37. }
  38. Right[x]=key;
  39. }
  40. void init() {
  41. for(int i=0; i<=n; i++) {
  42. Left[i]=0;
  43. Right[i]=0;
  44. C[i]=0;
  45. graph[i].clear();
  46. }
  47. }
  48. int main() {
  49. while(scanf("%d",&n)!=EOF) {
  50. init();
  51. for(int i=0; i<n-1; i++) {
  52. int a,b;
  53. scanf("%d%d",&a,&b);
  54. graph[a].push_back(b);
  55. }
  56. key=1,dfs(1);
  57. for(int i=1; i<=n; i++){
  58. vis[i]=1;
  59. add(i,1);
  60. }
  61. int m,x;
  62. char c;
  63. scanf("%d",&m);
  64. while(m--) {
  65. getchar();
  66. scanf("%c %d",&c,&x);
  67. if(c=='Q') {
  68. printf("%d\n",Sum(Right[x])-Sum(Left[x]-1));
  69. } else {
  70. int t=vis[x]==1?-1:1;
  71. add(Left[x],t);
  72. vis[x]=!vis[x];
  73. }
  74. }
  75. }
  76. return 0;
  77. }

题目地址:http://poj.org/problem?id=3321

发表评论

表情:
评论列表 (有 0 条评论,280人围观)

还没有评论,来说两句吧...

相关阅读

    相关 POJ 3321-Apple Tree【树状数组+DFS序】

    卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果。卡卡很喜欢苹果。树上有N个节点,卡卡给他们编号1到N,根的编号永远是1.每个节点上最多结一个苹果。卡卡想要了解某一个子树上一共

    相关 POJ--2255 Tree recovery

    补一下这一道恢复树的题目,前面好就做的吧。 题意:   就是给你一个前序遍历树和一个中序遍历树,让你恢复后序遍历树。([树的遍历][Link 1]) 解法: 利用了前序

    相关 [poj1741]Tree

    点分治模板题,可以将同一棵树的链分为两种:1.通过重心;2.在子树内部。第2种可以搜下去,第1种的答案即$\\sum\_\{i,j\}\[di+dj<=m\]-\\sum\\l