poj3321(dfs序)
来源:互联网 发布:mac cyberduck使用 编辑:程序博客网 时间:2024/06/09 20:06
Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 29894 Accepted: 8905
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
#include <iostream>#include <stdio.h>#include <vector>#include <string.h>using namespace std;const int N=123456;int in[N],out[N];vector< vector<int> > vec(N);int s[N];int vis[N];int haspre[N];int t,sum;void init(int n){ for(int i=1;i<=n;i++){ vec[i].clear(); s[i]=1; } t=1; memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); memset(vis,0,sizeof(vis)); memset(haspre,0,sizeof(haspre));}void dfs(int u){ in[u]=t; for(int i=0;i<vec[u].size();i++){ int v=vec[u][i]; if(!vis[v]){ t++; vis[v]=1; dfs(v); } } out[u]=t;}struct node{ int l,r; int num;}a[4*N];void build(int l,int r,int node){ a[node].l=l; a[node].r=r; if(l==r){ a[node].num=1; return; } int mid=(l+r)/2; build(l,mid,2*node); build(mid+1,r,2*node+1); a[node].num=a[node*2].num+a[node*2+1].num;}void update(int index,int node,int n){ if(a[node].l==a[node].r && a[node].l==index){ a[node].num+=n; return; } int mid=(a[node].l+a[node].r)/2; if(index<=mid){ update(index,2*node,n); } else { update(index,2*node+1,n); } a[node].num=a[node*2].num+a[node*2+1].num;}void query(int l,int r,int node){ if(a[node].l>=l&&a[node].r<=r){ sum+=a[node].num; return; } int mid=(a[node].l+a[node].r)/2; if(r<=mid){ query(l,r,2*node); } else if(l>mid){ query(l,r,2*node+1); } else { query(l,mid,2*node); query(mid+1,r,2*node+1); }}int main(){ int n; while(~scanf("%d",&n)){ init(n); for(int i=0;i<n-1;i++){ int u,v; scanf("%d %d",&u,&v); vec[u].push_back(v); haspre[v]=1; } int root; for(int i=1;i<=n;i++){ if(!haspre[i]){ root=i; break; } } build(1,n,1); vis[root]=1; dfs(root); int m; scanf("%d",&m); char str[12]; int k; while(m--){ sum=0; scanf("%s",str); scanf("%d",&k); if(str[0]=='Q'){ int l=in[k]; int r=out[k]; query(l,r,1); printf("%d\n",sum); } else { if(s[k]){ update(in[k],1,-1); } else { update(in[k],1,1); } s[k]=!s[k]; } } } return 0;}
- poj3321(dfs序)
- POJ3321 dfs序+树状数组
- POJ3321(dfs序+树状数组)
- POJ3321/Apple tree/(DFS序+线段树)
- [jzoj1016][poj3321]苹果树(dfs序+树状数组维护)
- POJ3321 Apple Tree(DFS序 + 树状数组)
- poj3321-Apple Tree-dfs序+树状数组
- POJ3321[苹果树]--DFS序+树状数组
- Poj3321 Apple Tree【DFS序】【树状数组】
- POJ3321-Apple Tree(dfs序+线段树)
- poj3321 树状数组+dfs(时间戳)
- poj3321 dfs+树状数组
- 【树状数组--dfs序转化】poj3321 Apple Tree
- (POJ3321)Apple Tree <dfs序列 树状数组>
- poj3321-dfs序&树状数组&dfs序模板题-Apple Tree
- POJ3321:Apple Tree 树状数组 (一个点通过dfs变成两个值)
- poj3321 Apple Tree(DFS+树状数组)
- 【POJ3321】Apple Tree-DFS+树状数组维护
- Linux中的cat命令用法
- JavaSE语法小知识汇总
- 在Windows上安装zeplin,并安装到photoshop中
- LeetCode 17. Letter Combinations of a Phone Number
- 微信分享时如何自定义标题,简介和图片
- poj3321(dfs序)
- 水一发
- 《教机器人理解语义概念》翻译
- 2.1 Android Studio和Eclipse的对比与选择
- Android得到控件在屏幕中的坐标
- Math Magic hdu 4427 (dp)
- 离散
- 颜色选择器
- Max Sum of Max-K-sub-sequence HDU