程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 3321 Apple Tree

POJ 3321 Apple Tree

編輯:C++入門知識

Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15595   Accepted: 4639

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深搜重新編號,然後用樹狀數組
[cpp]
/***************************************************************
 > File Name: POJ3321.cpp
 > Author: SDUT_GYX
 > Mail: [email protected]
 > Created Time: 2013/6/5 10:30:04
 **************************************************************/ 
 
#include <iostream>  
#include <cstring>  
#include <algorithm>  
#include <cstdio>  
#include <queue>  
#include <cstdlib>  
#include <iomanip>  
#include <string>  
#include <vector>  
#include <map>  
#include <cmath>  
#include <stack>  
#define LL long long  
#define N 110000  
using namespace std; 
struct num  

    int end,next; 
}b[N*10]; 
int a[N],id[N],num[N],pt[N],Top,cou,n; 
bool visit[N]; 
char s1[10]; 
int main() 

    //freopen("data1.in","r",stdin);  
    void addeage(int x,int y); 
    void dfs(int k); 
    int get_sum(int x); 
    int update(int x,int val); 
    while(scanf("%d",&n)!=EOF) 
    { 
        memset(pt,-1,sizeof(pt)); 
        Top=0; 
        for(int i=1;i<=n-1;i++) 
        { 
            int x,y; 
            scanf("%d %d",&x,&y); 
            addeage(x,y); 
            addeage(y,x); 
        } 
        cou=0; 
        memset(visit,false,sizeof(visit)); 
        dfs(1); 
        int m,k; 
        scanf("%d",&m); 
        for(int i=1;i<=n;i++) 
        { 
            update(i,1); 
        } 
        while(m--) 
        { 
            scanf("%s %d",s1,&k); 
            int res; 
            if(s1[0]=='Q') 
            { 
                res=get_sum(id[k]+num[k]-1) - get_sum(id[k]-1); 
                printf("%d\n",res); 
            }else if(s1[0]=='C') 
            { 
                res = get_sum(id[k]) - get_sum(id[k]-1); 
                if(res==0) 
                { 
                    update(id[k],1); 
                }else 
                { 
                    update(id[k],-1); 
                } 
            } 
        } 
    } 
    return 0; 

void addeage(int x,int y) 

    b[Top].end= y; b[Top].next = pt[x]; 
    pt[x] = Top; Top++; 

void dfs(int k) 

    id[k] = ++cou; 
    int pre = cou; 
    visit[k] = true; 
    for(int i=pt[k]; i!=-1; i=b[i].next) 
    { 
        if(!visit[b[i].end]) 
        { 
             
          dfs(b[i].end); 
        } 
    } 
    num[k] = cou - pre + 1; 

int find(int x) 

    return x&(-x); 

int get_sum(int x) 

    int s=0; 
    while(x>0) 
    { 
        s+=a[x]; 
        x-=find(x); 
    } 
    return s; 

void update(int x,int val) 

    while(x<=n) 
    { 
        a[x] += val; 
        x += find(x); 
    } 

/***************************************************************
 > File Name: POJ3321.cpp
 > Author: SDUT_GYX
 > Mail: [email protected]
 > Created Time: 2013/6/5 10:30:04
 **************************************************************/

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <stack>
#define LL long long
#define N 110000
using namespace std;
struct num
{
 int end,next;
}b[N*10];
int a[N],id[N],num[N],pt[N],Top,cou,n;
bool visit[N];
char s1[10];
int main()
{
 //freopen("data1.in","r",stdin);
 void addeage(int x,int y);
 void dfs(int k);
 int get_sum(int x);
 int update(int x,int val);
 while(scanf("%d",&n)!=EOF)
 {
  memset(pt,-1,sizeof(pt));
  Top=0;
  for(int i=1;i<=n-1;i++)
  {
   int x,y;
   scanf("%d %d",&x,&y);
   addeage(x,y);
   addeage(y,x);
  }
  cou=0;
  memset(visit,false,sizeof(visit));
  dfs(1);
  int m,k;
  scanf("%d",&m);
  for(int i=1;i<=n;i++)
  {
   update(i,1);
  }
  while(m--)
  {
   scanf("%s %d",s1,&k);
   int res;
   if(s1[0]=='Q')
   {
    res=get_sum(id[k]+num[k]-1) - get_sum(id[k]-1);
    printf("%d\n",res);
   }else if(s1[0]=='C')
   {
    res = get_sum(id[k]) - get_sum(id[k]-1);
    if(res==0)
    {
     update(id[k],1);
    }else
    {
     update(id[k],-1);
    }
   }
  }
 }
 return 0;
}
void addeage(int x,int y)
{
 b[Top].end= y; b[Top].next = pt[x];
 pt[x] = Top; Top++;
}
void dfs(int k)
{
 id[k] = ++cou;
 int pre = cou;
 visit[k] = true;
 for(int i=pt[k]; i!=-1; i=b[i].next)
 {
  if(!visit[b[i].end])
  {
   
    dfs(b[i].end);
  }
 }
 num[k] = cou - pre + 1;
}
int find(int x)
{
 return x&(-x);
}
int get_sum(int x)
{
 int s=0;
 while(x>0)
 {
  s+=a[x];
  x-=find(x);
 }
 return s;
}
void update(int x,int val)
{
 while(x<=n)
 {
  a[x] += val;
  x += find(x);
 }
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved