poj-3321-Apple Tree(树状数组)
来源:互联网 发布:市场份额数据 编辑:程序博客网 时间:2024/06/06 00:42
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 <cstdio>#include <cstring>#include <vector>#include <algorithm>#define N 100005using namespace std;int n, tree[N], begin[N], end[N], inc;typedef vector<int> INF;vector<INF> Tree(N);void dfs(int index){ int i, j, k, l = Tree[index].size(); begin[index] = ++ inc; for (i = 0; i < l; i++) if (!begin[Tree[index][i]]) dfs(Tree[index][i]); end[index] = inc;}void update(int x, int val){ while(x <= n) { tree[x] += val; x += x&-x; }}int sum(int x){ int ans = 0; while(x > 0) { ans += tree[x]; x -= x&-x; } return ans;}int main() {#ifndef ONLINE_JUDGE freopen("1.txt", "r", stdin);#endif int i, j, m, t, u, v; char c[2]; scanf("%d", &n); inc = 0; for (i = 1; i < n; i++) { scanf("%d%d", &u, &v); Tree[u].push_back(v); Tree[v].push_back(u); } dfs(1); for (i = 1; i <= n; i++) update(i, 1); //每个初始化有一个苹果 scanf("%d", &m); for (i = 0; i < m; i++) { scanf("%s%d", c, &t); if (c[0] == 'Q') { printf("%d\n", sum(end[t])-sum(begin[t]-1)); } else { u = sum(begin[t])-sum(begin[t]-1); if (u == 1) update(begin[t], -1); else update(begin[t], 1); } } return 0;}
- POJ 3321 Apple Tree(树状数组)
- poj-3321 Apple Tree(树状数组)
- poj 3321 Apple Tree(树状数组)
- POJ 3321 Apple Tree( 树状数组)
- POJ 3321 Apple Tree(树状数组)
- POJ 3321 Apple Tree(树状数组)
- POJ 3321 Apple Tree(树状数组)
- POJ---3321-Apple tree(树状数组)
- POJ 3321 Apple Tree (树状数组)
- poj-3321-Apple Tree(树状数组)
- POJ 3321-Apple Tree(树状数组)
- poj 3321 Apple Tree(树状数组)
- POJ 3321 Apple Tree (树状数组)
- poj 3321 Apple Tree( 树状数组 )
- POJ 3321 apple tree 树状数组
- POJ 3321 Apple Tree(DFS+树状数组)
- POJ 3321 Apple Tree 树状数组
- poj 3321 apple tree 树状数组 水
- 微信抢红包辅助工具 AccessibilityService
- initWithCoder: 与initWithFrame:的区别
- iOS开发笔记--将视图或者按钮设置为圆形
- Redis安装部署
- Big-endian_littleEndian
- poj-3321-Apple Tree(树状数组)
- linux程序包管理
- Android Proguard 详解
- mysql查询首字母小写的例子
- ORA-12514错误:Listener refused the connection with the following&
- iOS开发笔记--如何去掉UItableview header(footer)view黏性(sticky)
- 仿微信界面的只能点击,不能滑动,和随机显示不同的布局
- 模板特例化
- Qt使用教程之创建Qt Quick应用程序(二)