【poj 3321】 Apple Tree 树状数组+dfs序
2016-04-05 15:05
387 查看
Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 23568 Accepted: 7118
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
题目链接:http://poj.org/problem?id=3321
题意:
给出一个苹果树,每个节点一开始都有苹果
C X,如果X点有苹果,则拿掉,如果没有,则新长出一个
Q X,查询X点与它的所有后代分支一共有几个苹果
思路:1为根节点,子树问题用dfs序,然后区间查询,单点修改–》树状数组
*注意!!
代码
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 23568 Accepted: 7118
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
题目链接:http://poj.org/problem?id=3321
题意:
给出一个苹果树,每个节点一开始都有苹果
C X,如果X点有苹果,则拿掉,如果没有,则新长出一个
Q X,查询X点与它的所有后代分支一共有几个苹果
思路:1为根节点,子树问题用dfs序,然后区间查询,单点修改–》树状数组
*注意!!
typedef vector<int> ve_int; vector<ve_int> lin(100005); !vector定义 ,不然TLE!
代码
#include<iostream> #include<vector> #include<stdio.h> #include<string.h> using namespace std; int in[100005],out[100005]; int C[100005]; int a[100005]; int n,m; int tot=0; typedef vector<int> ve_int;//---!!! vector<ve_int> lin(100005); void dfs(int x,int fa) { in[x]=++tot; int len=lin[x].size(); for(int i=0;i<len;i++) { int y=lin[x][i]; if(y==fa) continue; dfs(y,x); } out[x]=tot; } void add(int x,int y) { while(x<=n) { C[x]+=y; x+=x&(-x); } } int _get(int x) { int ret=0; while(x>0) { ret+=C[x]; x-=x&(-x); } return ret; } int main() { int aa,bb; scanf("%d",&n); for(int i=1;i<n;i++) { scanf("%d%d",&aa,&bb); lin[aa].push_back(bb); lin[bb].push_back(aa); } dfs(1,0); char s[3]; scanf("%d",&m); // for(int i=1;i<=n;i++) // cout<<i<<" "<<in[i]<<" "<<out[i]<<endl; for(int i=1;i<=m;i++) { scanf("%s%d",s,&aa); if(s[0]=='Q') printf("%d\n",out[aa]-in[aa]+1-(_get(out[aa])-_get(in[aa]-1))); else { if(a[aa]==0) { add(in[aa],1); a[aa]=1; } else { add(in[aa],-1); a[aa]=0; } } } }
相关文章推荐
- Android Log升级版
- Android 内置FM频道
- Android 实现耳机hook键长按
- Android图片裁剪(拍照和从相册选择)
- iOS - 正则表达式判断邮箱、手机号码验证、车牌号验证、车型、用户名、密码、昵称、身份证号
- Android开发中,那些让你觉得相见恨晚的方法、类或接口
- Android中在GridView网格视图上实现item拖拽交换的方法
- iOS开发的一些奇巧淫技
- NDK-JNI实战教程(一) 在Android Studio运行第一个NDK程序
- 移动端css技巧
- 如何在源码中使用app
- Thinkpad E450c进入BIOS
- iOS 支付宝集成获取私钥与公钥
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- android 登陆输入框的实现
- Android 第十天重置版 _Media
- Android Intent.FLAG详解
- Android Studio导入第三方类库的方法
- 完美解决Android的WebView加载失败(404,500),显示的自定义视图
- Android中读取数据库