POJ 3321 Apple Tree

Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 16817 Accepted: 5090

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

 

【题目大意】:

          给出一棵树,判断以某个节点为根的子树有多少个节点有苹果。。

【分析】:

           首先对一棵树进行深搜,然后将深搜的顺序重新标上号<程序中存在了high元素中,此值也相当于是后序遍历>,记下每一个点即将进入扫描儿子及子树时的值<程序中存在了low中,此值也相当于对每个树枝编了下号,详见图1>,然后就可以用树状数组了。当要询问时,只需求getsum(high)-getsum(low-1),即该点子树结点数<包含该点>,若要执行C操作,那么分情况:用exist数组记录该点是否有苹果,若当前点有苹果,则用树状数组的add操作删掉该节点以下的苹果,若没有,则反过来,加上苹果

【代码】:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
#define MAX 200001
struct EDGE{int t,next;};
struct POINT{int low,high;};
EDGE a[MAX];
POINT b[MAX];
int N,M,last[MAX],index=1,tot=0,S[MAX];
bool exist[MAX];
int lowbit(int x){return x&(-x);}
void add(int from,int to)
{
      a[++tot].t=to;
      a[tot].next=last[from];
      last[from]=tot;
}
void addx(int x,int v)
{
      while(x<=N)
      {
            S[x]+=v;
            x+=lowbit(x);
      }
}
int getsum(int x)
{
      int cnt=0;
      while(x>0)
      {
            cnt+=S[x];
            x-=lowbit(x);
      }
      return cnt;
}
void DFS(int x)
{
      b[x].low=index;
      if(!last[x])
      {
            b[x].high=index++;
            return;
      }
      for(int i=last[x];i;i=a[i].next)
            DFS(a[i].t);
      b[x].high=index++;
}
void pre()
{
      DFS(1);
      for(int i=1;i<=N;i++)
            S[i]=lowbit(i);
}
int main()
{
      //freopen("input.in","r",stdin);
	  //freopen("output.out","w",stdout); 
	  memset(exist,true,sizeof(exist));
	  scanf("%d",&N);
	  for(int i=1;i<N;i++)
	  {
            int A,B;
            scanf("%d%d",&A,&B);
            add(A,B);
      }      
      pre();
      scanf("%d",&M);
      for(int i=1;i<=M;i++)
      {
            char ch;
            int num;
            scanf("\n%c%d",&ch,&num);
            if(ch=='Q')
                  printf("%d\n",getsum(b[num].high)-getsum(b[num].low-1));
            else if(ch=='C')
            {
                  int sign;
                  if(exist[num])
                        sign=-1;
                  else  sign=1;
                  exist[num]=!exist[num];
                  addx(b[num].high,sign);
            }
      }
	  //system("pause");
      return 0;
}


 

转载注明出处: http://blog.csdn.net/u011400953

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值