【POJ 3321】Apple Tree
2016-03-11 23:53
375 查看
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
Sample Output
题目大意:一棵苹果树上有n个苹果,每个叉上都最多只能有1个苹果,1为根节点。现在有2种操作,Q为求以某一节点为根节点的子树上所有苹果的数量和。C是更新某一点上的苹果数量,如果原先为0则长出来一个苹果,否则则被吃掉。
思路:我们发现本题中的树为一个多叉树,而树的先序遍历即dfs则可以知道以某一节点为根节点的子树所包含的所有节点编号。所以我们用一个left和right数组记录i号苹果在先序遍历下的子树中的最小编号以及最大编号,即它的祖先和自己子树中的所有编号的最大值,然后求和时候用getSum(right[i])-getSum(left[i]-1)即可。
代码:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 23212 | Accepted: 7050 |
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
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #define N 100010 using namespace std; int n,a ,tot,lef ,rig ,vis ; vector<vector<int> >v(N); ///最开始用了vector,但是一直不过,百度了某大牛的代码,才发现和自己有同样的问题,改成这样就过了 int lowbit(int k) { return k&(-k); } int getSum(int i) { int res=0; while(i>0) { res+=a[i]; i-=lowbit(i); } return res; } void add(int i) { while(i<=n) { a[i]++; i+=lowbit(i); } } void eat(int i) { while(i<=n) { a[i]--; i+=lowbit(i); } } void dfs(int i) ///深搜对于每一个节点进行编号 { lef[i]=tot; for(int j=0;j<v[i].size();j++) { tot++; dfs(v[i][j]); } rig[i]=tot; } int main() { int m; while(~scanf("%d",&n)) { memset(vis,0,sizeof(vis)); memset(lef,0,sizeof(lef)); memset(rig,0,sizeof(rig)); memset(a,0,sizeof(a)); for(int i=0; i<N; i++) v[i].clear(); for(int i=1;i<n;i++) { int x,y; scanf("%d%d",&x,&y); v[x].push_back(y); } tot=1; dfs(1); for(int i=1;i<=n;i++) { vis[i]=1; add(i); } for(int i=1;i<=n;i++) { cout<<lef[i]<<" "<<rig[i]<<endl; } scanf("%d",&m); while(m--) { char op[5]; int q; scanf("%s%d",op,&q); if(!strcmp(op,"Q")) printf("%d\n",getSum(rig[q])-getSum(lef[q]-1)); if(!strcmp(op,"C")) { if(vis[q]) eat(lef[q]); else add(lef[q]); vis[q]=!vis[q]; } } } }
3 1 2 1 3 3 Q 1 C 2 Q 1
Sample Output
3 2
题目大意:一棵苹果树上有n个苹果,每个叉上都最多只能有1个苹果,1为根节点。现在有2种操作,Q为求以某一节点为根节点的子树上所有苹果的数量和。C是更新某一点上的苹果数量,如果原先为0则长出来一个苹果,否则则被吃掉。
思路:我们发现本题中的树为一个多叉树,而树的先序遍历即dfs则可以知道以某一节点为根节点的子树所包含的所有节点编号。所以我们用一个left和right数组记录i号苹果在先序遍历下的子树中的最小编号以及最大编号,即它的祖先和自己子树中的所有编号的最大值,然后求和时候用getSum(right[i])-getSum(left[i]-1)即可。
代码:
相关文章推荐
- Android菜鸟入职第一天
- iOS内功篇:runtime
- Android开发过程中用到的设计模式
- Android-面向Holder编程
- Unity3D 类似COC的鼠标拾取游戏物体并在限定区域摆放
- Android 之Service
- Android 之Overview Screen(任务列表)
- iOS开发常识
- Android 之Tasks和Back Stack(任务和返回栈)
- Android Stutio 提示错误:Plugin is too old, please update to a more recent version......的解决办法
- IOS逆向【2】-cydia之开发者模式
- PHPer转战Android的学习过程以及Android学习
- Android Uri介绍
- Android Studio初接触
- Android 框架之创建设备节点文件
- [置顶] 设置android studio启动时不检查sdk Android studio启动时总是在找AndroidSDK的解决办法
- 设计模式在Android中的应用
- 使用Xcode7的Instruments检测解决iOS内存泄露
- android 用相机照相并剪裁图片
- 小程序