POJ 3321 Apple Tree(树状数组+DFS构建)

24 篇文章 0 订阅
19 篇文章 0 订阅

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

题目大意

现有一棵苹果树,有n个树枝,根始终编号为1,初始情况下每个树枝上都有一个苹果,C操作代表指定树枝上的苹果发生由无到有、由有到无的变化,Q操作代表查查询包括当前树枝和当前树枝上方的树枝共有多少个苹果。

解题思路

更新节点的值,询问区间和,可用树状数组求解,该题的难点在于如何将树转化为树状数组,即将树转化为一维数组。
这里写图片描述
构建成一维数组的思路就是先找到每个节点求和时涉及到的区间范围,即该节点及该节点以下(树我是倒着画的)一共包含多少个节点,如图所示,对于一棵树 8 (1 5) (1 3) (5 4) (5 2) (4 6) (4 7) (2 8),我们可以对照树将每个节点进行编号可以理解为节点的深度(图中带圆圈的数字),然后任一节点求和时包含的范围便可由最小的深度(即节点本身的编号)和最大的深度(节点以下最大的节点编号)表示,如图中节点5,本身深度为3,其以下的树枝节点包括 2、8、4、7、6,最大编号为8,所以节点5范围是(3,8)。该过程可通过DFS实现。
然后结果值便可通过树状数组进行维护
这里写图片描述
以节点5为例,节点5的区间为[3,8],维护时即为树状数组的思想,而节点5的求和即为c[8]-c[2]。

代码实现

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 100007
int s[maxn],e[maxn],f[maxn],c[maxn],n;
int mark=0;    //标记节点深度
bool vis[maxn],hav[maxn];
struct node
{
    int v;
    int pre;
} edge[maxn*2];
void dfs(int u)
{
    s[u]=++mark; 
    vis[u]=1;
    for(int i=f[u]; i; i=edge[i].pre)
        if(!vis[edge[i].v])
            dfs(edge[i].v);
    e[u]=mark;
}
void add(int j,int w)
{
    while(j<=n)
    {
        c[j]+=w;
        j+=(j&-j);
    }
}
int sum(int j)
{
    int ans=0;
    while(j>0)
    {
        ans+=c[j];
        j-=(j&-j);
    }
    return ans;
}
int main()
{
    int m,p;
    char ch;
    memset(f,0,sizeof(f));
    memset(vis,0,sizeof(vis));
    memset(hav,1,sizeof(hav));
    scanf("%d",&n);
    int u,v;
    int k=1;
    for(int i=1; i<n; i++)
    {
        scanf("%d %d",&u,&v);
        edge[k].v=v;
        edge[k].pre=f[u];
        f[u]=k++;
    }
    for(int i=1; i<=n; i++) //起初每一个树枝上都长有一个苹果
        add(i,1);
    dfs(1);  //dfs构建树状数组
    scanf("%d%*c",&m);
    for(int i=1; i<=m; i++)
    {
        scanf("%c%d%*c",&ch,&p);
        if(ch=='Q')
            printf("%d\n",sum(e[p])-sum(s[p]-1));
        else
        {
            if(hav[p])    //该树枝上有苹果,将该苹果摘走
            {
                add(s[p],-1);
                hav[p]=0;
            }
            else          //树枝上没有苹果,新长出来一个苹果
            {
                add(s[p],1);
                hav[p]=1;
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值