首页 > 代码库 > poj3321--Apple Tree(多叉树建树状数组)

poj3321--Apple Tree(多叉树建树状数组)

Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 19425 Accepted: 5923

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

31 21 33Q 1C 2Q 1

Sample Output

32

Source

POJ Monthly--2007.08.05, Huang, Jinsong
题目大意:

给你一颗苹果树,树的主干设为1,每一个分支设为一个数,一直到N,代表这颗苹果树。每个分支上面只能最多有一个苹果,也就是一个枝子上面不可能有两个苹果,另外注意一点,不要把苹果树想象成二叉树,苹果树每个节点可以分出很多叉,应该是多叉树。

 

输入是叉之间的关系,

1 2

1 3

就是主干上面两个叉分别是2 和3.

 

下面是两种操作,Q 和C

C   j  的意思是如果 j 这个枝子上面有苹果就摘下来,如果没有,那么就会长出新的一个

Q  j  就是问 j 这个叉上面的苹果总数。

 
 
将多叉树储存后,用dfs遍历,得到一个顺序,在这个顺序中,每个点都控制了一个范围,然后通过树状数组求解问题
例如:
在这个图中,通过遍历得到 1 2 5 6 2 7 4 8 9 以每个点为根,可以发现以它形成的树都紧邻这它,得到一个范围
如:1的范围1到9,2的范围2到4.。。。。
 这就是要求解的范围和,不断维护这个值就可以
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define maxn 110000struct node{    int next , v ;} p[maxn];int head[maxn] ;int c[maxn] , num , temp[maxn] ;int s[maxn] , e[maxn] ;int last[maxn] ;void addp(int u,int v,int i){    p[i].v = v ;    p[i].next = head[u] ;    head[u] = i ;}void dfs(int u){    int i ;    last[u] = num ;    s[ last[u] ] = num++ ;    for(i = head[u] ; i != -1 ; i = p[i].next)        dfs( p[i].v );    e[ last[u] ] = num - 1 ;    return ;}int lowbit(int x){    return x & -x ;}void add(int i,int k,int n){    while( i <= n )    {        c[i] += k ;        i += lowbit(i) ;    }}int sum(int i){    int ans = 0 ;    while( i )    {        ans += c[i] ;        i -= lowbit(i) ;    }    return ans ;}int main(){    int n , m , i , u , v , a ;    char str[10] ;    num = 1 ;    memset(head,-1,sizeof(head));    memset(last,-1,sizeof(last));    scanf("%d", &n);    for(i = 1 ; i <= n ; i++)    {        add(i,1,n);        temp[i] = 1 ;    }    for(i = 1 ; i < n ; i++)    {        scanf("%d %d", &u, &v);        addp(u,v,i);    }    dfs(1);    scanf("%d", &m);    while(m--)    {        scanf("%s %d", str, &a);        if( str[0] == 'Q' )        {            printf("%d\n", sum( e[ last[a] ] ) - sum( s[ last[a] ] - 1 ) );        }        else        {            if( temp[a] )            {                temp[a] = 0 ;                add( last[a],-1,n );            }            else            {                temp[a] = 1 ;                add( last[a],1,n );            }        }    }    printf("\n");    return 0;}

poj3321--Apple Tree(多叉树建树状数组)