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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章