Descriptionnode
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.ios
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?app
Inputide
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 beginningui
Outputspa
Sample Inputcode
3 1 2 1 3 3 Q 1 C 2 Q 1
Sample Outputxml
3 2
這題能夠用樹狀數組作,這題的關鍵是怎麼把樹上的節點在座標上表示,這裏用到了時間戳,先把邊用鄰接表存起來(這裏我認爲要用雙向邊的,由於數據可能出現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; }