poj 3321 Apple Tree(dfs序+树状数组)

来源:互联网 发布:知乎 礼仪的重要性 编辑:程序博客网 时间:2024/06/07 01:20

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

31 21 33Q 1C 2Q 1

Sample Output

32

题意:一棵具有n个节点的树,一开始,每个节点上都有一个苹果。现在给出m组动态的操作:(C,i)是摘掉第i个节点上面的苹果(若苹果不存在,则为加上一个苹果),(Q,i)是查询以第i个节点为根的子树有几个苹果(包括第i个节点)。

解 :首先你得明白什么是dfs序  左边的为刚开始遍历时间 右边为改点遍历结束时间,然后通过树状数组求解

void dfs(int u,int pre){    in[u]=++cnt;    for(int i=first[u];i;i=s[i].nxt)    {        int v=s[i].v;        if(v==pre)            continue;        dfs(v,u);    }    out[u]=cnt;}



#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<map>#include<vector>#include<queue>#include<stack>#define eps 1e-8const int inf = 0x3f3f3f3f;const long long mod=1e9+7;const int N=100020;using namespace std;int in[N],out[N];struct node//领接表{  int v, nxt;}s[N];int ar[N],first[N];char e;bool pick[N];//是否被摘过int n,u,c,m,cnt;int lowbit(int x){    return x&-x;}void add(int i,int w){    while(i<=n)    {        ar[i]+=w;        i+=lowbit(i);    }}int sum(int k){    int ans=0;    while(k)    {        ans+=ar[k];        k-=lowbit(k);    }    return ans;}void dfs(int u,int pre){    in[u]=++cnt;    for(int i=first[u];i;i=s[i].nxt)    {        int v=s[i].v;        if(v==pre)            continue;        dfs(v,u);    }    out[u]=cnt;}int main(){   int k=1;   scanf("%d",&n);   for(int i=1;i<n;i++)   {       scanf("%d%d",&u,&c);        s[k].v = c;        s[k].nxt = first[u];        first[u] = k ++;   }   memset(ar,0,sizeof(ar));   memset(pick,0,sizeof(pick));   for(int i=1;i<=n;i++)        add(i,1);   cnt=0;   dfs(1,-1);   scanf("%d",&m);   while(m--)   {   cin>>e>>u;   if(e=='C')   {       if(pick[u])           add(in[u],1);       else           add(in[u],-1);       pick[u]=1-pick[u];   }   else   {       int ans=sum(out[u])-sum(in[u]-1);       printf("%d\n",ans);   }   }}