Apple Tree poj3321 树状数组+DFS序

Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 32666   Accepted: 9812

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

题意是查询一个节点的子树所有的苹果数,或修改节点苹果数。

要知道子树的编号就需要得出DFS序,先序遍历,记下对于每个节点的第一个节点和最后一个遍历到的节点,就可以知道子树的范围。

然后修改的时候也这个顺序修改就可以了

#include <iostream>
#include<vector>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1e5+5;
int edge[maxn][100];
int length[maxn];
int s[maxn];
int in[maxn],out[maxn];
int num;
int n;
int tree[maxn];
void dfs(int pos)
{
//    vis[pos]=1;
    int len=length[pos];
    in[pos]=++num;
    for(int i=1;i<=len;i++)
    {
            dfs(edge[pos][i]);
    }
    out[pos]=num;
}
int lowbit(int i)
{
    return i&(-i);
}
void update(int x,int y)
{
    while(x<=n)
    {
        tree[x]+=y;
        x+=lowbit(x);
    }
}
int query(int x)
{
    int ans=0;
    while(x>0)
    {
        ans+=tree[x];
        //cout<<x <<ans<<endl;
        x-=lowbit(x);
    }
    return ans;
}
int main()
{
  //  int n;
    while(cin>>n)
    {
        num=0;
        memset(tree,0,sizeof(tree));
        memset(length,0,sizeof(length));

        for(int i=1;i<n;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            edge[u][++length[u]]=v;
        }
        dfs(1);
        int m;
        scanf("%d",&m);
        for(int i=1;i<=n;i++)
                update(i,1),s[i]=1;
        while(m--)
        {
            char x;
            int y;
            scanf(" %c%d",&x,&y);
            if(x=='C')
            {
                if(s[y]==1)update(in[y],-1),s[y]=0;
               else update(in[y],1),s[y]=1;
            }
            else
            {
                if((in[y]-1)!=0)printf("%d \n",query(out[y])-query(in[y]-1));
                else printf("%d \n",query(out[y]));
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/mayuqing98/article/details/79658172