Language: C++

//
//  AOJ 2170 Marked Ancestor
//
//  Created by TaoSama on 2015-03-16
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <vector>
#define CLR(x,y) memset(x, y, sizeof(x))

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;

int n, q, par[N], rank[N];

void init(int n) {
	for(int i = 1; i <= n; ++i) {
		par[i] = i;
		rank[i] = 0;
	}
}

int find(int x) {
	if(par[x] == x) return x;
	return par[x] = find(par[x]);
}

void unite(int x, int y) {
	x = find(x); y = find(y);
	if(x == y) return;
	if(rank[x] < rank[y]) par[x] = y;
	else {
		par[y] = x;
		if(rank[x] == rank[y]) ++rank[x];
	}
}

//蜴滓?
int father[N], ancestor[N];
vector<int> child[N];
bool marked[N];
stack<char> op;
stack<int> node;

void bfs(int s, int anc) {
	queue<pair<int, int> > q;
	q.push(make_pair(s, anc));
	while(!q.empty()) {
		int cur = q.front().first;
		anc = q.front().second; q.pop();
		if(marked[cur]) anc = cur;
		ancestor[cur] = anc;
		for(int i = 0; i < child[cur].size(); ++i)
			q.push(make_pair(child[cur][i], anc));
	}
}

int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
	ios_base::sync_with_stdio(0);

	while(~scanf("%d%d", &n, &q) && n && q) {
		for(int i = 1; i <= n; ++i) {
			marked[i] = false;
			child[i].clear();
		}
		marked[1] = true;
		for(int i = 2; i <= n; ++i) {
			int x; scanf("%d", &x);
			father[i] = x;
			child[x].push_back(i);
		}

		for(int i = 1; i <= q; ++i) {
			char c[2]; int x;
			scanf("%s%d", c, &x);
			if(c[0] == 'M') marked[x] = true;
			op.push(c[0]); node.push(x);
		}

		bfs(1, 1);

		init(n);
		for(int i = 1; i <= n; ++i)
			unite(i, ancestor[i]);

		long long ans = 0;
		while(!op.empty()){
			char o = op.top(); op.pop();
			int t = node.top(); node.pop();
			if(o == 'Q') ans += ancestor[find(t)];
			else{
				// 蜿肴?隶ー謫堺ス?				int p = ancestor[find(father[t])];
				unite(t, father[t]);
				ancestor[find(t)] = p;
			}
		}

		printf("%lld\n", ans);
	}
	return 0;
}

Compiler output:
a.cc: In function 'void init(int)':
a.cc:33:17: error: reference to 'rank' is ambiguous
   33 |                 rank[i] = 0;
      |                 ^~~~
In file included from /usr/include/c++/14/bits/stl_pair.h:60,
                 from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from a.cc:7:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:28:19: note:                 'int rank [100010]'
   28 | int n, q, par[N], rank[N];
      |                   ^~~~
a.cc: In function 'void unite(int, int)':
a.cc:45:12: error: reference to 'rank' is ambiguous
   45 |         if(rank[x] < rank[y]) par[x] = y;
      |            ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:28:19: note:                 'int rank [100010]'
   28 | int n, q, par[N], rank[N];
      |                   ^~~~
a.cc:45:22: error: reference to 'rank' is ambiguous
   45 |         if(rank[x] < rank[y]) par[x] = y;
      |                      ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:28:19: note:                 'int rank [100010]'
   28 | int n, q, par[N], rank[N];
      |                   ^~~~
a.cc:48:20: error: reference to 'rank' is ambiguous
   48 |                 if(rank[x] == rank[y]) ++rank[x];
      |                    ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:28:19: note:                 'int rank [100010]'
   28 | int n, q, par[N], rank[N];
      |                   ^~~~
a.cc:48:31: error: reference to 'rank' is ambiguous
   48 |                 if(rank[x] == rank[y]) ++rank[x];
      |                               ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:28:19: note:                 'int rank [100010]'
   28 | int n, q, par[N], rank[N];
      |                   ^~~~
a.cc:48:42: error: reference to 'rank' is ambiguous
   48 |                 if(rank[x] == rank[y]) ++rank[x];
      |                                          ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:28:19: note:                 'int rank [100010]'
   28 | int n, q, par[N], rank[N];
      |                   ^~~~
a.cc: In function 'int main()':
a.cc:112:53: error: 'p' was not declared in this scope
  112 |                                 ancestor[find(t)] = p;
      |                                                     ^