POJ3321 树状数组,多叉树转线性结构,经典基础题

1 题意

对于一个多叉树的,有两种操作,在某个点上摘一个苹果或者生成一个苹果(每个点要么有一个苹果要么有0个苹果);输出某个点作为root的子树上的苹果之和(包括root)。

2 分析

将多叉树用邻接表存储下来,通过DFS将多叉树转换为线性结构并且记录子树所在区间,然后构造树状数组,进行改动和查询。

3

#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
using namespace std;

const int maxn=100010;
//存储边:
struct Edge{
    int next;
    int from;
    int to;
}edge[maxn];
int head[maxn];
int cur,cnt;//cur-用于存储边,cnt-用于转变为线性结构
int n,m;
//记录线性结构+记录子树:
struct Apple{
    int left;
    int right;//apple[x].right实际上是结点x从树结构转变为线性结构的一个映射
}apple[maxn];

//树状数组存储结构:
int sum[maxn];//前缀和,sum[x,y]=sum[y]-sum[x-1];
int a[maxn];//结点的权值

void Ini(){
    cur=0;
    cnt=1;
    memset(head,-1,sizeof(head[0])*(n+1));
    memset(sum,0,sizeof(sum[0])*(n+1));
    memset(apple,0,sizeof(apple[0])*(n+1));
}
void Add(int from,int to){
    edge[cur].next=head[from];
    edge[cur].from=from;
    edge[cur].to=to;
    head[from]=cur++;
}
void Dfs(int u){
    apple[u].left=cnt;
    for(int i=head[u];i!=-1;i=edge[i].next){
        Dfs(edge[i].to);
    }
    apple[u].right=cnt++;
}
int lowbit(int x){
    return x&(-x);//x的二进制表示中,从最右边数第一个不为0的位数,返回2^x
}
void Change(int x,int value){
    for(int i=x;i<cnt;i+=lowbit(i)){
        sum[i]+=value;
    }
}
int Sum(int x){
    int res=0;
    for(int i=x;i>0;i-=lowbit(i)){
        res+=sum[i];
    }
    return res;
}
void Build(int value){
    for(int i=1;i<=n;i++){
        a[i]=1;
        Change(i,1);
    }
}
int main()
{
    int num1,num2;
    char str[3];
    while(~scanf("%d",&n)){
        Ini();
        for(int i=1;i<n;i++){
            scanf("%d%d",&num1,&num2);
            Add(num1,num2);
        }
        Dfs(1);
        Build(1);
        scanf("%d",&m);
        for(int i=1;i<=m;i++){
            scanf("%s%d",str,&num1);
            if(str[0]=='Q'){
                cout<<Sum(apple[num1].right)-Sum(apple[num1].left-1)<<endl;
            }
            else if(str[0]=='C'){
                if(1==a[apple[num1].right]){
                    a[apple[num1].right]=0;
                    Change(apple[num1].right,-1);
                }
                else if(0==a[apple[num1].right]){
                    a[apple[num1].right]=1;
                    Change(apple[num1].right,1);
                }
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值