POJ3321 Apple Tree(树状数组 + dfs + 线性表)
来源:互联网 发布:淘宝网男童运动套装 编辑:程序博客网 时间:2024/05/16 05:46
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
Sample Input
31 21 33Q 1C 2Q 1
Sample Output
32
给了一颗满是苹果的苹果树,n - 1条熟知,m次询问,若为Q,问你树叉x上共有多少苹果树,若为C,树叉x有苹果树则摘掉,没有则
长出一个。
首先用链表存储每个树叉的信息,然后dfs扫一次将信息保存到两个数组st, ed中,最后使用树状数组即可。
AC代码:
#include "iostream"#include "cstdio"#include "cstring"#include "algorithm"using namespace std;const int MAXN = 1e5 + 5;int n, m, c[MAXN], st[MAXN], ed[MAXN], num;struct node{/* data */int data;node *next;}t[MAXN];void dfs(int x){st[x] = ++num;node *p = t[x].next;while(p) {if(st[p -> data] == 0) dfs(p -> data);p = p -> next;}ed[x] = num;}int lobbit(int x){return x & (-x);}void update(int x, int v){for(int i = x; i <= n; i += lobbit(i))c[i] += v;}int get_sum(int x){int sum = 0;for(int i = x; i > 0; i -= lobbit(i))sum += c[i];return sum;}int main(int argc, char const *argv[]){while(scanf("%d", &n) != EOF) {num = 0;memset(c, 0, sizeof(c));for(int i = 1; i < n; ++i) {int u, v;scanf("%d%d", &u, &v);node *p = new node;p -> data = v;p -> next = t[u].next;t[u].next = p;node *q = new node;q -> data = u;q -> next = t[v].next;t[v].next = q;}dfs(1);for(int i = 1; i <= n; ++i)update(i, 1);scanf("%d", &m);for(int i = 0; i < m; ++i) {char op[2];int x;scanf("%s%d", op, &x);if(op[0] == 'C') {int tmp = get_sum(st[x]);tmp -= get_sum(st[x] - 1);if(tmp == 1) update(st[x], -1);else update(st[x], 1);}else {int ans = get_sum(ed[x]);ans -= get_sum(st[x] - 1);printf("%d\n", ans);}}}return 0;}
- POJ3321 Apple Tree(树状数组 + dfs + 线性表)
- poj3321 Apple Tree(DFS+树状数组)
- POJ3321 Apple Tree(DFS序 + 树状数组)
- poj3321-Apple Tree-dfs序+树状数组
- 【POJ3321】Apple Tree-DFS+树状数组维护
- poj3321 Apple Tree(树状数组+dfs)
- Poj3321 Apple Tree【DFS序】【树状数组】
- 【树状数组】 poj3321 Apple Tree
- POJ3321---Apple Tree(树状数组)
- POJ3321:Apple Tree(树状数组)
- POJ3321:Apple Tree(树状数组)
- POJ3321——Apple Tree(DFS+树状数组)
- 【poj3321】Apple Tree——树状数组&DFS
- (POJ3321)Apple Tree <dfs序列 树状数组>
- 【树状数组--dfs序转化】poj3321 Apple Tree
- poj3321——Apple Tree//树状数组
- Apple Tree(POJ3321)(树状数组)
- [POJ3321] Apple Tree 苹果树 - 树状数组
- DX控件循环找控件
- Java拾遗------IO流
- 快速选择算法
- 绘图与动画之使用自定义属性与颜色渐变实现色带加载动画
- 心路历程(一)-自学java两个月心得
- POJ3321 Apple Tree(树状数组 + dfs + 线性表)
- HDU 5381 The sum of gcd 询问区间内所有子区间的GCD和 [暴力法]
- python2.7学习笔记(8) ——模块
- Android OkHttp文件上传与下载的进度监听扩展
- 1080. Graduate Admission (30)
- 在Ubuntu下修改sublime默认模板代码
- 在Centos中安装并配置phpMyAdmin
- 【more effective c++读书笔记】【第5章】技术(2)——限制某个class所能产生的对象数量
- Activity传递数据(四)获得返回数据