"POJ 3321" Apple Tree (Dfs re-labeling interval + tree-like array summation)

Source: Internet
Author: User

"POJ 3321" Apple Tree (Dfs re-labeling interval + tree-like array summation)

Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 21966 Accepted: 6654

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
" c  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
" q  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

The main idea is a tree 1~n Apple two apples are connected by branches and guaranteed number 1 apples are root and satisfy the tree structure (a node has only one father)

Then there are two kinds of actions

Q x Ask number x on the subtree a few apples total

C x if x Apple is still picking it up (the number of apples on the subtree minus one) if it is removed, it will grow again (the number of apples on the subtree plus one)

Because only knowing that 1 is the root of the other indeterminate link order, then adjacency the table store diagram in order to implement the interval query Dfs re-marking so that the father is larger than the child number so the range of the father's jurisdiction is easy to express (Dfs is a continuous process each node pips all the son traversal to ensure the interval continuity)

So after the re-marking with a tree-like array of apples can be Q x-X-(X-Number of the last node number + 1) to check the value can this is the father than the son of a large number of reasons for the tree array accumulation from high to low so from large to small number to meet

The code is as follows:

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring>using namespace std; typedef struct edge{int v,next;} Edge; Edge eg[200200];int head[100100];//adjacency table map int f[100100],en[100100];//re-label interval end-to-end int tr[100100];//tree-like array bool vis[100100];// access token int n,tp,id;//n-node number tp-adjacency table pointer Id-dfs re-designator number int lowbite (int x) {return x& (-X);}        void Ad (int x,int data) {while (x <= N) {tr[x] + = data;    x + = Lowbite (x);    }}int Sum (int x) {int ans = 0;        while (x) {ans + = tr[x];    X-= Lowbite (x); } return ans;    void Add (int u,int v)//plus edge {eg[tp].v = v;    Eg[tp].next = Head[u]; Head[u] = tp++;}    void Dfs (int u) {Ad (id,1);    Vis[u] = 1;    F[u] = id--;    int i,v;        for (i = head[u]; I! =-1; i = eg[i].next) {v = eg[i].v;        if (Vis[v]) continue;    DFS (v);    } En[f[u]] = id;//u subtree After the completion of the ID is its interval the last +1}int main () {int m,i,x,u,v;    Char ch[3];    memset (head,-1,sizeof (head));    TP = 0; scanf ("%d", &amP;N);        for (i = 0; i < n-1; ++i) {scanf ("%d%d", &u,&v);        ADD (U,V);    ADD (V,u);    } id = n;//id Initial maximum number memset (vis,0,sizeof (VIS));    DFS (1);    scanf ("%d", &m);    memset (vis,0,sizeof (VIS));        while (m--) {scanf ("%s%d", ch,&x);                if (ch[0] = = ' C ') {if (vis[x])//Apple was removed from rebirth {Ad (f[x],1);            Vis[x] = 0;                } else//has Apple removed {Ad (f[x],-1);            VIS[X] = 1;    }} else printf ("%d\n", Sum (F[x])-sum (en[f[x])); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"POJ 3321" Apple Tree (Dfs re-labeling interval + tree-like array summation)

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.