poj 3321 Apple Tree(dfs序+线段树)

本文介绍了一道关于苹果树的编程题目,通过构建一棵有N个节点的树,并实现对树的操作来解决苹果数量的查询及变更问题。利用DFS序进行优化处理。

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

Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 28674 Accepted: 8513
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
题意:给出一棵有n个节点的树,n-1条边, m个操作,Q:询问以x为根的子树的苹果数。C:将x点的苹果异或1。
题解:dfs序的例题。
代码:

#include<algorithm>
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
using namespace std;
const int N=110100;
int head[N],q[N],st[N],ed[N],sum[N<<2];
struct node{int to,next;}edge[N<<1];
int n,u,v,x,m;
int cnt=0;
int id=0;
void pushup(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void Insert(int u,int v)
{
    edge[cnt].to=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void dfs(int x,int fa)
{
    q[++id]=x;
    st[x]=id;
    for(int i=head[x];i!=-1;i=edge[i].next)
    {
        if(edge[i].to!=fa)
            dfs(edge[i].to,x);
    }
    ed[x]=id;
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]=1;
        return ;
    }
    int mid=(r+l)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    pushup(rt);
}
int query(int l,int r,int ll,int rr,int rt)
{
   if(ll<=l&&rr>=r) return sum[rt];
   int mid=(r+l)>>1;
    if(rr<=mid) return query(l,mid,ll,rr,rt<<1);
    else if(ll>mid) return query(mid+1,r,ll,rr,rt<<1|1);
    else return query(l,mid,ll,rr,rt<<1)+query(mid+1,r,ll,rr,rt<<1|1);

}
void update(int l,int r,int pos,int rt)
{
    if(l==r)
    {
        sum[rt]^=1;
        return ;
    }
    int mid=(l+r)>>1;
    if(pos<=mid) update(l,mid,pos,rt<<1);
    else update(mid+1,r,pos,rt<<1|1);
    pushup(rt);
}
int main()
{
    memset(head,-1,sizeof(head));
    scanf("%d",&n);
    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&u,&v);
        Insert(u,v);
    }
    dfs(1,0);
    build(1,n,1);
    scanf("%d",&m);
    char ch[10];
    for(int i=1;i<=m;i++)
    {
        scanf("%s",ch);
        scanf("%d",&x);
        if(ch[0]=='Q')
            printf("%d\n",query(1,n,st[x],ed[x],1));
        else
            update(1,n,st[x],1);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值