POJ 3321 Apple Tree 【二叉树结点个数的统计也可以用树状数组来做】

Apple Tree
Time Limit: 2000MS  Memory Limit: 65536K
Total Submissions: 7907  Accepted: 2194

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
"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
/*
这道题乍一看好像不能用树状数组来解决,后仔细想了想发现是可以的,
其实只需要将一棵树形结构映射成树状数组的下标范围即可
1)首先需要利用邻接表来建树
2)然后对这棵树进行先序遍历并给各个节点重新赋值下标,同时算出当
前节点及其子树的最小(其实就是自己的新下标)和最大下标,那么当前
节点所涵盖的范围就是[最小下标,最大下标]了
3)建立树状树状,每个节点初值利用modify加1(因为初始每个节点都有
苹果).对于原结点i进行修改操作,只需调用modify(i的最小下标)即可;
对于原始结点i进行查询操作,只需调用sum(i的最大下标) - sum(i的最小下标-1)即可,
其实画个图就很明白了.
*/
#include <iostream>
#define MAX_N 100000
using namespace std;
struct edge
{
    int fromId, to;
    edge *next;
    edge()
    {
        fromId = to = 0;
        next = NULL;
    }
}*head[MAX_N + 5];
bool has[MAX_N + 1];
int count[MAX_N + 1];
int idLH[MAX_N + 1][2];
int num, curId = 0;
void addEdge(int from, int to)
{
    edge *ne = new edge();
    ne->fromId = from;
    ne->to = to;
    ne->next = head[from];
    head[from] = ne;
}
int dfsGetId(int id)
{
    idLH[id][0] = ++curId;
    edge *ne = head[id];
    int highId = INT_MIN;
    if(curId > highId) highId = curId;
    while(ne != NULL)
    {
        if((curId = dfsGetId(ne->to)) > highId) highId = curId;
        ne = ne->next;
    }
    idLH[id][1] = highId;
    return highId;
}
int lowerbit(int n)
{
    return n & (n ^ (n - 1));
}
void modify(int n, int val)
{
    int i;
    for(i = n; i <= num; i += lowerbit(i))
        count[i] += val;
}
int sum(int n)
{
    int i;
    int res = 0;
    for(i = n; i >= 1; i -= lowerbit(i))
        res += count[i];
    return res;
}
int main()
{
    int i, from, to;
    scanf("%d", &num);
    for(i = 1; i < num; i++)
    {
        scanf("%d%d", &from, &to);
        addEdge(from, to);
    }
    int temp = dfsGetId(1);
    for(i = 1; i <= num; i++)
    {
        has[i] = true;
        modify(i, 1);
    }
    int qNum, dest;
    char type;
    scanf("%d", &qNum);
    for(i = 1; i <= qNum; i++)
    {
        getchar();
        scanf("%c%d", &type, &dest);
        if(type == 'C')
        {
            if(has[dest]){ has[dest] = false; modify(idLH[dest][0], -1);}
            else {has[dest] = true; modify(idLH[dest][0], 1);}
        }
        else 
        {
            int res1 = sum(idLH[dest][1]);
            int res2 = sum(idLH[dest][0] - 1);
            printf("%d\n", res1 - res2);
        }
    }
    return 0;
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值