Apple Tree————POJ—3321

题目:

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

 

题意:

有一棵树,上面有n个节点(根节点为1),初始时每一个节点上有一个苹果,

有n-1条树枝,每个树枝连接两个节点,有两种操作类型:

C x 表示,如果x节点有苹果的话Kaka会把它摘下来,如果没有苹果的话,会长出来一个。

Q x 表示,要输出x以及其子树上的苹果数量。

 

思路:

利用dfs对树进行一次遍历,记录每个节点被遍历到的时间st,以及其子树遍历完的时间en;

再利用树状数组进行操作和查询;

 

#include<stdio.h>
#include<string.h>
#define ll long long
#define N 100861
#define inf 0x3f3f3f3f
struct node
{
    int st,en;
}e[N];
int first[N],next[N],y,tree[N];
int u[N],v[N],book[N],c[N];
int dfs(int x)
{
    book[x]=1;
    e[x].st=++y;//记录搜索到当前节点的时间戳
    for(int i=first[x];i!=-1;i=next[i])
    {
        if(!book[v[i]])
            dfs(v[i]);
    }
    e[x].en=y;//记录遍历完子树时的时间戳
}
int lowbit(int x)
{
    return x&(-x);
}
void update(int x,int v)
{
    for(int i=x;i<100861;i=i+lowbit(i))
        c[i]+=v;
}
int getsum(int x)
{
    int sum=0;
    for(int i=x;i>0;i=i-lowbit(i))
        sum=sum+c[i];
    return sum;
}
int main()
{
    int n;
    y=0;
    scanf("%d",&n);
    memset(first,-1,sizeof(first));
    memset(book,0,sizeof(book));
    memset(c,0,sizeof(c));
    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&u[i],&v[i]);
        next[i]=first[u[i]];
        first[u[i]]=i;
    }
    dfs(1);
    for(int i=1;i<=n;i++)//记录每个节点有没有苹果
        tree[i]=1;
    for(int i=1;i<=n;i++)//初始化
        update(e[i].st,1);
    int m;
    scanf("%d",&m);
    while(m--)
    {
        char c;
        int s;
        getchar();
        scanf("%c %d",&c,&s);
        if(c=='Q')
        {
            printf("%d\n",getsum(e[s].en)-getsum(e[s].st-1));
        }
        if(c=='C')
        {
            if(tree[s])
            {
                tree[s]=0;
                update(e[s].st,-1);
                /*
                    注意修改时是对节点重新编号后节点的修改
                    并不是直接对s节点update(s,-1);
                */
            }
            else
            {
                tree[s]=1;
                update(e[s].st,1);//同上
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值