(POJ3321) Apple Tree

Source: Internet
Author: User

Apple Tree
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 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 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, including 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

3
1 2
1 3
3
Q 1
C 2
Q 1
Sample Output

3
2
Source

POJ monthly–2007.08.05, Huang, Jinsong

Test Instructions:
A tree with n nodes, beginning with an apple on each node. Now gives the M-group dynamic operation: (C,i) is to take off the first node above the apple (if the Apple does not exist, then add an apple), (q,i) is to query the node I nodes as the root of the subtree has several apples (including the first node i).

Analysis:
A tree-like array. This topic focuses on how to establish tree-to-tree array mapping Relationship: The use of DFS traversal tree, the number of each node two times, the first time I found the node when the depth of DEP, for the node of the upper limit lowi, and then search the node sub-node, and finally search back the depth of DEP, For this node, the lower bound of the jurisdictional interval high[i], as shown in the following figure.

After doing this, we have given each node a number that is low[i]. And we know that the tree containing the root node of I is the node {Low[i],high[i]}, and it is a contiguous interval.
When we ask for the Apple number of the subtree with the I node as the root node, it is actually required node Low[i]~high[i].
The tree-like array can be handled very well for continuous interval and dynamic point modification.
Note: Each update is the value at which the node's number is processed, that is, Low[i]

AC Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <

Cmath> using namespace std;

const int MAXN = 100010;
    struct Edge {int u,v,next;
        Edge () {} edge (int u_,int v_) {u = u_;
    v = v_;

}}EDGES[2*MAXN];
int HEAD[MAXN],C[MAXN];

int e,n;
    void Addedge (int u,int v) {Edges[e] = Edge (u,v);
    Edges[e].next = Head[u];
Head[u] = e++;
    } int sum (int x) {int res = 0;
        while (x > 0) {res + = c[x];
    X-= (x& (x));
} return res;
        } void Add (int x,int d) {while (x <= N) {c[x] + = D;
    x + = (x& (-X));
}} int low[maxn],high[maxn],dep;

BOOL PICK[MAXN];
    void Dfs (int u) {low[u] = ++DEP;
    for (int i=head[u];i!=-1;i=edges[i].next) DFS (EDGES[I].V);
High[u] = DEP;
    } int main () {int m,u,v,x;
    Char ch;
        while (scanf ("%d", &n)!=eof) {memset (c,0,sizeof (c)); memset (pick,0,sizeof (pick));
        memset (head,-1,sizeof (head)); e = 0;
        DEP = 0;
            for (int i=0;i<n-1;i++) {scanf ("%d%d", &u,&v);
        Addedge (U,V);
        } dfs (1);
        for (int i=1;i<=n;i++) Add (i,1);
        scanf ("%d", &m);
        GetChar ();
            for (int i=0;i<m;i++) {scanf ("%c%d", &ch,&x);
            GetChar ();
                    if (ch = = ' C ') {if (Pick[x]) {Add (low[x],1);
                PICK[X] = false;
                    } else {Add (low[x],-1);
                Pick[x] = true;
            }} else {printf ("%d\n", SUM (High[x])-sum (low[x]-1));
}}} return 0;
 }

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.