poj 3321 Apple Tree
来源:互联网 发布:js点击链接下载图片 编辑:程序博客网 时间:2024/06/06 03:49
Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 26809 Accepted: 7962
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
【分析】
花式树状数组
先用dfs记录结点的发现时间dfn与退出时间exi,那么结点x的孩子节点y具有的特点就是:dfn[y]>dfn[x] && exi[y] < exi[x]
根据这一特点建立树状数组
【代码】
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#define fo(i,j,k) for(i=j;i<=k;i++)using namespace std;vector < vector<int> > f(100001);int c[100001],a[100001],pre[100001],next[100001];int n,m,cnt=1;inline int lowbit(int x) {return x&(-x);}inline void add(int x,int v){ while(x<=n) { c[x]+=v; x+=lowbit(x); }}inline int sum(int x){ int s=0; while(x>0) { s+=c[x]; x-=lowbit(x); } return s;}inline void dfs(int u){ pre[u]=cnt; int i,x=f[u].size()-1; fo(i,0,x) { int v=f[u][i]; cnt++; dfs(v); } next[u]=cnt;}int main(){ int i,j,k,u,v; scanf("%d",&n); fo(i,2,n) { scanf("%d%d",&u,&v); f[u].push_back(v); } cnt=1;dfs(1); fo(i,1,n) { a[i]=1; add(i,1); } scanf("%d",&m); while(m--) { char tmp[2]; scanf("%s",tmp); scanf("%d",&k); if(tmp[0]=='C') { if(a[k]) add(pre[k],-1); else add(pre[k],1); a[k]=!a[k]; } else printf("%d\n",sum(next[k])-sum(pre[k]-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
- POJ:3321 Apple Tree
- POJ 3321 Apple Tree
- CODE[VS] 1288 埃及分数
- Android下载工具类和json解析例子
- PHP条件循环
- 分布式存储fastDFS
- 头文件保护 #pragma once与#ifndef 的区别
- poj 3321 Apple Tree
- PHP函数
- <boost -01> boost::filesystem在visual studio 2015的安装/配置方法
- 一致代码段与非一致代码段
- java编程自学记录(day16)
- 【20160904】NOIP模拟赛T3
- eclipse配置tomcat图标在工具栏
- Islands
- 【2016普及组模拟考试】04 搜索 tribe(部落卫队)