POJ-3321 Apple Tree "DFS sequence + Tree array"

Source: Internet
Author: User

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 I n the tree.
The following N -1 lines each contain a 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 t He existence of the apple on Fork x has been changed. i.e. if there is a 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 Em>x , including the apple (if exists) on the fork x
Note The tree was 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

3

2

Ideas:
DFS sequence, each node is transformed into an interval, the tree structure is represented as a linear structure, and then maintained by a segment tree or a tree-like array.

Code:
1#include <iostream>2#include <algorithm>3#include <cstring>4#include <vector>5 using namespacestd;6 #defineINF 0x3f3f3f3f7 #defineM (A, B) memset (A, B, sizeof (a))8 Const intMAXN = 1e5 +5;9 intC[MAXN],inch[maxn<<2], out[maxn<<2], head[maxn<<1], N, id, CNT;Ten BOOLAP[MAXN]; One structEdge { A     intto, next; -}e[maxn<<1]; -  the voidInsertintUintv) { -e[++cnt].to=v;e[cnt].next=head[u];head[u]=CNT; -e[++cnt].to=u;e[cnt].next=head[v];head[v]=CNT; - } +  - voidDfsintXintFA) { +     inch[x] = + +ID; A      for(inti = head[x]; I i =E[i].next) { at         intv =e[i].to; -         if(v = = FA)Continue; - Dfs (v, x); -     } -      out[X] =ID; - } in  - intLowbit (intx) {returnX &-x;} to  + voidUpdateintXintd) { -      while(x <=N) { theC[X] + = D; X + =lowbit (x); *     } $ }Panax Notoginseng  - intSumintx) { the     intRET =0; +      while(x) { ARET + = c[x]; X-=lowbit (x); the     } +     returnret; - } $  $ intMain () { -Ios::sync_with_stdio (false); -      while(Cin >>N) { thememset (C,0,sizeof(C)); -         intA, B;Wuyi          for(inti =1; I <= N; ++i) { theAp[i] =1; -Update (I,1); Wu         } -          for(inti =1; I < n; ++i) { AboutCin >> a >>b; $ Insert (A, b); -         } -ID =0; -Dfs1,0); A         Charop[3]; +         intm, q; theCIN >>m; -          while(m--) { $CIN >>op; theCIN >>Q; the             if(op[0] =='C') { the                 if(Ap[q]) update (inch[Q],-1); the                 ElseUpdateinch[Q],1); -AP[Q] ^=1; in             } the             Else { thecout << sum ( out[Q])-SUM (inch[q]-1) <<Endl; About             } the         } the     } the  +     return 0; -}

POJ-3321 Apple Tree "DFS sequence + Tree array"

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.