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

POJ 3321 Apple Tree【树状数组】

2014-08-18 14:20 417 查看
POJ 3321 Apple Tree【树状数组】http://poj.org/problem?id=3321

Apple Tree

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 19102Accepted: 5810
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
[Submit] [Go Back] [Status]
[Discuss]
【题意】一颗苹果树有n个节点,n-1条边,编号从1到n,根节点是1 。原先每个结点都有一个苹果,有两个操作,Q x表示询问以第x个节点为根节点的子树的苹果个数;C x 表示若第x个节点有苹果就把它摘掉,没有就长出苹果。
【分析】很明显是线段树,但我们一般用树状数组去解决。

在很多的情况下,线段树都可以用树状数组实现.凡是能用树状数组的一定能用线段树.当题目不满足减法原则的时候,就只能用线段树,不能用树状数组.例如数列操作如果让我们求出一段数字中最大或者最小的数字,就不能用树状数组了。

如何把一个树转化为一维数组呢?这是这道题目的关键,否则没法做。

对一棵树进行深搜,然后将深搜的顺序重新标上号,然后放到一个数组中,记下每一个枝子得起始点和终结点,然后就可以用树状数组了。

还是举个例子吧。。。如果一棵苹果树是这样的。



那么我们知道1是主干。。。。

深搜的顺序是1 4 6 5 7 2 3 当然这也是根据题目的输入建的广义表。

那么当我们深搜的时候就可以在下面标上:

1 4 6 5 7 2 3 (广义表里面的东西,深搜结果)

1 2 3 4 5 6 7(num数组我们求和要用的)

这样不就是树状数组了么。

我们用start【1】= 1 end【1】=7代表1以上的树枝为1-7,也就是所有

用 start【2】= 6 end【2】 = 7 代表2以上的树枝是 3

同理

start【4】=2 end【4】 = 5 这样我们就知道4上面的树枝了

每一个树枝在num【n】总数组中的位置,这样我们就可以计算啦。。。树状数组,和上面将的一样了,成功转换

具体键代码,main函数主要建广义表,dfs主要是找到每一个树枝在num【n】数组中的起点和终点并记录在start和end数组中。
【代码如下】
//Accepted	5044K	438MS	C++	1834B
//先确定节点关系,使每个节点都用一个线段表示,begin表示起始位置,
//end表示结束位置在树上搜索确定。然后就是纯树状数组模板了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int maxn = 100000 + 10;

struct Tnode
{
int to, next;
}node[2*maxn];

int n, m, edges;
bool apple[maxn], vis[maxn];
int f[maxn], cal[maxn], begin[maxn], end[maxn];

void addedge(int u, int v)
{
node[edges].to = v;
node[edges].next = f[u];
f[u] = edges++;
}

void dfs(int u)
{
edges++;
begin[u] = edges;
vis[u] = 1;
for(int i=f[u]; i!=-1; i=node[i].next)
if(!vis[node[i].to])
dfs(node[i].to);
end[u] = edges;
}

int lowbit(int x)
{
return x & (-x);
}

int getsum(int x)
{
int s = 0;
while(x > 0){
s += cal[x]; x -= lowbit(x);
}
return s;
}

void update(int x, int value)
{
while(x <= n){
cal[x] += value; x += lowbit(x);
}
}

int main()
{
int u, v;
char str[5];
scanf("%d", &n);
memset(f, -1, sizeof(f));
memset(vis, 0, sizeof(vis));
memset(cal, 0, sizeof(vis));
memset(apple, 1, sizeof(apple));
edges = 0;
for(int i=1; i<n; i++){
scanf("%d %d", &u, &v);
addedge(u, v);
addedge(v, u);
}
edges = 0;
dfs(1);
for(int i=1; i<=n; i++)
update(i, 1);
scanf("%d", &m);
while(m--)
{
scanf("%s %d", str, &u);
if(str[0]=='C'){
if(apple[u]){
update(begin[u], -1);
apple[u] = 0;
}
else{
update(begin[u], 1);
apple[u] = 1;
}
}
else
printf("%d\n", getsum(end[u])-getsum(begin[u]-1));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: