POJ 3321 Apple Tree【dfs序+线段树】

来源:互联网 发布:免费淘宝全套模板下载 编辑:程序博客网 时间:2024/06/06 04:41

Apple Tree

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 toN 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 forku 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 forkx, 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

大神总结DFS序:点击打开链接

将树状结构转化为线性结构,用线段树维护;

#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>const int MAXN = 100005;using namespace std;int q[MAXN], in[MAXN], out[MAXN];int sum[MAXN << 2], head[MAXN];int n, m, id, cnt;struct node {        int to;        int next;}edge[MAXN << 1];void add_edge(int u, int v) {        edge[++cnt].to = v, edge[cnt].next = head[u], head[u] = cnt;        edge[++cnt].to = u, edge[cnt].next = head[v], head[v] = cnt;}void dfs(int x, int fa) { //将树状结构转化为线性结构,用线段树维护;        q[++id] = x;        in[x] = id;        for(int i = head[x]; i; i = edge[i].next) {                if(edge[i].to != fa)                        dfs(edge[i].to, x);        }        out[x] = id;}void build(int k, int L, int R) {        int mid = (L + R) >> 1;        if(L == R) {                sum[k] = 1;                return;        }        build(k << 1, L, mid);        build(k << 1 | 1, mid + 1, R);        sum[k] = sum[k << 1] + sum[k << 1 | 1];}int query(int k, int L, int R, int s, int e) {        if(L == s && R == e) {                return sum[k];        }        int mid = (L + R) >> 1;        if(mid >= e) {                return query(k << 1, L, mid, s, e);        }        else if(mid < s) {                return query(k << 1 | 1, mid + 1, R, s, e);        }        else {                return query(k << 1, L, mid, s, mid) + query(k << 1 | 1, mid + 1, R, mid + 1, e);        }}void updata(int k, int L, int R, int x) {        if(L == R) {                sum[k] ^= 1; //代表苹果的更新;                return;        }        int mid = (L + R) >> 1;        if(x <= mid) updata(k << 1, L, mid, x);        else updata(k << 1 | 1, mid + 1, R, x);        sum[k] = sum[k << 1] + sum[k << 1 | 1];}int main() {        int u, v, x;        scanf("%d", &n);        for(int i = 1; i < n; i++) {                scanf("%d %d", &u, &v);                add_edge(u, v);        }        dfs(1, 0);        scanf("%d", &m);        char ch[10];        build(1, 1, n);        for(int i = 1; i <= m; i++)        {                scanf("%s", ch + 1);                scanf("%d", &x);                if(ch[1] == 'Q')                        printf("%d\n", query(1, 1, n, in[x], out[x]));                else                        updata(1, 1, n, in[x]);        }        return 0;}

原创粉丝点击