poj_3321 Apple Tree(dfs序+树状数组)
2017-01-16 17:21
375 查看
Apple Tree
DescriptionThere 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 toN 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?InputThe 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 forku 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 forkx 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 forkx, including the apple (if exists) on the fork xNote the tree is full of apples at the beginningOutputFor every inquiry, output the correspond answer per line.Sample Input
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 27778 | Accepted: 8251 |
3 1 2 1 3 3 Q 1 C 2 Q 1Sample Output
3 2
树上统计更新问题。
这道题只需要求树上某结点与其子树的信息,所以可以用 dfs序+树状数组/线段树 维护。
这里有张图画得很清楚
而如果是树上两点之间的查询,好像可以用树链剖分维护。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 100010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;struct Node{int l, r;}interval[maxn];int n, m;//vector<int> vec[maxn]; 这样会超时typedef vector<int>ve; vector<ve>vec(100050);bool vis[maxn];int cot;int c[maxn];int lowbit(int x){return x&(-x);}void update(int x, int d){for(int i = x; i <= n; i += lowbit(i)) c[i] += d;}int Sum(int x){int res = 0;for(int i = x; i >= 1; i -= lowbit(i)) res += c[i];return res;}void dfs(int u){cot++, vis[u] = 1;interval[u].l = cot;for(int i = 0; i < vec[u].size(); i++){if(!vis[vec[u][i]]) dfs(vec[u][i]);}interval[u].r = cot;}int main(){while(~scanf("%d", &n)){cot = 0;memset(vis, 0, sizeof(vis));for(int i = 1; i <= n; i++) vec[i].clear();int u, v;for(int i = 1; i < n; i++){scanf("%d%d", &u, &v);vec[u].push_back(v);vec[v].push_back(u);}dfs(1);for(int i = 1; i <= n; i++) update(interval[i].l, 1);scanf("%d", &m);char s[2]; int x;while(m--){scanf("%s%d", s, &x);if(s[0] == 'C'){if(vis[x]) update(interval[x].l, -1), vis[x] = 0;else update(interval[x].l, 1), vis[x] = 1;}else if(s[0] == 'Q') printf("%d\n", Sum(interval[x].r) - Sum(interval[x].l-1));}}return 0;}
相关文章推荐
- 带你全方位学习Picasso(一)
- Android 蓝牙2.0的连接通讯
- 竖屏转横屏 Activity的切换效果
- Android 监听程序的安装、卸载、替换
- Android 保存bitmap图像到本地之后,相册查看不显示问题
- 做推送,怎么能不了解推送的 4 种消息形式呢?( Android 篇)
- android studio快捷键
- Android知识点收集
- List<Map<String,Object>>使用Java代码遍历以获取String,Object的值
- Android绘图板
- Android studio切换标签快捷键(Alt+左右键)
- Android基站定位——通过手机信号获取基站信息(一)
- IOS ARC下内存泄露总结
- 微信小程序 问题收集
- Unity3D占用内存太大的解决方法
- IOS图片处理基础详解
- 微信小程序开发基础篇之工具介绍(二)
- Google 发布 Android 性能优化典范
- 设计模式深入学习--State 状态模式(行为型模式)
- Android 5.0之后屏幕截图的方法