(POJ3321)Apple Tree <dfs序列 树状数组>

来源:互联网 发布:禁止软件创建快捷方式 编辑:程序博客网 时间:2024/04/29 14:16

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 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
Source

POJ Monthly–2007.08.05, Huang, Jinsong

题意:
一棵具有n个节点的树,一开始,每个节点上都有一个苹果。现在给出m组动态的操作:(C,i)是摘掉第i个节点上面的苹果(若苹果不存在,则为加上一个苹果),(Q,i)是查询以第i个节点为根的子树有几个苹果(包括第i个节点)。

分析:
树状数组。这道题重点怎么建立树到树状数组的映射关系:利用dfs遍历树,对每个节点进行两次编号,第一次搜到第i个节点时的深度dep,为这个节点管辖区间的上限lowi,然后搜这个节点的子节点,最后搜回来后的深度dep,为这个节点管辖区间的下限high[i],如下图所示。
这里写图片描述
做了以上操作后就相当于我们给了每个节点编了号即low[i].并且我们知道了以i为根节点的树所包含的节点即{low[i],high[i]},并且是一段连续的区间。
当我们要求以i节点为根节点的子树的苹果数时,其实就是要求节点low[i]~high[i]的和。
关于求连续区间的和,并且有动态点修改时,树状数组就可以非常好的处理了。
注意:每次更新是处理该节点的编号处的值,即处理的是low[i]

AC代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int maxn = 100010;struct Edge{    int u,v,next;    Edge(){}    Edge(int u_,int v_)    {        u = u_;        v = v_;    }}edges[2*maxn];int head[maxn],c[maxn];int e,n;void addedge(int u,int v){    edges[e] = Edge(u,v);    edges[e].next = head[u];    head[u] = e++;}int sum(int x){    int res = 0;    while(x > 0)    {        res += c[x];        x -= (x&(-x));    }    return res;}void add(int x,int d){    while(x <= n)    {        c[x] += d;        x += (x&(-x));    }}int low[maxn],high[maxn],dep;bool pick[maxn];void dfs(int u){    low[u] = ++dep;    for(int i=head[u];i!=-1;i=edges[i].next)        dfs(edges[i].v);    high[u] = dep;}int main(){    int m,u,v,x;    char ch;    while(scanf("%d",&n)!=EOF)    {        memset(c,0,sizeof(c));        memset(pick,0,sizeof(pick));        memset(head,-1,sizeof(head));        e = 0; dep = 0;        for(int i=0;i<n-1;i++)        {            scanf("%d%d",&u,&v);            addedge(u,v);        }        dfs(1);        for(int i=1;i<=n;i++)            add(i,1);        scanf("%d",&m);        getchar();        for(int i=0;i<m;i++)        {            scanf("%c %d",&ch,&x);            getchar();            if(ch == 'C')            {                if(pick[x])                {                    add(low[x],1);                    pick[x] = false;                }                else                {                    add(low[x],-1);                    pick[x] = true;                }            }            else            {                printf("%d\n",sum(high[x])-sum(low[x]-1));            }        }    }    return 0;}
1 0
原创粉丝点击