POJ3321Apple Tree

Apple Tree

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 39566 Accepted: 11727


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?


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


For every inquiry, output the correspond answer per line.

Sample Input

1 2
1 3
Q 1
C 2
Q 1

Sample Output



POJ Monthly--2007.08.05, Huang, Jinsong





#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <iostream>
void swap(int &a, int &b){int tmp = a;a = b, b = tmp;}
int max(int a, int b){return a > b ? a : b;}
int min(int a, int b){return a < b ? a : b;}
int lowbit(int x){return x & (-x);}
void read(int &x)
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;

const int INF = 0x3f3f3f3f;
const int MAXN = 100000 + 10;

struct Edge
	int u, v, nxt;
	Edge(int _u, int _v, int _nxt){u = _u, v = _v, nxt = _nxt;}
}edge[MAXN << 1];
int head[MAXN], cnt;

void insert(int a, int b)
	edge[++ cnt] = Edge(a, b, head[a]), head[a] = cnt;
	edge[++ cnt] = Edge(b, a, head[b]), head[b] = cnt;

int in[MAXN], out[MAXN], rank[MAXN], num[MAXN], t;

int n, m;

void modify(int x, int y)
	for(;x <= n;x += lowbit(x))
		num[x] += y;
int ask(int x)
	int sum = 0;
	for(;x;x -= lowbit(x))
		sum += num[x];
	return sum;

void dfs(int x)
	in[x] = ++ t;
	rank[t] = x;
	for(int pos = head[x];pos;pos = edge[pos].nxt)
		int v = edge[pos].v;
		if(in[v]) continue;
	out[x] = t;

int main()
	freopen("data.txt", "r", stdin);
	for(int i = 1;i < n;++ i)
		int tmp1, tmp2;
		read(tmp1), read(tmp2);
		insert(tmp1, tmp2);
	for(int i = 1;i <= n;++ i) modify(i, 1);
	for(int i = 1;i <= m;++ i)
		char tmp1;
		int tmp2;
		scanf("%c", &tmp1);
		if(tmp1 == 'Q')
			printf("%d\n", ask(out[tmp2]) - ask(in[tmp2] - 1));
		else if(tmp1 == 'C')
			int tmp3 = ask(in[tmp2]) - ask(in[tmp2] - 1);
			if(tmp3 == 1)	modify(in[tmp2], -1);
			else modify(in[tmp2], 1);
	return 0;
posted @ 2019-07-22 15:15  嘒彼小星  阅读(145)  评论(0编辑  收藏