POJ3321 Apple Tree(DFS序+线段树)

给出一个多叉树,在任意点上删除或加入苹果,问某一结点的子树上的苹果数量的总数。

子树,用DFS序记下入和出的时间,线段树维护一下就好了。

vector会TLE。。。。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
const int N=1e5+5;
//vector<int>vec[N*4];

int has[N];
int vec[N][100];

int in[N*4],out[N*4];
int tot1;
int tree[N*4];

void build(int l,int r,int i)
{
    if(l==r)
    {
        tree[i]=1;
        return ;
    }
    int mid=(l+r)/2;
    build(l,mid,i*2);
    build(mid+1,r,i*2+1);
    tree[i]=tree[i*2]+tree[i*2+1];
}
void updata(int l,int r,int pos,int i)
{
    if(l==r&&pos==l)
    {
        tree[i]=!tree[i];
        return ;
    }
    int mid=(l+r)/2;
    if(pos<=mid)
        updata(l,mid,pos,i*2);
    else
        updata(mid+1,r,pos,i*2+1);
    tree[i]=tree[i*2]+tree[i*2+1];
}
int query(int l,int r,int L,int R,int i)
{
    if(l>=L&&r<=R)
        return tree[i];
    int mid=(l+r)/2;
    int ans=0;
    if(L<=mid)
        ans+=query(l,mid,L,R,i*2);
    if(R>mid)
        ans+=query(mid+1,r,L,R,i*2+1);
    return ans;
}
void dfs(int root)
{
    in[root]=++tot1;
    for(int i=1;i<=has[root];i++)
        dfs(vec[root][i]);
    /*
    for(int i=0;i<vec[root].size();i++)
        dfs(vec[root][i]);
    */
    out[root]=tot1;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        tot1=0;
        memset(has,0,sizeof(has));
        memset(vec,0,sizeof(vec));

        int a,b;
        for(int i=1;i<n;i++)
        {
            scanf("%d %d",&a,&b);
            vec[a][++has[a]]=b;

            //vec[a].push_back(b);
        }
        dfs(1);
        build(1,n,1);
        int q;
        scanf("%d",&q);
        while(q--)
        {
            char str;
            int x;
            scanf(" %c %d",&str,&x);
            if(str=='Q')
            {
                int ans=query(1,n,in[x],out[x],1);
                printf("%d\n",ans);
            }
            if(str=='C')
            {
                updata(1,n,in[x],1);
            }
        }
    }
    return 0;
}

Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 32579 Accepted: 9795

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值