POJ 3321:apple trees (tree-like array, raising questions)

Source: Internet
Author: User

Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 18623 Accepted: 5629

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 the 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 be either
" x strong> "which means the 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
" x " which means an inquiry for the number of apples in the sub-tree above the F ork  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

32

Source

POJ monthly--2007.08.05, Huang, Jinsong

  tree-like array to improve the problem .

Some difficulty, to turn the bend.

  Test Instructions :

At first, I gave you a n-1, an apple tree, by default there are apples on every fork. Then there are Q operations, these operations are divided into two, or Apple picked away apples, no apples grow apple, or a fork on the number of apples.

  Ideas :

The key is to record a start time, and an end time at each fork, indicating the time at which the DFS went through the fork and back to the fork. The time here is actually the location of the DFS traversal order. This is equivalent to having an interval in which the number of all apples on the fork point can be calculated using a tree array.

  Note :

To your n-1 edge, is bidirectional, that is, a point to b,b also points to a, is a non-directed graph.

  test data (from POJ discussion edition):

Ten2 13 13 44 74 57 95 86 5Ten 6TenQ1C5Q3Q4C5Q6C6Q8C9Q1ans:Ten76218

  Code :

1#include <iostream>2#include <stdio.h>3 using namespacestd;4 5 #defineMAXN 1000106 7 intN;8 intCnt=0;9 intC[MAXN];Ten intSTART[MAXN]; One intEND[MAXN]; A  - structnode{ -     intnum; theNode* Next;//child node - Node () -     { -Next =NULL; +     } -}TREE[MAXN];//Critical Table +  A intLowbit (intx) at { -     returnX & (-x); - } -  - voidAddintDintx) - { in      while(d<=N) { -C[D] + =x; toD + =Lowbit (d); +     } - } the  * intSumintd) $ {Panax Notoginseng     intAns =0; -      while(d>=1){ theAns + =C[d]; +D-=Lowbit (d); A     } the     returnans; + } -  $ voidDfsintV//DFS traversal with R as the root node, returning the time after the entire traversal $ { -START[V] = + +CNT; -node* p =Tree[v].next; the      while(p) { -         if(start[p->num]==0)WuyiDFS (p->num); thep = p->Next; -     } WuEND[V] =CNT; - } About  $ voidAddedge (intAintb//Add a branch to the apple tree - {     -node* p =NewNode; -P->num =b; AP->next =Tree[a].next; +Tree[a].next =p; the } -  $ intMain () the {     the     inti,q; thescanf"%d",&N); the      for(i=1; i<n;i++){ -         intb; inscanf"%d%d",&a,&b); the Addedge (A, b); the Addedge (b,a); About     } theDfs1); the  the      for(i=1; i<=n;i++)//Initialize c[] +Add (I,1); -  thescanf"%d",&q);Bayi      while(q--) {//Q-time Operation the         Charcmd[Ten]; the         intD; -scanf"%s%d",cmd,&d); -         if(cmd[0]=='C'){ the             if(SUM (start[d])-sum (start[d]-1)==1) theAdd (start[d],-1); the             Else theAdd (Start[d],1); -         } the         Else if(cmd[0]=='Q'){ theprintf"%d\n", SUM (End[d])-sum (start[d]-1)); the         }94     } the     return 0; the}

freecode:www.cnblogs.com/yym2013

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.