[poj3321]Apple Tree(dfs序+树状数组)

Posted Pumbit-Legion

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[poj3321]Apple Tree(dfs序+树状数组)相关的知识,希望对你有一定的参考价值。

Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 26762   Accepted: 7947

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
 
继续继续继续
 
dfs序嘛
原来之前理解的是错的
其实就是tarjan的dfn与low数组
得出dfn与low后
dfn[i]是起点,low[i]是终点,中间的一段就是点i的子树了
这样dfs序就可以“方便的维护子树”
而对于大部分按照dfs序维护序列的题,与树状数组是相当好的组合
这道题差不多是裸题了,多开一个表示当前节点状态的数组就可以了
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 int ap[100010],bit[100010];
 5 int bg[100010],ed[100010],cnt=0;
 6 int n;
 7 typedef struct{
 8     int to,nxt;
 9 }edge;
10 edge gra[200010];
11 int head[100010],num=0;
12 int add(int frm,int to){
13     gra[++num].nxt=head[frm];
14     gra[num].to=to;
15     head[frm]=num;
16     return 0;
17 }
18 int dfs(int u,int fa){
19     bg[u]=++cnt;
20     int j;
21     for(j=head[u];j;j=gra[j].nxt){
22         if(gra[j].to!=fa)dfs(gra[j].to,u);
23     }
24     ed[u]=cnt;
25     return 0;
26 }
27 int lb(int x){
28     return x&(-x);
29 }
30 int c(int x){
31     int num=ap[x];
32     x=bg[x];
33     while(x<=n){
34         bit[x]+=num;
35         x+=lb(x);
36     }
37     return 0;
38 }
39 int q(int x){
40     int a1=0,a2=0;
41     int e=ed[x];
42     while(e){
43         a1+=bit[e];
44         e-=lb(e);
45     }
46     int b=bg[x]-1;
47     while(b){
48         a2+=bit[b];
49         b-=lb(b);
50     }
51     return a1-a2;
52 }
53 int main(){
54     scanf("%d",&n);
55     for(int i=1;i<n;i++){
56         int x,y;
57         scanf("%d %d",&x,&y);
58         add(x,y);
59         add(y,x);
60     }
61     dfs(1,0);
62     for(int i=1;i<=n;i++){
63         ap[i]=1;
64         c(i);
65     }
66     int m;
67     scanf("%d",&m);
68     for(int i=1;i<=m;i++){
69         char in[2];
70         int x;
71         scanf("%s %d",in,&x);
72         if(in[0]==\'C\'){
73             ap[x]*=-1;
74             c(x);
75         }
76         else printf("%d\\n",q(x));
77     }
78     return 0;
79 }
View Code

睡觉

以上是关于[poj3321]Apple Tree(dfs序+树状数组)的主要内容,如果未能解决你的问题,请参考以下文章

poj3321-Apple Tree(DFS序+树状数组)

[poj3321]Apple Tree(dfs序+树状数组)

POJ 3321 Apple Tree DFS序 + 树状数组

POJ - 3321 Apple Tree (dfs序+线段树)

POJ 3321 Apple Tree DFS序+fenwick

POJ-3321 Apple Tree---树状数组+DFS序