poj 3321 Apple Tree(dfs序+树状数组)

本文介绍了一道关于树形结构的数据结构题目,主要内容是如何通过深度优先搜索(DFS)和树状数组来实现节点上的苹果数量的动态增删及查询。文章提供了完整的代码实现,并解释了关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
"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
"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个节点的树,一开始,每个节点上都有一个苹果。现在给出m组动态的操作:(C,i)是摘掉第i个节点上面的苹果(若苹果不存在,则为加上一个苹果),(Q,i)是查询以第i个节点为根的子树有几个苹果(包括第i个节点)。

解 :首先你得明白什么是dfs序  左边的为刚开始遍历时间 右边为改点遍历结束时间,然后通过树状数组求解

void dfs(int u,int pre)
{
    in[u]=++cnt;
    for(int i=first[u];i;i=s[i].nxt)
    {
        int v=s[i].v;
        if(v==pre)
            continue;
        dfs(v,u);
    }
    out[u]=cnt;
}



#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#define eps 1e-8
const int inf = 0x3f3f3f3f;
const long long mod=1e9+7;
const int N=100020;
using namespace std;
int in[N],out[N];
struct node//领接表
{
  int v, nxt;
}s[N];
int ar[N],first[N];
char e;
bool pick[N];//是否被摘过
int n,u,c,m,cnt;
int lowbit(int x)
{
    return x&-x;
}
void add(int i,int w)
{
    while(i<=n)
    {
        ar[i]+=w;
        i+=lowbit(i);
    }
}
int sum(int k)
{
    int ans=0;
    while(k)
    {
        ans+=ar[k];
        k-=lowbit(k);
    }
    return ans;
}
void dfs(int u,int pre)
{
    in[u]=++cnt;
    for(int i=first[u];i;i=s[i].nxt)
    {
        int v=s[i].v;
        if(v==pre)
            continue;
        dfs(v,u);
    }
    out[u]=cnt;
}
int main()
{
   int k=1;
   scanf("%d",&n);
   for(int i=1;i<n;i++)
   {
       scanf("%d%d",&u,&c);
        s[k].v = c;
        s[k].nxt = first[u];
        first[u] = k ++;
   }
   memset(ar,0,sizeof(ar));
   memset(pick,0,sizeof(pick));
   for(int i=1;i<=n;i++)
        add(i,1);
   cnt=0;
   dfs(1,-1);
   scanf("%d",&m);
   while(m--)
   {
   cin>>e>>u;
   if(e=='C')
   {
       if(pick[u])
           add(in[u],1);
       else
           add(in[u],-1);
       pick[u]=1-pick[u];
   }
   else
   {
       int ans=sum(out[u])-sum(in[u]-1);
       printf("%d\n",ans);
   }
   }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值