poj_3321.Apple Tree(DFS序+线段树)

Apple Tree
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

POJ Monthly–2007.08.05, Huang, Jinsong

关于DFS序,因为有时候树形结构的遍历相比线性结构要麻烦一些,DFS序的目的就是把在树上进行的节点修改,子树求和等操作转化成对于线性结构的区间操作,从而用线段树或者是树状数组来解决。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e5+7;
const int maxn2= 6010;
const int inf =0x3f3f3f3f;
int head[maxn],cnt;
int have[maxn],in[maxn],out[maxn],res=0;
struct node{
    int y,p;
}w[maxn];
struct node1{
    int sum;
}s[maxn<<4];
void add(int a,int b){
    w[++cnt]={b,head[a]};
    head[a]=cnt;
}
void dfs(int n){
    in[n]=++res;
    for(int i=head[n];~i;i=w[i].p){
        dfs(w[i].y);
    }
    out[n]=res;
}
void update(int ro){
    s[ro].sum=s[ro<<1].sum+s[ro<<1|1].sum;
}
void build_tree(int ro,int l,int r){
    if(l==r){
        s[ro].sum=1; return;
    }
    int mid=(l+r)>>1;
    build_tree(ro<<1,l,mid);
    build_tree(ro<<1|1,mid+1,r);
    update(ro);
}
int query(int ro,int l,int r,int nl,int nr){
    if(l==nl&&r==nr){
        return s[ro].sum;
    }
    int mid=(l+r)>>1;
    if(nr<=mid) return query(ro<<1,l,mid,nl,nr);
    else if(nl>mid) return query(ro<<1|1,mid+1,r,nl,nr);
    else return query(ro<<1,l,mid,nl,mid)+query(ro<<1|1,mid+1,r,mid+1,nr);
}
void change(int ro,int l,int r,int pos,int val){
    if(l==r&&r==pos){
        s[ro].sum+=val;
        return ;
    }
    int mid=(l+r)>>1;
    if(mid<pos) change(ro<<1|1,mid+1,r,pos,val);
    else change(ro<<1,l,mid,pos,val);
    update(ro);
}
int main()
{
    int n,m;
    while(~scanf("%d",&n)){
        memset(head,-1,sizeof(head));cnt=0;res=0;
        for(int i=1;i<n;i++){
            int a,b;scanf("%d%d",&a,&b);
            add(a,b);
        }
        for(int i=1;i<=n;i++) have[i]=1;
        dfs(1);build_tree(1,1,n);
        scanf("%d",&m);
        while(m--){
            int a;char b;
            scanf(" %c%d",&b,&a);
            if(b=='Q'){
                int ans=query(1,1,n,in[a],out[a]);
                printf("%d\n",ans);
            }else{
                if(have[a]){
                    change(1,1,n,in[a],-1);
                }else{
                    change(1,1,n,in[a],1);
                }
                have[a]^=1;
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值