POJ 3321 Apple Tree
来源:互联网 发布:交大长天软件怎么样 编辑:程序博客网 时间:2024/06/09 16:45
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
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
先确定节点关系,使每个节点都用一个线段表示,begin表示起始位置,end表示结束位置在树上搜索确定。然后就是纯树状数组模板了。
#include<iostream>#include<vector>#include<cstdio>using namespace std;const int maxn=100005;typedef vector<int>tmp;vector<tmp>v(maxn);int n,q,cnt,begin[maxn],end[maxn],c[maxn];bool b[maxn];void dfs(int u,int fa){ begin[u]=++cnt; for(int i=0;i<v[u].size();i++) if(v[u][i]!=fa) dfs(v[u][i],u); end[u]=cnt;}int lowbit(int x){ return x&(-x);}int getsum(int pos){ int sum=0; while(pos>0) { sum+=c[pos]; pos-=lowbit(pos); } return sum;}void chg(int pos,int x){ while(pos<=n) { c[pos]+=x; pos+=lowbit(pos); }}int main(){ scanf("%d",&n); for(int i=1;i<=n-1;i++) { int x,y; scanf("%d%d",&x,&y); v[x].push_back(y); v[y].push_back(x); } dfs(1,0); for(int i=1;i<=n;i++) { b[i]=1; chg(i,1); } scanf("%d",&q); while(q--) { int x; char ch[11]; scanf("%s%d",ch,&x); if(ch[0]=='C') { if(b[begin[x]]) { b[begin[x]]=0; chg(begin[x],-1); } else { b[begin[x]]=1; chg(begin[x],1); } } else printf("%d\n",getsum(end[x])-getsum(begin[x]-1)); } return 0;}
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- poj 3321 Apple Tree
- POJ 3321 Apple Tree
- Poj 3321 Apple Tree
- poj 3321 apple tree
- poj 3321 Apple Tree
- poj 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ--3321--Apple Tree
- POJ-3321-Apple Tree
- poj 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- POJ:3321 Apple Tree
- POJ 3321 Apple Tree
- 也谈谈机器学习中的Evaluation Metrics
- 关于digital ocean 的使用(转载)
- Android组件复习之基本组件06—autocompletetextview自动完成文本框
- Struts2笔记
- android利用shap画小圆圈(空心圆、实心圆)
- POJ 3321 Apple Tree
- ViewAnimation和DrawableAnimation
- android studio renamed plugin
- Android UI :Try of Toolbar
- 删除远程svn上idea中的module (另附Linux解压命名)
- 数据存储和界面展现之二
- 位运算符,逻辑运算符,比较运算符
- Android-自定义PopupWindow
- 跟我学Java多线程——线程池与阻塞队列