poj3321 Apple Tree

来源:互联网 发布:php报价系统 编辑:程序博客网 时间:2024/05/20 06:42

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
这题可以用树状数组做,这题的关键是怎么把树上的节点在坐标上表示,这里用到了时间戳,先把边用邻接表存起来(这里我认为要用双向边的,因为数据可能出现5 1 2 3 2 4 3 5 3这种情况,这样如果存的是单向边用dfs的时候就会发生错误,但是poj的数据好像都是深度浅的节点在前,深的在后,所以存一条边也行= =。),然后用dfs记录起始时间和终止时间start[i],end[i].然后把更新的时候就更新这点的起始点,询问的时候就询问getsum[end]-getsum[start]+b[c].num;                                                                                                                                                                                                  
#include<iostream>  #include<stdio.h>  #include<string.h>  #include<math.h>  #include<string>  #include<algorithm>  using namespace std;  #define maxn 100004  int now;  int first[maxn],vis[maxn],b1[maxn];  struct edge{      int to,next;  }e[2*maxn];  struct node{      int start,end,num;  }b[maxn];  char s[10];    void dfs(int id)  {      int i,j;      b[id].start=now;      vis[id]=1;    for(i=first[id];i!=-1;i=e[i].next){          if(vis[e[i].to]==0){              now++;dfs(e[i].to);          }      }      b[id].end=now;//时间戳的st[i]到ed[i]表示的是节点i的子树,并且这些子树按遍历顺序排列,回溯的时候时间不用加1}    int lowbit(int x){      return  x&(-x);  }  void update(int pos,int num)  {      while(pos<=maxn){          b1[pos]+=num;pos+=lowbit(pos);      }  }    int getsum(int pos)  {      int num=0;      while(pos>0){          num+=b1[pos];pos-=lowbit(pos);      }      return num;  }    int main()  {      int n,m,i,j,d,c;      while(scanf("%d",&n)!=EOF)      {          memset(b,0,sizeof(b));memset(b1,0,sizeof(b1));          for(i=1;i<=n;i++){first[i]=-1;b[i].num=1;update(i,1);}                    for(i=1;i<=n-1;i++){              scanf("%d%d",&c,&d);              e[i].to=d;              e[i].next=first[c];              first[c]=i;                            e[i+n-1].to=c;              e[i+n-1].next=first[d];              first[d]=i+n-1;          }                    memset(vis,0,sizeof(vis));          now=1;          dfs(1);          scanf("%d",&m);          for(i=1;i<=m;i++){              scanf("%s%d",s,&c);              if(s[0]=='C'){                  if(b[c].num==1){update(b[c].start,-1);b[c].num=0;}                  else {update(b[c].start,1);b[c].num=1;}              }              else{                  printf("%d\n",getsum(b[c].end)-getsum(b[c].start)+b[c].num);              }          }      }      return 0;  }

0 0
原创粉丝点击