POJ 3321 Apple Tree

Apple Tree
Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 20182Accepted: 6129

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?

POJ 3321 Apple Tree - 风未定 - NGUNAUJ

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
"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
"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
DFS对树 的结点进行标记 同时记录每个节点的管辖范围 Left[]~Right[].将树转换成一维数组。然后树状数组求区间和。
代码如下:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 100010
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;
int a[maxn],c[maxn];
int Left[maxn],Right[maxn];
int u,v;
vector<vector<int> >edge(maxn);
int cnt,n,m,t;
char s[2];
int lowbit(int i)
{
return i&(-i);
}
void add(int i,int d)
{
while(i<=n)
{
c[i]+=d;
i+=lowbit(i);
}
}
int sum(int i)
{
int ans=0;
while(i>0)
{
ans+=c[i];
i-=lowbit(i);
}
return ans;
}
void dfs(int i)
{
Left[i]=cnt;
for(int j=0;j<edge[i].size();j++)
{
cnt++;
dfs(edge[i][j]);
}
Right[i]=cnt;
}
void init()
{
cle(Left),cle(Right),cle(a),cle(c);
for(int i=0;i<maxn;i++)
edge[i].clear();
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(~scanf("%d",&n))
{
init();
cnt=1;
for(int i=1;i<n;i++)
{
scanf("%d%d",&u,&v);
edge[u].push_back(v);
}
dfs(1);
for(int i=1;i<=n;i++)
{
a[i]=1;
add(i,1);//初始化
}
scanf("%d",&m);
while(m--)
{
scanf("%s%d",&s,&t);
if(s[0]=='Q')
{
printf("%d\n",sum(Right[t])-sum(Left[t]-1));
}
else
{
if(a[t]) {add(Left[t],-1);a[t]-=1;}
else {add(Left[t],1);a[t]+=1;}
}
}


}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值