POJ 3321 Apple Tree (+dfs build)

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 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
Main Topic

There is an apple tree, there are n branches, the root is always numbered 1, in the initial case, each branch has an Apple, C operation represents the specified tree on the branch of the apple occurs from No to have, from there to no change, Q operation Representative check query including the current branch and the branches above the current total number of apples. Thinking of solving problems

Update the value of the node, ask interval and, can be solved by the tree array, the difficulty is how to transform the tree into a tree array, the tree into a one-dimensional array.

The idea of constructing a one-dimensional array is to first find the range of intervals involved in summing each node. That node and the node below (the tree I was painting backwards) total number of nodes, as shown in the figure, for a tree 8 (1 5) (1 3) (5 4) (5 2) (4 6) (4 7) (2 8), we can control the tree Numbering each node can be understood as the depth of the node (circled number in the figure), and then the sum of any node can be represented by a minimum depth (that is, the number of the node itself) and the maximum depth (the node number below the maximum node), as shown in Figure 5, which has a depth of 3, the following tree nodes include 2, 8, 4, 7, 6, the maximum number is 8, so the node 5 range is (3,8). The process is implemented through DFS.
The resulting values can then be maintained in a tree-like array

In the case of node 5, the interval of node 5 is [3,8], while maintenance is the idea of a tree array, and the sum of Node 5 is c[8]-c[2]. Code Implementation

#include <iostream> #include <cstdio> #include <cstring> using namespace std;
#define MAXN 100007 int s[maxn],e[maxn],f[maxn],c[maxn],n;    int mark=0;
Tag node depth bool VIS[MAXN],HAV[MAXN];
    struct Node {int v;
int pre;
} Edge[maxn*2]; 
    void Dfs (int u) {s[u]=++mark;
    Vis[u]=1;
    for (int i=f[u]; i; i=edge[i].pre) if (!VIS[EDGE[I].V]) DFS (EDGE[I].V);
E[u]=mark;
        } void Add (int j,int W) {while (j<=n) {c[j]+=w;
    j+= (J&AMP;-J);
    }} int sum (int j) {int ans=0;
        while (j>0) {ans+=c[j];
    j-= (J&AMP;-J);
} return ans;
    } int main () {int m,p;
    Char ch;
    memset (F,0,sizeof (f));
    memset (vis,0,sizeof (VIS));
    memset (hav,1,sizeof (hav));
    scanf ("%d", &n);
    int u,v;
    int k=1;
        for (int i=1; i<n; i++) {scanf ("%d%d", &u,&v);
        Edge[k].v=v;
        Edge[k].pre=f[u];
    f[u]=k++; } for (int i=1; i<=n; i++)//first on each branchThere is an Apple Add (i,1);  DFS (1);
    DFS build Tree array scanf ("%d%*c", &m);
        for (int i=1; i<=m; i++) {scanf ("%c%d%*c", &ch,&p);
        if (ch== ' Q ') printf ("%d\n", SUM (E[p])-sum (s[p]-1));
                else {if (hav[p])//There is an Apple on the branch, and the Apple is removed {add (s[p],-1);
            hav[p]=0;
                } else//There is no Apple on the branch and a new Apple {Add (s[p],1);
            Hav[p]=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.