poj3321(dfs序)

本文介绍了一种针对苹果树模型的数据结构与算法实现,该算法可以高效地处理苹果生长和采摘的情况,支持查询特定子树中苹果的数量,并能够快速响应苹果生长或被采摘的变化。

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

Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 29894 Accepted: 8905
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

#include <iostream>
#include <stdio.h>
#include <vector>
#include <string.h>
using namespace std;
const int N=123456;
int in[N],out[N];
vector< vector<int> > vec(N);
int s[N];
int vis[N];
int haspre[N];
int t,sum;
void init(int n){
    for(int i=1;i<=n;i++){
        vec[i].clear();
        s[i]=1;
    }
    t=1;
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    memset(vis,0,sizeof(vis));
    memset(haspre,0,sizeof(haspre));
}
void dfs(int u){
    in[u]=t;
    for(int i=0;i<vec[u].size();i++){
        int v=vec[u][i];
        if(!vis[v]){
            t++;
            vis[v]=1;
            dfs(v);
        }
    }
    out[u]=t;
}
struct node{
    int l,r;
    int num;
}a[4*N];
void build(int l,int r,int node){
    a[node].l=l;
    a[node].r=r;
    if(l==r){
        a[node].num=1;
        return;
    }
    int mid=(l+r)/2;
    build(l,mid,2*node);
    build(mid+1,r,2*node+1);
    a[node].num=a[node*2].num+a[node*2+1].num;
}
void update(int index,int node,int n){
    if(a[node].l==a[node].r && a[node].l==index){
        a[node].num+=n;
        return;
    }
    int mid=(a[node].l+a[node].r)/2;
    if(index<=mid){
        update(index,2*node,n);
    } else {
        update(index,2*node+1,n);
    }
    a[node].num=a[node*2].num+a[node*2+1].num;
}
void query(int l,int r,int node){
    if(a[node].l>=l&&a[node].r<=r){
        sum+=a[node].num;
        return;
    }
    int mid=(a[node].l+a[node].r)/2;
    if(r<=mid){
        query(l,r,2*node);
    } else if(l>mid){
        query(l,r,2*node+1);
    } else {
        query(l,mid,2*node);
        query(mid+1,r,2*node+1);
    }
}
int main(){
    int n;
    while(~scanf("%d",&n)){
        init(n);
        for(int i=0;i<n-1;i++){
            int u,v;
            scanf("%d %d",&u,&v);
            vec[u].push_back(v);
            haspre[v]=1;
        }
        int root;
        for(int i=1;i<=n;i++){
            if(!haspre[i]){
                root=i;
                break;
            }
        }
        build(1,n,1);
        vis[root]=1;
        dfs(root);
        int m;
        scanf("%d",&m);
        char str[12];
        int k;
        while(m--){
            sum=0;
            scanf("%s",str);
            scanf("%d",&k);
            if(str[0]=='Q'){
                int l=in[k];
                int r=out[k];
                query(l,r,1);
                printf("%d\n",sum);
            } else {
                if(s[k]){
                    update(in[k],1,-1);
                } else {
                    update(in[k],1,1);
                }
                s[k]=!s[k];
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值