POJ 3321 Apple Tree

来源:互联网 发布:淘宝技术这10年百度云 编辑:程序博客网 时间:2024/04/30 14:22
Apple Tree
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 19316 Accepted: 5874

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

31 21 33Q 1C 2Q 1

Sample Output

32

Source

POJ Monthly--2007.08.05, Huang, Jinsong

解题思路:DFS序+树状数组,简而言之就是将树状结构利用DFS编号成序列构成一个线性结构,利用树状数组来维护的过程

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <set>#include <map>#include <list>#include <queue>#include <stack>#include <deque>#include <vector>#include <bitset>#include <cmath>#include <utility>#define Maxn 100005#define Maxm 1000005#define lowbit(x) x&(-x)#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define PI acos(-1.0)#define make_pair MP#define LL long long #define Inf (1LL<<62)#define inf 0x3f3f3f3f#define re freopen("in.txt","r",stdin)#define wr freopen("out.txt","w",stdout)using namespace std;int head[Maxn],tree[Maxn],id[Maxn],st[Maxn],ed[Maxn],k,cnt;struct{    int e;    int next;}edge[Maxn<<1];void update(int pos,int n,int val){    int i;    for(i=pos;i<=n;i+=lowbit(i))        tree[i]+=val;}int sum(int pos){    int i,sum=0;    for(i=pos;i>0;i-=lowbit(i))        sum+=tree[i];    return sum;}void add(int s,int e){    edge[k].e=e;    edge[k].next=head[s];    head[s]=k++;    edge[k].e=s;    edge[k].next=head[e];    head[e]=k++;}void dfs(int s,int pre){    id[s]=++cnt;    st[s]=cnt;    for(int i=head[s];i!=-1;i=edge[i].next)    {        int e=edge[i].e;        if(e==pre)            continue;        dfs(e,s);    }    ed[s]=cnt;}int main(){    int i,n,m,a,b,apple[Maxn],q,s;    char op[3];    //re;wr;    while(~scanf("%d",&n))    {        k=cnt=0;        memset(head,-1,sizeof(head));        memset(tree,0,sizeof(tree));        for(i=1;i<n;i++)        {            scanf("%d%d",&a,&b);            add(a,b);        }        dfs(1,0);        for(i=1;i<=n;i++)        {            apple[i]=1;            update(id[i],n,1);        }        scanf("%d",&q);        while(q--)        {            scanf("%s%d",&op,&s);            if(op[0]=='C')            {                apple[s]^=1;                update(id[s],n,apple[s]?1:-1);            }            else                printf("%d\n",sum(ed[s])-sum(st[s]-1));        }    }    return 0;}


0 0
原创粉丝点击