呓语 | 杨英明的个人博客

专注于c++、Python,欢迎交流

By

poj 3321:Apple Tree(树状数组,提高题)

Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 18623   Accepted: 5629

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?

 

 

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
"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
"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

Source

POJ Monthly--2007.08.05, Huang, Jinsong

 

  树状数组,提高题

  有些难度,要转一下弯。

  题意

  一开始给你n-1个边,构成一颗苹果树,默认这个时候每个分叉上都有苹果。接下来有q个操作,这些操作分两种,要不有苹果摘走苹果,没苹果长出苹果,要不求某一分叉上所有的苹果个数。

  思路

  关键是在每一个分叉处记录一个开始时间,和结束时间,表示dfs时经过这个分叉的时间和回到这个分叉的时间。这里的时间其实是dfs遍历次序的定位。这样就相当于有了一个区间,用树状数组就可以求出这个分叉点上的所有苹果的个数。

  注意

  给你的n-1条边,是双向的,即a指向b,b也指向a,是无向图。

  测试数据(来自poj讨论版)

10

2 1
3 1
3 4
4 7
4 5
7 9
5 8
6 5
10 6

10
Q 1
C 5
Q 3
Q 4
C 5
Q 6
C 6
Q 8
C 9
Q 1
ans:
10
7
6
2
1
8

 

  代码

 1 #include <iostream>
 2 #include <stdio.h>
 3 using namespace std;
 4 
 5 #define MAXN 100010
 6 
 7 int N;
 8 int cnt=0;
 9 int c[MAXN];
10 int start[MAXN];
11 int end[MAXN];
12 
13 struct Node{ 
14     int num;
15     Node* next;        //孩子节点
16     Node()
17     {
18         next = NULL;
19     }
20 }tree[MAXN];    //临界表
21 
22 int lowbit(int x)
23 {
24     return x & (-x);
25 }
26 
27 void add(int d,int x)
28 {
29     while(d<=N){
30         c[d] += x;
31         d += lowbit(d);
32     }
33 }
34 
35 int sum(int d)
36 {
37     int ans =0;
38     while(d>=1){
39         ans += c[d];
40         d -= lowbit(d);
41     }
42     return ans;
43 }
44 
45 void dfs(int v)    //以r为根节点进行dfs遍历,返整个遍历之后的时间
46 {
47     start[v] = ++cnt;
48     Node* p = tree[v].next;
49     while(p){
50         if(start[p->num]==0)
51             dfs(p->num);
52         p = p->next;
53     }
54     end[v] = cnt;
55 }
56 
57 void addedge(int a,int b)    //在苹果树上加分支
58 {    
59     Node* p = new Node;
60     p->num = b;
61     p->next = tree[a].next;
62     tree[a].next = p;
63 }
64 
65 int main()
66 {    
67     int i,q;
68     scanf("%d",&N);
69     for(i=1;i<N;i++){
70         int a,b;
71         scanf("%d%d",&a,&b);
72         addedge(a,b);
73         addedge(b,a);
74     }
75     dfs(1);
76 
77     for(i=1;i<=N;i++)    //初始化c[]
78         add(i,1);
79 
80     scanf("%d",&q);
81     while(q--){    //q次操作
82         char cmd[10];
83         int d;
84         scanf("%s%d",cmd,&d);
85         if(cmd[0]=='C'){
86             if(sum(start[d])-sum(start[d]-1)==1)
87                 add(start[d],-1);
88             else
89                 add(start[d],1);
90         }
91         else if(cmd[0]=='Q'){
92             printf("%d\n",sum(end[d])-sum(start[d]-1));
93         }
94     }
95     return 0;
96 }

 

Freecode : www.cnblogs.com/yym2013

##原创声明 **转载请注明:[呓语](http://www.yangyingming.com) » [poj 3321:Apple Tree(树状数组,提高题)](http://www.yangyingming.com/article/58)**