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

poj 3321:Apple Tree(树状数组,提高题)

2014-07-20 20:22 411 查看
Apple Tree

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 18623Accepted: 5629
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
  树状数组,提高题

  有些难度,要转一下弯。

  题意

  一开始给你n-1个边,构成一颗苹果树,默认这个时候每个分叉上都有苹果。接下来有q个操作,这些操作分两种,要不有苹果摘走苹果,没苹果长出苹果,要不求某一分叉上所有的苹果个数。

  思路

  关键是在每一个分叉处记录一个开始时间,和结束时间,表示dfs时经过这个分叉的时间和回到这个分叉的时间。这里的时间其实是dfs遍历次序的定位。这样就相当于有了一个区间,用树状数组就可以求出这个分叉点上的所有苹果的个数。

  注意

  给你的n-1条边,是双向的,即a指向b,b也指向a,是无向图。

  测试数据(来自poj讨论版)

10

2 1
3 1
3 4
4 7
4 5
7 9
5 8
6 5
10 6

10
Q 1
C 5
Q 3
Q 4
C 5
Q 6
C 6
Q 8
C 9
Q 1
ans:
10
7
6
2
1
8


  代码

#include <iostream>
#include <stdio.h>
using namespace std;

#define MAXN 100010

int N;
int cnt=0;
int c[MAXN];
int start[MAXN];
int end[MAXN];

struct Node{
int num;
Node* next;        //孩子节点
Node()
{
next = NULL;
}
}tree[MAXN];    //临界表

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

void add(int d,int x)
{
while(d<=N){
c[d] += x;
d += lowbit(d);
}
}

int sum(int d)
{
int ans =0;
while(d>=1){
ans += c[d];
d -= lowbit(d);
}
return ans;
}

void dfs(int v)    //以r为根节点进行dfs遍历,返整个遍历之后的时间
{
start[v] = ++cnt;
Node* p = tree[v].next;
while(p){
if(start[p->num]==0)
dfs(p->num);
p = p->next;
}
end[v] = cnt;
}

void addedge(int a,int b)    //在苹果树上加分支
{
Node* p = new Node;
p->num = b;
p->next = tree[a].next;
tree[a].next = p;
}

int main()
{
int i,q;
scanf("%d",&N);
for(i=1;i<N;i++){
int a,b;
scanf("%d%d",&a,&b);
addedge(a,b);
addedge(b,a);
}
dfs(1);

for(i=1;i<=N;i++)    //初始化c[]
add(i,1);

scanf("%d",&q);
while(q--){    //q次操作
char cmd[10];
int d;
scanf("%s%d",cmd,&d);
if(cmd[0]=='C'){
if(sum(start[d])-sum(start[d]-1)==1)
add(start[d],-1);
else
add(start[d],1);
}
else if(cmd[0]=='Q'){
printf("%d\n",sum(end[d])-sum(start[d]-1));
}
}
return 0;
}


Freecode : www.cnblogs.com/yym2013
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: