poj3321Apple Tree
来源:互联网 发布:perl 进入编程 编辑:程序博客网 时间:2024/06/03 03:35
难点是如何:将树型结构转为用数组表示: 这里用深搜标记一个点开始和结束时间#include <iostream>#include <cstdio>#include <vector>using namespace std;const int M = 100005;//vector<int>que[M];struct Node { int value; int next;}edge[M*2];int head[M];int apple[M];int cnt[M];struct node{ int s; int e;}tree[M];int n, k;int index;void init() { k = 0; index = 1; for(int i = 0; i < M; i++) {// que[i].clear(); tree[i].s = 0; tree[i].e = 0; cnt[i] = 0; apple[i] = 1; head[i] = -1; }}void add1(int a, int b) { edge[index].value = b; edge[index].next = head[a]; head[a] = index++;}void dfs(int v) { tree[v].s = ++k; for(int i = head[v]; i != -1; i = edge[i].next){ int u = edge[i].value; dfs(u); }// for(int i = 0; i < (int)que[v].size(); i++) {//// dfs(que[v][i]);// } tree[v].e = k;}int lowbit(int t) { return t & (-t);}void add(int v, int d) { while(v <= n) { cnt[v] += d; v += lowbit(v); }}int getsum(int v) { int s = 0; while(v > 0) { s += cnt[v]; v -= lowbit(v); } return s;}int main(){ char str[5]; int a, b, m; int sum; scanf("%d", &n); init(); for(int i= 1; i < n; i++) { scanf("%d%d", &a, &b); add1(a, b); // que[a].push_back(b); } dfs(1);// for(int i = 1; i <= n; i++){//// printf("%d %d\n", tree[i].s, tree[i].e);// } for(int i = 1; i <= n; i++) { add(i, 1); } scanf("%d", &m); for(int i = 0; i < m; i++) { scanf("%s%d", str, &a); if(str[0] == 'C') { if(apple[a] == 1) { add(tree[a].s , -1); apple[a] = -1; }else { add(tree[a].s, 1); apple[a] = 1; } }else { sum = getsum(tree[a].e); sum -= getsum(tree[a].s-1); printf("%d\n", sum); } } return 0;}
0 0
- poj3321Apple Tree
- poj3321Apple Tree
- 树转化成树状数组POJ3321Apple Tree
- POJ3321Apple Tree Dfs序 树状数组
- Tree
- tree
- tree
- TREE
- Tree
- Tree
- tree
- tree
- tree
- tree
- tree
- tree
- Tree
- Tree
- Oracle11服务器客户端下载地址
- leetcode之Word Break II
- 学习非刚性人脸跟踪
- 亲身经历的几次合同陷阱
- Ubuntu更新apt-get的源的方法以及其他源推荐
- poj3321Apple Tree
- linuxgrub引导启动过程详解
- Light Oj 1214 大数整除
- RTSP协议详解
- Unity 动画初探(2)
- 博弈类型的题 kiki's game hdoj 2147
- Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅
- 输出任意一年的年历(闲来没事,写了一个小程序)
- inline、block、inline-block的区别