POJ 3321 Apple Tree

Source: Internet
Author: User

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

Description

There is a apple tree outside of Kaka ' s house. Every autumn, a lot of apples would 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 is connected by branches. Kaka numbers the forks by 1 to N and the root are always numbered by 1. Apples'll grow on the forks and both Apple won ' t grow on the same fork. Kaka wants to know how many apples is there in a sub-tree, for his study of the produce ability of the apple tree.

The trouble is, a new Apple may grow in an empty fork some time and Kaka could pick an apple from the tree for his Desse Rt. Can you help Kaka?

Input

The first line contains a integer n (n ≤100,000), which is the number of the forks in the tree.
the following N -1 lines each contain double integers u and v, which means fork u an D Fork v is 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 have been changed. i.e. if there is a n Apple on the fork and 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, Inc. Luding the apple (if exists) on the fork x
Note The tree is full of apples at the beginning

Output

for every inquiry, the output of the correspond answer per line.

Sample Input

33Q 1C 2Q 1

Sample Output

32

The main idea :n nodes of the apple tree, at the beginning of each node has 1 apples, there are two operations Q X, asking for the X-root subtree of the apple sum,
C x for the X this node originally had an apple to take off, without an apple to give birth to an apple.
The following:DFS sequence + tree-like array
DFS order important properties, all nodes of a subtrees tree are contiguous in the DFS sequence, l[x] is the DFS designator of the X node, such as C x, modified by l[x]
1 A good happy ~=u=
Code:
#include <iostream>#include<cstdio>#include<cstring>#defineMAXN 100002using namespacestd;intN,m,sumedge,tim_node;intHEAD[MAXN],L[MAXN],R[MAXN],TR[MAXN],SUM[MAXN];structedge{intx,y,nxt; Edge (intx=0,inty=0,intnxt=0): X (x), Y (y), NXT (NXT) {}}EDGE[MAXN<<1];voidAddintXinty) {edge[++sumedge]=Edge (x,y,head[x]); HEAD[X]=Sumedge;}voidDfsintXintFA) {L[x]=++Tim_node;  for(intI=head[x];i;i=edge[i].nxt) {        intv=edge[i].y; if(V==FA)Continue;    DFS (V,X); } R[x]=Tim_node;}intLowbit (intx) {    returnx& (-x);}voidUpdateintXintv) {     for(intI=x;i<=n;i+=lowbit (i)) tr[i]+=v;}intQueryintx) {    intw=0;  for(intI=x;i;i-=lowbit (i)) w+=Tr[i]; returnW;}intMain () {scanf ("%d",&N);  for(intI=1; i<n;i++){        intx, y; scanf ("%d%d",&x,&y);    Add (x, y); add (y,x); }     for(intI=1; i<=n;i++) Update (I,1); DFS (1,-1); scanf ("%d",&m);  for(intI=1; i<=m;i++){        Charod[3];intx; CIN>>od>>x; if(od[0]=='Q'){            intAns=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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.