poj-3321 Apple Tree(树状数组)

来源:互联网 发布:迅雷赚钱宝 监控软件 编辑:程序博客网 时间:2024/06/11 01:20
Apple Tree
Time Limit:2000MS     Memory Limit:65536KB 
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

题目大意:

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

输入是叉之间的关系,

1 2

1 3

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

下面是两种操作,Q 和C

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

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

树转化为树状数组:对一棵树进行深搜,然后将深搜的顺序重新标上号,然后放到一个数组中,记下每一个枝子得起始点和终结点,然后就可以用树状数组了。

#include <iostream>#include <cstdio>using namespace std;int n, m;int inc = 0;int num[100001];int start[100001];int end[100001];//要用到的广义表的结构struct TREE {int data;TREE * next;};TREE tree[100001];void dfs(int pos)//深搜标记节点起始位置{int i, j, k;start[pos] = ++ inc;TREE *p = tree[pos].next;while (p){if (start[p->data] == 0){dfs(p->data);}p = p->next;}end[pos] = inc;}//中间用到的规律值int lowBit(int x){return x&(x^(x-1));}//求从开始到这里的和int sSum(int end){int sum = 0;while (end > 0){sum += num[end];end -= lowBit(end);}return sum;}//更新自己并且和自己相关的void change(int pos, int tmp){while (pos <= n){num[pos] += tmp;pos += lowBit(pos);}}int main(){int j,k,s,t;scanf("%d", &n);for (int i = 1; i < n; i ++){//每一个点都建一个长长的链表,表示自己的一个分支scanf("%d%d", &s, &t);TREE *p = new TREE;p->data = t;p->next = tree[s].next;tree[s].next = p;TREE *q = new TREE;q->data = s;q->next = tree[t].next;tree[t].next = q;}//映射到树状数组dfs(1);//每个初始化有一个苹果for (int i = 1; i <= n; i ++){change(i, 1);}char ch;scanf("%d", &m);for (int i = 0; i < m; i ++){getchar();scanf("%c%d", &ch, &j);if (ch == 'C'){int sum = sSum(start[j]);sum -= sSum(start[j] - 1);if (sum == 1){change(start[j], -1);}else{change(start[j], 1);}}else{int sum = sSum(end[j]);sum -= sSum(start[j] - 1);printf("%d\n", sum);}}return 0;}


0 0
原创粉丝点击