POJ 3321 Apple Tree(DFS序 ,修改节点值,子树求和)

来源:互联网 发布:淘宝店铺要交税吗 编辑:程序博客网 时间:2024/06/04 19:57

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

题意:两种操作,更新,求和,DFS序的简单应用。

题解:简单的DFS之后就是BIT单点更新和求和问题。

#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int INF = 0x3f3f3f3f;const int maxn=1e5+100;int c[maxn*2+100];int L[maxn],R[maxn],cnt;struct node{    int u,v;    int next;}e[maxn*2+10];int head[maxn];void addedge(int u,int v){    e[cnt].u=u;e[cnt].v=v;    e[cnt].next=head[u];    head[u]=cnt++;}int n,m,dfn;void dfs(int u,int pre){    L[u]=++dfn;    for(int i=head[u];i!=-1;i=e[i].next)    {        int to=e[i].v;        if(to==pre) continue;        dfs(to,u);    }    R[u]=++dfn;}int lowbit(int x){    return x&(-x);}void update(int x,int val){    while(x<=dfn)    {        c[x]+=val;        x+=lowbit(x);    }}int query(int x){    int sum=0;    while(x>0)    {        sum+=c[x];        x-=lowbit(x);    }    return sum;}void init(){    CLEAR(head,-1);    dfn=cnt=0;}int main(){    int x,y,op;char str[2];    while(~scanf("%d",&n))    {        init();        REP(i,n-1)        {           scanf("%d%d",&x,&y);           addedge(x,y);           addedge(y,x);        }        dfn=0;        dfs(1,-1);        CLEAR(c,0);        for(int i=1;i<=dfn;i++)            update(i,1);        scanf("%d",&m);        while(m--)        {            scanf("%s%d",str,&op);            if(str[0]=='C')            {                if(query(R[op])-query(R[op]-1)==1)                {                    update(R[op],-1);                    update(L[op],-1);                }                else                {                    update(R[op],1);                    update(L[op],1);                }            }            else            {                int ans=query(R[op])-query(L[op]-1);                printf("%d\n",ans/2);            }        }    }    return 0;}/**/


0 0
原创粉丝点击