您的位置:首页 > 移动开发

poj_3321 Apple Tree(dfs序+树状数组)

2017-01-16 17:21 375 查看
Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 27778 Accepted: 8251
DescriptionThere 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?InputThe 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 forkx 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 xNote the tree is full of apples at the beginningOutputFor 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
树上统计更新问题。
这道题只需要求树上某结点与其子树的信息,所以可以用 dfs序+树状数组/线段树 维护。
这里有张图画得很清楚
而如果是树上两点之间的查询,好像可以用树链剖分维护。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 100010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;struct Node{int l, r;}interval[maxn];int n, m;//vector<int> vec[maxn]; 这样会超时typedef vector<int>ve; vector<ve>vec(100050);bool vis[maxn];int cot;int c[maxn];int lowbit(int x){return x&(-x);}void update(int x, int d){for(int i = x; i <= n; i += lowbit(i)) c[i] += d;}int Sum(int x){int res = 0;for(int i = x; i >= 1; i -= lowbit(i)) res += c[i];return res;}void dfs(int u){cot++, vis[u] = 1;interval[u].l = cot;for(int i = 0; i < vec[u].size(); i++){if(!vis[vec[u][i]]) dfs(vec[u][i]);}interval[u].r = cot;}int main(){while(~scanf("%d", &n)){cot = 0;memset(vis, 0, sizeof(vis));for(int i = 1; i <= n; i++) vec[i].clear();int u, v;for(int i = 1; i < n; i++){scanf("%d%d", &u, &v);vec[u].push_back(v);vec[v].push_back(u);}dfs(1);for(int i = 1; i <= n; i++) update(interval[i].l, 1);scanf("%d", &m);char s[2]; int x;while(m--){scanf("%s%d", s, &x);if(s[0] == 'C'){if(vis[x]) update(interval[x].l, -1), vis[x] = 0;else update(interval[x].l, 1), vis[x] = 1;}else if(s[0] == 'Q') printf("%d\n", Sum(interval[x].r) - Sum(interval[x].l-1));}}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: