POJ3321 Apple Tree【树+DFS+树状数组】

Apple Tree

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 34170   Accepted: 10243

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

问题链接POJ3321 Apple Tree

问题简述

  一棵树上各个节点(树叉和叶子)都有苹果,最多1个。每次可以摘掉一个苹果,也可能长出一个苹果。开始的时候,树上所有的地方都有苹果,如图所示。树的结构由输入数据指定。树上每个节点,有长苹果和不长苹果这两种状态。命令“C x”,将切换节点x的状态。命令“Q x”查询节点及其子树的苹果数量。编写程序,模拟执行命令过程,随时统计某个树叉(包含其子树)上有多少苹果。

问题分析

  这个问题可以用树状数组来解。

  需要注意的是,这棵树节点数量由输入的n决定的,数的结构由输入数据决定。

  先用DFS计算节点i管辖的范围,即开始start[i]和结束end[i]。这个需要给定的树保证各个节点的管辖范围是合理的,否则就没法算了。

  这样就可以用树状数组进行模拟,执行后续的命令并且根据命令输出计算结果。

程序说明

  程序中,树用图g(邻接表,双重向量)表示。

  程序中,由于变量重名,开始start[i]和结束end[i]数组的命名加数字。

  数组sign[]用于切换节点有无苹果的状态,开始时所有节点都有苹果,所以sign[i]初始化为1。

题记:(略)

参考链接:(略)

AC的C++语言程序如下:

/* POJ3321 Apple Tree */

#include <iostream>
#include <vector>
#include <stdio.h>
#include <string.h>

using namespace std;

const int N = 1e5 + 1;
int a[N + 1], n;
int cnt;

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

void update(int i, int v) //更新函数
{
    while(i <= n) {
        a[i] += v;
        i += lowbit(i);
    }
}

int sum(int i)      //求和函数
{
    int sum = 0;
    while(i > 0) {
        sum += a[i];
        i -= lowbit(i);
    }
    return sum;
}

vector< vector<int> > g(N);
int start2[N], end2[N], sign[N];

void dfs(int u)
{
    start2[u] = cnt;
    for(int i = 0; i < (int)g[u].size(); i++) {
        cnt++;
        dfs(g[u][i]);
    }
    end2[u] = cnt;
}

int main()
{
    while(~scanf("%d", &n)) {
        g.clear();
        g.resize(N +1);

        // 构建图
        for(int i = 1; i < n; i++) {
            int u, v;
            scanf("%d%d", &u, &v);
            g[u].push_back(v);
        }

        // DFS处理
        cnt = 1;
        dfs(1);

        // 初始化
        memset(a, 0, sizeof(a));
        for(int i = 1; i <= n; i++) {
            sign[i] = 1;
            update(i, 1);
        }

        int m, x;
        char cmd[8];
        scanf("%d", &m);
        for(int i = 1; i <=m; i++) {
            scanf("%s%d", cmd, &x);
            if(cmd[0] == 'Q')
                printf("%d\n", sum(end2[x]) - sum(start2[x] - 1));
            else {
                update(start2[x], sign[x] ? -1 : 1);
                sign[x] = 1 - sign[x];
            }
        }
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/81295180