Poj3321--apple Tree (multi-wishbone array)

Source: Internet
Author: User

Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 19425 Accepted: 5923

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 an 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 contain a message which is either
"C x "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 fork xIncluding 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

33Q 1C 2Q 1

Sample Output

32

Source

POJ monthly--2007.08.05, Huang, Jinsong topic:

Give you an apple tree, the trunk of the tree is set to 1, each branch is set to a number, until N, representing the apple tree. There can only be up to one Apple per branch, that is, there can be no more than two apples on top of a twig, and note not to think of the apple tree as a binary tree, each node of the apple tree may have many forks, and it should be a multi-pronged tree.

The input is the relationship between the forks,

1 2

1 3

Is the trunk above two forks are 2 and 3 respectively.

Here are two operations, Q and C

The meaning of C J is that if there is an Apple on the branch of J, it will be picked, if not, then a new one will grow.

Q J is asking J the total number of apples above the fork.

After storing the multi-fork tree, use DFS traversal to get a sequence in which each point controls a range and then solves the problem through a tree array such as: In this figure, by traversing 1 2 5 6 2 7 4 8 9 at each point as the root, you can find that the tree formed by it is close to it, Get a range such as: 1 of the range 1 to 9, 2 of the range 2 to 4 ..... This is the scope to be solved and the constant maintenance of this value can be
#include <cstdio> #include <cstring> #include <algorithm>using namespace std; #define MAXN 110000struct node{int Next, V;} p[maxn];int HEAD[MAXN]; int C[MAXN], num, TEMP[MAXN]; int S[MAXN], E[MAXN]; int l    AST[MAXN]; void addp (int u,int v,int i) {p[i].v = v;    P[i].next = Head[u]; Head[u] = i;}    void Dfs (int u) {int i;    LAST[U] = num;    s[Last[u]] = num++;    for (i = head[u]; i =-1; i = p[i].next) dfs (P[I].V);    e[Last[u]] = num-1; return;} int lowbit (int x) {return x & x;}        void Add (int i,int k,int N) {while (I <= N) {C[i] + = k;    i + = Lowbit (i);    }}int sum (int i) {int ans = 0;        while (i) {ans + = c[i];    I-= lowbit (i); } return ans;    int main () {int n, m, I, U, V, A;    Char str[10];    num = 1;    memset (head,-1,sizeof (head));    Memset (Last,-1,sizeof (last));    scanf ("%d", &n);        for (i = 1; I <= n; i++) {Add (i,1,n); TEmp[i] = 1;        } for (i = 1; i < n; i++) {scanf ("%d%d", &u, &v);    ADDP (U,v,i);    } dfs (1);    scanf ("%d", &m);        while (m--) {scanf ("%s%d", str, &a);        if (str[0] = = ' Q ') {printf ("%d\n", SUM (e[last[a])-sum (s[last[a]]-1));                } else {if (Temp[a]) {Temp[a] = 0;            Add (last[a],-1,n);                } else {Temp[a] = 1;            Add (last[a],1,n);    }}} printf ("\ n"); return 0;}

Poj3321--apple Tree (multi-wishbone 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.