【poj3321】Apple Tree——树状数组&DFS
2016-05-29 14:24
288 查看
题目
Apple TreeTime Limit: 2000MS Memory Limit: 65536K
Total Submissions: 24449 Accepted: 7295
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
描述:
给定一颗树,初始时每个节点上的权值均为1,之后有m次操作。C x表示将编号为x的节点上的权值取反
Q x表示查询以x为根节点的子树的权值和
题解:
用树状数组来维护区间的权值和,用dfs序来维护每个节点对应的区间,dfs开始时的深度为该节点对应区间的下界,dfs返回时的深度为该节点对应区间的上界,注意在dfs时深度要用++depth来维护,否则的话如果该节点是叶节点则区间长度不是0而是1。
代码:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 100005; struct edge { int v,nxt; }e[maxn]; int head[maxn],c[maxn],low[maxn],high[maxn],depth,n; bool vis[maxn],hasApple[maxn]; int lowbit(int x) { return x & (-x); } int cal(int x) { int sum = 0; for(;x;x -= lowbit(x))sum += c[x]; return sum; } void update(int x,int val) { for(;x <= n;x += lowbit(x))c[x] += val; } void dfs(int t) { low[t] = ++depth; vis[t] = true; for(int i = head[t];i > 0;i = e[i].nxt) { if(vis[e[i].v] == false) { dfs(e[i].v); } } high[t] = depth; } int main() { //freopen("in.txt","r",stdin); while(~scanf("%d",&n)) { int u,v,cnt = 1; for(int i = 0;i < n-1;i++) { scanf("%d%d",&u,&v); e[cnt].v = v; e[cnt].nxt = head[u]; head[u] = cnt++; } depth = 0; //一定要从0开始++depth,因为不知道当前节点还有没有孩子,如果没有的话返回的depth就会比自身的depth多1 dfs(1); for(int i = 1;i <= n;i++) { update(i,1); hasApple[i] = true; } int m;scanf("%d",&m); while(m--) { char op; int x; getchar(); scanf("%c %d",&op,&x); if(op == 'C') { if(hasApple[x]) { update(low[x], -1); hasApple[x] = false; } else { update(low[x], 1); hasApple[x] = true; } } else { printf("%d\n",cal(high[x]) - cal(low[x] - 1)); } } } return 0; }
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- POJ 2159 Ancient Cipher
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- POJ1050 最大子矩阵和
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)