标签:marked ancestor aoj 2170 并查集
You are given a tree T that consists of N nodes. Each node is numbered from 1 to N, and node 1 is always the root node of T. Consider the following two operations on T:
Your job is to write a program that performs a sequence of these operations on a given tree and calculates the value that each Q operation will print. To avoid too large output file, your program is requested to print the sum of the outputs of all query operations. Note that the judges confirmed that it is possible to calculate every output of query operations in a given sequence.
The input consists of multiple datasets. Each dataset has the following format:
The first line of the input contains two integers N and Q, which denotes the number of nodes in the tree Tand the number of operations, respectively. These numbers meet the following conditions: 1 ≤ N ≤ 100000 and 1 ≤ Q ≤ 100000.
The following N - 1 lines describe the configuration of the tree T. Each line contains a single integer pi (i = 2, ... , N), which represents the index of the parent of i-th node.
The next Q lines contain operations in order. Each operation is formatted as "M v" or "Q v", where v is the index of a node.
The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed.
For each dataset, print the sum of the outputs of all query operations in one line.
6 3 1 1 2 3 3 Q 5 M 3 Q 5 0 0
4
#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 rson 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, sizeof(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 namespace 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 并查集)
标签:marked ancestor aoj 2170 并查集
原文地址:http://blog.csdn.net/u014422052/article/details/44517239