Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16777 | Accepted: 5075 |
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
Sample Input
3 1 2 1 3 3 Q 1 C 2 Q 1
Sample Output
3 2
题意:有一棵苹果树,有n个分叉,每个分叉都有一个编号i,开始时每个分叉上有一个苹果。然后有m个操作,操作类型为c时,则改变分叉i,若分叉i的苹果数为1,则变为0,若为0,则变为1;操作类型为Q时,则计算出分叉i以为分叉i的所有分支上一共有多少个苹果。
思路:dfs+树状数组。先dfs一次,按照dfs访问的次序给每个结点标号,dfs结束后就得到了每个结点统辖的范围,这样我们就可以按照新的标号来维护树状数组。
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <stack> #include <vector> #include <map> #include <cmath> #include <cstdlib> #define L(rt) (rt<<1) #define R(rt) (rt<<1|1) #define ll __int64 #define eps 1e-6 using namespace std; const int INF = 1000000007; const int maxn = 100005; struct Edge{ int v, next; }et[maxn]; int eh[maxn], c[maxn], id[maxn], l[maxn], r[maxn], a[maxn]; int n, m, num, idx; void init() { memset(eh, -1, sizeof(eh)); num = 0; } void add(int u, int v) { et[num].v = v; et[num].next = eh[u]; eh[u] = num++; } int lowbit(int x) { return x & (-x); } void addn(int x, int d) { for(int i = x; i <= n; i += lowbit(i)) c[i] += d; } int sum(int x) { int ret = 0; for(int i = x; i > 0; i -= lowbit(i)) ret += c[i]; return ret; } void dfs(int u, int pre) { id[u] = ++idx; l[u] = idx; for(int i = eh[u]; i != -1; i = et[i].next) dfs(et[i].v, u); r[u] = idx; } int main() { int u, v; char op[3]; while(~scanf("%d", &n)) { init(); for(int i = 1; i <= n; i++) { a[i] = 1; addn(i, 1); } for(int i = 0; i < n - 1; i++) { scanf("%d%d", &u, &v); add(u, v); } idx = 0; dfs(1, -1); scanf("%d", &m); while(m--) { scanf("%s%d", op, &u); if(op[0] == 'C') { if(a[u]) { a[u] = 0; addn(id[u], -1); } else { a[u] = 1; addn(id[u], 1); } } else printf("%d\n", sum(r[u]) - sum(l[u] - 1)); } } return 0; }