Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 19160 | Accepted: 5831 |
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
"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 beginning
Output
Sample Input
3 1 2 1 3 3 Q 1 C 2 Q 1
Sample Output
3 2
————————————————————分割线——————————
题目大意:
给定一个有n个节点的苹果树,树上初始每个节点有1个苹果。然后给出n-1条边。有两种操作:
1.如果节点x有苹果,就摘掉,否则就长苹果
2.求出以x节点为根的子树上的苹果数
思路:
将树形结构转变为线性结构——————方法dfs这棵树,某个节点第一次访问的编号和最后一次访问的编号之间的节点就是以该点为根的子树的所有子节点。
那么就可以套用BIT模板的改点求段.
用链表建立双向边,节点1肯定是根,dfs。
这题好像有个BUG,题目给出的N的范围是10万,但是我原来开10万+10wa了,后来改成100万就a了。 好神奇
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #define M 1000000+10 using namespace std; struct pp { int v,w,next; }edge[M];int tot,head[M],n,vis[M],c[M],app[M],cnt; inline void addedge(int u,int v,int w,int *h) { edge[tot].v=v; edge[tot].w=w; edge[tot].next=h[u]; h[u]=tot++; } struct node { int s,e; }a[M]; void dfs(int u) { a[u].s=cnt; vis[u]=1; for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].v; if(vis[v]) continue; cnt++; dfs(v); } a[u].e=cnt; } void update(int x,int v) { for(int i=x;i<=n;i+=i&-i){ c[i]+=v; } } int getsum(int x) { int sum=0; for(int i=x;i>0;i-=i&-i){ sum+=c[i]; } return sum; } int main() { scanf("%d",&n); if(n==0) return 0; memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); tot=0; for(int i=0;i<n-1;++i){ int u,v; scanf("%d %d",&u,&v); addedge(u,v,0,head); addedge(v,u,0,head); } cnt=1; dfs(1); for(int i=1;i<=n;++i){ app[i]=1; } for(int i=1;i<=n;++i){ c[i]=i&-i; } int m,x; scanf("%d",&m); while(m--){ getchar(); char ch;int x; scanf("%c %d",&ch,&x); if(ch=='C'){ int v; if(app[x]){v=-1;app[x]=0;} else{app[x]=1;v=1;} //printf("\t%d....%d\n",a[x].s,v); update(a[x].s,v); } if(ch=='Q'){ printf("%d\n",getsum(a[x].e) - getsum(a[x].s-1)); } } return 0; }