您的位置:首页 > 移动开发

POJ 3321 Apple Tree 【树状数组 dfs记录时间戳】

2015-11-27 18:16 585 查看

Apple Tree

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 22366Accepted: 6798
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 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?



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 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 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 forkx, 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


恩,题目大意就是说,每一个树杈都有一个苹果,然后树枝上也有,C表示改变该点的状态,若为0则变为1 ,若为1 则变为0 ,最后问一树杈包括它自己所有的苹果数。看了人家的题解,才了解到要记录树杈所包括的范围/article/7126178.html

#include <iostream>
#include<cstdio>
#include<cstring>
#define maxn 100010
using namespace std;
int n,cnt,head[maxn],c[maxn];
bool apple[maxn],vis[maxn];
int begin[maxn],end[maxn];
struct node
{
int from,to,next;
};
node edge[maxn*2];
void add(int f,int t)
{
edge[cnt].from=f;
edge[cnt].to=t;
edge[cnt].next=head[f];
head[f]=cnt++;
}
void dfs(int x)
{
cnt++;
begin[x]=cnt;
vis[x]=1;
for(int i=head[x];i!=-1;i=edge[i].next)
{
if(!vis[edge[i].to])
dfs(edge[i].to);
}
end[x]=cnt;
}
int lowbit(int t)
{
return t&(t^(t-1));
}
void update(int x,int v)
{
for(int i=x;i<=n;i+=lowbit(i))
c[i]+=v;
}
int sum(int x)
{
int ans=0;
for(int i=x;i>0;i-=lowbit(i))
ans+=c[i];
return ans;
}
int main()
{
int m,a,b,p;
char s[10];
while(~scanf("%d",&n))
{
memset(head,-1,sizeof(head));
memset(apple,1,sizeof(apple));
memset(vis,0,sizeof(vis));
memset(c,0,sizeof(c));
memset(begin,0,sizeof(begin));
memset(end,0,sizeof(end));
cnt=0;
for(int i=1;i<n;++i)
{
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
cnt=0;
dfs(1);
for(int i=1;i<=n;++i)
update(i,1);
scanf("%d",&m);
while(m--)
{
scanf("%s%d",s,&p);
if(s[0]=='Q')
printf("%d\n",sum(end[p])-sum(begin[p]-1));
else
{
if(apple[p])
{
update(begin[p],-1);
apple[p]=0;
}
else
{
update(begin[p],1);
apple[p]=1;
}
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: