码迷,mamicode.com
首页 > 移动开发 > 详细

poj 3321 Apple Tree

时间:2017-09-23 00:11:55      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:sts   开始   enc   miss   change   begin   his   query   accept   

Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 30869   Accepted: 9241

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

题目大意:n个节点的苹果树,一开始每个节点都有1个苹果,有两个操作Q x,询问以x为根的子树的苹果总和,
C x 为将x这个节点原本有苹果就摘掉,没苹果就生一个苹果。
题解:dfs序+树状数组
dfs序重要性质,一棵子树的所有节点在dfs序里是连续的一段,l[x]是x节点的dfs标号,如C X,修改的是l[x]
1A好开心~=u=
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 100002
using namespace std;

int n,m,sumedge,tim_node;
int head[maxn],l[maxn],r[maxn],tr[maxn],sum[maxn];

struct Edge{
    int x,y,nxt;
    Edge(int x=0,int y=0,int nxt=0):
        x(x),y(y),nxt(nxt){}
}edge[maxn<<1];

void add(int x,int y){
    edge[++sumedge]=Edge(x,y,head[x]);
    head[x]=sumedge;
}

void dfs(int x,int fa){
    l[x]=++tim_node;
    for(int i=head[x];i;i=edge[i].nxt){
        int v=edge[i].y;
        if(v==fa)continue;
        dfs(v,x);
    }
    r[x]=tim_node;
}

int lowbit(int x){
    return x&(-x);
}

void update(int x,int v){
    for(int i=x;i<=n;i+=lowbit(i))tr[i]+=v;
}

int query(int x){
    int w=0;
    for(int i=x;i;i-=lowbit(i))w+=tr[i];
    return w;
}

int main(){
    scanf("%d",&n);
    for(int i=1;i<n;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);add(y,x);
    }
    for(int i=1;i<=n;i++)update(i,1);
    dfs(1,-1);
    scanf("%d",&m);
    for(int i=1;i<=m;i++){
        char od[3];int x;
        cin>>od>>x;
        if(od[0]==Q){
            int ans=query(r[x])-query(l[x]-1);
            printf("%d\n",ans);
        }else{
            if(!sum[x]){
                sum[x]=1;
                update(l[x],-1);
            }else{
                sum[x]=0;
                update(l[x],1);
            }
        }
    }
    return 0;
}

 

 

poj 3321 Apple Tree

标签:sts   开始   enc   miss   change   begin   his   query   accept   

原文地址:http://www.cnblogs.com/zzyh/p/7577283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!