[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

Solution:

此题是树的 DFS 序的模板题。

由于节点的整棵子树在 DFS 序中是一段连续的序列,且该序列以开始调用时的 DFS 值为左端点,以递归退出时的 DFS 值为右端点。

通过一次 DFS,我们就可以将所有节点的子树在 DFS 序中的区间求出,然后用树状数组 (Binary Indexed Tree / Finwick Tree) 维护前缀和即可。

注意在询问和修改时,树状数组下标均为节点的 DFS 值,而非节点本身。

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[100010];

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

struct Graph{
	Edge *V[100010], E[200010];
	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[100010];
	
	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[100010], dfstime = 0;
int upp[100010];

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);
		G.addedge(u, v), G.addedge(v, u);
	}
	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[5]; int u;
		scanf("%s%d", op, &u);
		if(op[0] == '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

常年不在线的QQ:
49750

不定期更新的GitHub:
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