3321
来源:互联网 发布:cg用到哪些软件 编辑:程序博客网 时间:2024/05/22 00:32
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
Sample Input
31 21 33Q 1C 2Q 1
Sample Output
32
Source
我原本一直用无向图建图,一直WA,搞不清,用了有向图,就过了
用VECTOR效率低,竟然是内存9M,时间1000MS
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
typedef vector<int> VCT_INT;
vector<VCT_INT> tree(110001);
int start[110001*2],end[110001*2],c[110001*2];
int count,n;
int has[110001];
void dfs(int depth)
{
start[depth]=++count;
for(int i=0;i<tree[depth].size();i++)
dfs( tree[depth][i] );
end[depth]=++count;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int cc)
{
for( int i=x;i<=count;i+=lowbit(i) )
c[i]+=cc;
}
int Getsum(int x)
{
int temp=0;
for(int i=x;i>0;i-=lowbit(i))
temp+=c[i];
return temp;
}
int main()
{
//freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
tree[a].push_back(b);
}
count=0;
dfs(1);
for(int i=1;i<=n;i++)
has[i]=1;
for(int i=1;i<=count;i++)
c[i]=i-( i-lowbit(i) );
int m,x;
char str[10];
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%s %d",str,&x);
if(str[0]=='C')
{
if(has[x])
{
update(start[x],-1);
update(end[x],-1);
has[x]=0;
}
else
{
update(start[x],1);
update(end[x],1);
has[x]=1;
}
}
else
{
printf("%d/n",( Getsum( end[x] )-Getsum( start[x]-1 ) )/2);
}
}
}
return 0;
}
- 3321
- zoj 3321
- ZOJ 3321
- poj 3321
- poj 3321
- poj 3321
- poj 3321
- poj 3321
- poj 3321
- POJ 3321
- poj 3321
- POJ 3321
- POJ 3321
- POJ 3321
- POJ 3321 Apple Tree
- POJ 3321 Apple Tree
- poj 3321 Apple Tree
- zoj 3321 Circle
- JVM相关
- dwr 更新xml
- 在stylereport中做到一个单元格内写两个标题
- Cacti的安装(for CentOS 5.2)
- An unexpected error has been detected by Java Runtime Environment
- 3321
- qq申请器,有源码,用post提交
- c语言中static 用法总结
- 对不起,谢谢
- c++中的explicit关键字
- THE FIRST
- 在dos下,释放高端内存的命令!
- const和指针
- from program multi language support