Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16817 | Accepted: 5090 |
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
"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.
or
"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
Output
Sample Input
3 1 2 1 3 3 Q 1 C 2 Q 1
Sample Output
3 2
Source
【题目大意】:
给出一棵树,判断以某个节点为根的子树有多少个节点有苹果。。
【分析】:
首先对一棵树进行深搜,然后将深搜的顺序重新标上号<程序中存在了high元素中,此值也相当于是后序遍历>,记下每一个点即将进入扫描儿子及子树时的值<程序中存在了low中,此值也相当于对每个树枝编了下号,详见图1>,然后就可以用树状数组了。当要询问时,只需求getsum(high)-getsum(low-1),即该点子树结点数<包含该点>,若要执行C操作,那么分情况:用exist数组记录该点是否有苹果,若当前点有苹果,则用树状数组的add操作删掉该节点以下的苹果,若没有,则反过来,加上苹果
【代码】:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
#define MAX 200001
struct EDGE{int t,next;};
struct POINT{int low,high;};
EDGE a[MAX];
POINT b[MAX];
int N,M,last[MAX],index=1,tot=0,S[MAX];
bool exist[MAX];
int lowbit(int x){return x&(-x);}
void add(int from,int to)
{
a[++tot].t=to;
a[tot].next=last[from];
last[from]=tot;
}
void addx(int x,int v)
{
while(x<=N)
{
S[x]+=v;
x+=lowbit(x);
}
}
int getsum(int x)
{
int cnt=0;
while(x>0)
{
cnt+=S[x];
x-=lowbit(x);
}
return cnt;
}
void DFS(int x)
{
b[x].low=index;
if(!last[x])
{
b[x].high=index++;
return;
}
for(int i=last[x];i;i=a[i].next)
DFS(a[i].t);
b[x].high=index++;
}
void pre()
{
DFS(1);
for(int i=1;i<=N;i++)
S[i]=lowbit(i);
}
int main()
{
//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
memset(exist,true,sizeof(exist));
scanf("%d",&N);
for(int i=1;i<N;i++)
{
int A,B;
scanf("%d%d",&A,&B);
add(A,B);
}
pre();
scanf("%d",&M);
for(int i=1;i<=M;i++)
{
char ch;
int num;
scanf("\n%c%d",&ch,&num);
if(ch=='Q')
printf("%d\n",getsum(b[num].high)-getsum(b[num].low-1));
else if(ch=='C')
{
int sign;
if(exist[num])
sign=-1;
else sign=1;
exist[num]=!exist[num];
addx(b[num].high,sign);
}
}
//system("pause");
return 0;
}