poj3321 Apple Tree
2015-06-06 13:14
561 查看
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
Sample Output
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
这题可以用树状数组做,这题的关键是怎么把树上的节点在坐标上表示,这里用到了时间戳,先把边用邻接表存起来(这里我认为要用双向边的,因为数据可能出现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; for(i=first[id];i!=-1;i=e[i].next){ if(vis[e[i].to]==0){ vis[e[i].to]=1;now++;dfs(e[i].to); } } b[id].end=now; } 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)); vis[1]=1; 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; }
相关文章推荐
- Android通过Service调用远程接口—AIDL-进程间通信
- 卡诺图和Apple Watch的第一次亲密接触
- ios view的frame和bounds之区别(位置和大小)
- Android 自定义ToggleButton
- android--系统jar包引用
- 浏览器对象模型(Browser Object Model)
- cocoStudio: Button设置锚点问题
- cocos2d-x的Helloworld程序运行流程(win32)
- Android加载图片内存溢出问题解决方法
- Android开发中比较耗时的一些操作小结
- 几个androidSDK镜像源
- Android中Service(后台服务)详解
- In-App Purchase Programming Guide
- ubuntu14.04 给android 创建wifi
- WebView 报 Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 3777 (WebViewCoreThre)
- 获取WebView加载网页后的cookies
- ArcGIS操作——object name is not a regular identifier for underlaying DBMS[ArcGIS企业级数据导入错误]
- 轻松学习之一--Objective-C消息转发
- android仿QQ消息列表拖拽气泡效果源码读后感(1)
- Android开发 NDK 编译和使用静态库、动态库 (转)