(POJ3321)Apple Tree <dfs序列 树状数组>
2016-12-05 15:41
465 查看
Apple Tree
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
Source
POJ Monthly–2007.08.05, Huang, Jinsong
题意:
一棵具有n个节点的树,一开始,每个节点上都有一个苹果。现在给出m组动态的操作:(C,i)是摘掉第i个节点上面的苹果(若苹果不存在,则为加上一个苹果),(Q,i)是查询以第i个节点为根的子树有几个苹果(包括第i个节点)。
分析:
树状数组。这道题重点怎么建立树到树状数组的映射关系:利用dfs遍历树,对每个节点进行两次编号,第一次搜到第i个节点时的深度dep,为这个节点管辖区间的上限lowi,然后搜这个节点的子节点,最后搜回来后的深度dep,为这个节点管辖区间的下限high[i],如下图所示。
做了以上操作后就相当于我们给了每个节点编了号即low[i].并且我们知道了以i为根节点的树所包含的节点即{low[i],high[i]},并且是一段连续的区间。
当我们要求以i节点为根节点的子树的苹果数时,其实就是要求节点low[i]~high[i]的和。
关于求连续区间的和,并且有动态点修改时,树状数组就可以非常好的处理了。
注意:每次更新是处理该节点的编号处的值,即处理的是low[i]
AC代码:
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
Source
POJ Monthly–2007.08.05, Huang, Jinsong
题意:
一棵具有n个节点的树,一开始,每个节点上都有一个苹果。现在给出m组动态的操作:(C,i)是摘掉第i个节点上面的苹果(若苹果不存在,则为加上一个苹果),(Q,i)是查询以第i个节点为根的子树有几个苹果(包括第i个节点)。
分析:
树状数组。这道题重点怎么建立树到树状数组的映射关系:利用dfs遍历树,对每个节点进行两次编号,第一次搜到第i个节点时的深度dep,为这个节点管辖区间的上限lowi,然后搜这个节点的子节点,最后搜回来后的深度dep,为这个节点管辖区间的下限high[i],如下图所示。
做了以上操作后就相当于我们给了每个节点编了号即low[i].并且我们知道了以i为根节点的树所包含的节点即{low[i],high[i]},并且是一段连续的区间。
当我们要求以i节点为根节点的子树的苹果数时,其实就是要求节点low[i]~high[i]的和。
关于求连续区间的和,并且有动态点修改时,树状数组就可以非常好的处理了。
注意:每次更新是处理该节点的编号处的值,即处理的是low[i]
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int maxn = 100010; struct Edge { int u,v,next; Edge(){} Edge(int u_,int v_) { u = u_; v = v_; } }edges[2*maxn]; int head[maxn],c[maxn]; int e,n; void addedge(int u,int v) { edges[e] = Edge(u,v); edges[e].next = head[u]; head[u] = e++; } int sum(int x) { int res = 0; while(x > 0) { res += c[x]; x -= (x&(-x)); } return res; } void add(int x,int d) { while(x <= n) { c[x] += d; x += (x&(-x)); } } int low[maxn],high[maxn],dep; bool pick[maxn]; void dfs(int u) { low[u] = ++dep; for(int i=head[u];i!=-1;i=edges[i].next) dfs(edges[i].v); high[u] = dep; } int main() { int m,u,v,x; char ch; while(scanf("%d",&n)!=EOF) { memset(c,0,sizeof(c)); memset(pick,0,sizeof(pick)); memset(head,-1,sizeof(head)); e = 0; dep = 0; for(int i=0;i<n-1;i++) { scanf("%d%d",&u,&v); addedge(u,v); } dfs(1); for(int i=1;i<=n;i++) add(i,1); scanf("%d",&m); getchar(); for(int i=0;i<m;i++) { scanf("%c %d",&ch,&x); getchar(); if(ch == 'C') { if(pick[x]) { add(low[x],1); pick[x] = false; } else { add(low[x],-1); pick[x] = true; } } else { printf("%d\n",sum(high[x])-sum(low[x]-1)); } } } return 0; }
相关文章推荐
- selenium page object model
- android fragment的切换
- android群英传学习--弧线展示图与自定义topbar
- Android 轻松的查看与使用hide与internal API
- 微信小程序 Windows2008 R2服务器配置TLS1.2方法
- 安卓使用加解密异常集合
- android省市区区域联动地址优化
- Android笔记——AsyncTask介绍
- Android-25种开源炫酷动画框架
- 笔记——android EditText多行显示并且支持imeOptions
- 微信中web页面实现和公众号中查看图片一样的效果
- 快速查找Unity中的死循环
- Android动画流畅心得
- Android 圆形ImageView实战
- html5移动端Meta设置
- weex入门学习总结
- Android数据绑定框架DataBinding,堪称解决界面逻辑的黑科技
- 【Unity&DragonBones】像素角色人物骨骼动画教程(一)PS文件如何导入DragonBones
- Monkey测试的策略和分析
- iOS 常用小功能