Marked Ancestor (AOJ 2170 and check set)

Source: Internet
Author: User

Marked AncestorTime limit:8 sec, Memory limit:65536 KBproblem F:Marked Ancestor

you is given a tree t  that consists Of n  nodes. Each node was numbered from 1 To n , and Node 1 is always the root node Of t . Consider the following, operations On t :

  • M v: (Mark) Mark node v.
  • Q v: (Query) Print the index of the nearest marked ancestor of Node v which is nearest to it. Initially, only the root node is marked.

Your job is to write a program this performs a sequence of these operations on a given tree and calculates the value that Each Q operation would print. To avoid too large output file, your program was requested to print the sum of the outputs of all query operations. Note that the judges confirmed, that it's possible to calculate every output of query operations in a given sequence.

Input

The input consists of multiple datasets. Each dataset has the following format:

the first line of the input contains, Integers n  and q , which denotes the number of nodes in the Tree t and the number of operations, respectively. These numbers meet the following conditions:1≤ n  ≤100000 and 1≤ q  ≤100000.

The followingN-1 lines describe the tree of the configurationT. Each line contains a single integerpI (I= 2, ...,N), which represents the parent ofI-th node.

The next Q lines contain operations in order. Each operation are formatted as "M v" or "Q v", where v is the index of a node.

The last dataset was followed by a line containing the zeros. This was a part of the any dataset and should not being processed.

Output

For each dataset, print the sum of the outputs of all query operations on one line.

Sample Input
6 311233Q 5M 3Q 50 0
Output for the Sample Input
4
Test instructions: Give the tree containing n nodes, the initial time point 1 of the parent node is 1 and be marked, and then give the other N-1 node parent node, q times, M x indicates the node x mark, Q X is to find the nearest to the X of the Tagged parent node number, and finally output all the sum of the labels. Idea: The basis of the collection, but note here is: Look for the parent node do not compress the path. Also use a long long. Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 100005#define MAXN 2005# Define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define RSO  N rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FREE (i,a,b) for (i = A; I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A; i > b; i--) #define MEM (T, v) memset ((t), V, si Zeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n") typedef long Long ll;using Nam Espace std;ll father[maxn];int n,q;ll find_father (lL x) {if (x==father[x]) return father[x]; Return Find_father (Father[x]);}    int main () {int i,j;    ll X;    Char s[2];        while (SFF (n,q)) {if (n==0&&q==0) break;        Father[1]=1;            FRE (i,2,n) {SF (x);        Father[i]=x;        } ll Ans=0;            while (q--) {scanf ("%s%lld", s,&x);            if (s[0]== ' M ') father[x]=x;        else Ans+=find_father (x);    } PF ("%lld\n", ans); } return 0;}




Marked Ancestor (AOJ 2170 and check set)

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.