# [POJ 3321] Apple Tree【树的DFS序+树状数组】

• 2018-01-21
• 0
• 0

## Problem:

 Time Limit: 2000MS Memory Limit: 65536K

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

## Code: O((N+M)logN) [5064K, 641MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int N, M;
bool apple;

struct Edge{
int np;
Edge *nxt;
};

struct Graph{
Edge *V, E;
int tope;

inline void addedge(int u, int v) {E[++tope].np = v, E[tope].nxt = V[u], V[u] = &E[tope];}
} G;

struct BIT{
int node;

inline void add(int u, int v) {while(u < 100010) node[u] += v, u += u & -u;}

inline int query(int u) {int res = 0; while(u) res += node[u], u -= u & -u; return res;}
} b;

int dfn, dfstime = 0;
int upp;

inline void DFS(int u, int fa){
dfn[u] = ++dfstime;
for(register Edge *ne = G.V[u]; ne; ne = ne->nxt)
if(ne->np != fa) DFS(ne->np, u);
upp[u] = dfstime;
}

int main(){
scanf("%d", &N);
for(register int i = 1; i < N; i++){
int u, v;
scanf("%d%d", &u, &v);
}
DFS(1, 0);
memset(apple, 1, sizeof(apple));
for(register int i = 1; i <= N; i++) b.add(i, 1);
scanf("%d", &M);
for(register int i = 1; i <= M; i++){
char op; int u;
scanf("%s%d", op, &u);
if(op == 'Q') printf("%d\n", b.query(upp[u]) - b.query(dfn[u] - 1));
else if(apple[u]) apple[u] = 0, b.add(dfn[u], -1);
else apple[u] = 1, b.add(dfn[u], 1);
}
return 0;
}
```

#### 评论

darkleafin.cf
(该域名已过期且被抢注。。)
darkleafin.github.io

49750

https://github.com/Darkleafin

OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.

https://visualgo.net/en

- Theme by Qzhai