stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
/* 

 author:hdsdogge 

 begin: 

 end: 

 cost: 

 */

#include<iostream>

#include<string>

#include<queue>

#include<map>

#include<stack>

#include<algorithm>

#include<cmath>

#include<cstdio>

#include<cstring>

#include<vector>

#include<bitset>

#include<cstdlib>

#include<list>

#include <sstream>

#include<ctype.h>

using namespace std;

const int maxn=1000000+10;

typedef pair<int,int> P;

typedef long long ll;

const double PI = acos(-1.0);

const double eps = 1e-6;

const int INF = 1000000000;

int T,n,m;

int par[maxn];

int vis[maxn];

int main() {

   // freopen("test","r",stdin);

   // freopen("out","w",stdout);

    std::ios::sync_with_stdio(false);

    std::cin.tie(0);

    while(cin>>n>>m&&(n+m)){

        memset(vis,0, sizeof(vis));

        par[1]=1;

        vis[1]=1;

        ll ans=0;

        for(int i=2;i<=n;i++){

            int x;

            cin>>x;

            par[i]=x;

        }

        for(int i=0;i<m;i++){

            char c;

            int x;

            cin>>c>>x;

            if(c=='Q'){

                while(vis[x]==0){

                    x=par[x];

                }

                ans+=x;

            }

            else if(c=='M'){

               vis[x]=1;

            }

        }

        cout<<ans<<endl;

    }

    return 0;

} 






// Faster C++ solution for the same problem (measured speedup: 5.0685341946452125x):
#include <bits/stdc++.h>

using namespace std;



typedef long long ll;

#define maxn 100005

ll fa[maxn];

int n,q;



ll find_fa(ll x){

	if(x==fa[x]){

		return fa[x];

	}

	return find_fa(fa[x]);

}



int main(){

	while(cin>>n>>q){

		if(!n&&!q) break;

		char s[2];

		ll x;

		fa[1] = 1;

		for(int i=2;i<=n;i++){

			cin>>x;

			fa[i] = x;

		}

		ll ans = 0;

		while(q--){

			cin>>s>>x;

			if(s[0]=='M'){

				fa[x] = x;

			}

			else{

				ans += find_fa(x);

			}

			

		}

		cout<<ans<<endl;

	}

	return 0;

}
// Slower C++ solution:
/*

 ━━━━━?

 ┓┏┓┏┓┃μ'sic foever!!

 ┛┗┛┗┛┃＼○／

 ┓┏┓┏┓┃ /

 ┛┗┛┗┛┃ノ)

 ┓┏┓┏┓┃

 ┛┗┛┗┛┃

 ┓┏┓┏┓┃

 ┛┗┛┗┛┃

 ┓┏┓┏┓┃

 ┛┗┛┗┛┃

 ┓┏┓┏┓┃

 ┃┃┃┃┃┃

 ┻┻┻┻┻┻

 */

#include <iostream>

#include <cstdio>

#include <cstring>

#include <algorithm>

#include <cmath>

#include <map>

#include <stack>

#include <vector>

using namespace std;

const int maxn=1e5+10;

bool vis[maxn];

int pre[maxn];

long long res;

bool flag;

int find(int x){

    int r = x;

    while(pre[r]!=r){

        if(!flag&&vis[pre[r]]){

            res += pre[r];

            flag = true;

            return -1;

        }

        r = pre[r];

    }

    return r;

}

void join(int a,int b){

    pre[b]=a;

}

int main(){

    int n,q,i,j,a,b;

    char op;

    while(~scanf("%d%d",&n,&q)&&n&&q){

        res = 0;

        memset(vis, false, sizeof(vis));

        for(i=1;i<=n;i++){

            pre[i] = i;

        }

        for(i=2;i<=n;i++){

            scanf("%d",&a);

            join(a,i);

        }

        getchar();

        for(i=1;i<=q;i++){

            scanf("%c %d",&op,&a);

            if(op=='Q'){

                if(!vis[a]){

                    flag = false;

                    if(find(a)==-1){

                        

                    }else{

                        res += 1;

                    }

                }else{

                    res += a;

                }

            }else{

                vis[a] = true;

            }

            getchar();

        }

        printf("%lld\n",res);

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1090951155697675x):
#include<cstdio>

#include<iostream>

#include<algorithm>

#include<cmath>



using namespace std;

const int maxn = 5e5+5;

int n,m;

int far[maxn];

int Rank[maxn];

int find(int x){

	if(far[x] == x)return x;

	else return find(far[x]);

}

void unite(int x,int y){

	x = find(x);

	y = find(y);

	if(x == y) return;

	

	if(Rank[x] < Rank[y]) far[x] = y;

	else

	{

		far[y] = x;

		if(Rank[x] == Rank[y]) Rank[x]++;

	}

}

bool check(int x,int y){

	return find(x) == find(y);

}

void init(int n){

	for(int i = 0;i <= n;i++)

	{

		far[i] = i;

		//Rank[i] = 0;

	}

}



int main()

{

	while(~scanf("%d %d",&n,&m) &&(m||n))

	{

		init(n);

		int a;

		long long ans = 0;

		for(int i = 2;i <= n;i++)

		{

			scanf("%d",&far[i]);

		}

		char c[3];

		for(int i = 0;i <m;i++)

		{

			scanf("%s %d",c,&a);

			if(c[0] == 'M') far[a] = a;

			else  

			{

				ans += find(a);

			}

		}

		printf("%lld\n",ans);

	}

	

	return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<cstring>

#include<vector>

using namespace std;

#define MAXN 100005

int Set[MAXN];

int Root(int x)

{return Set[x]==x?x:Set[x]=Root(Set[x]);}

int N,Q;

int fa[MAXN];

int Que[MAXN][2];

int marked[MAXN];

int main()

{

	char op[3];

	int id;

	long long ans;

	while(~scanf("%d%d",&N,&Q)&&N&&Q)

	{

		memset(Que,0,sizeof Que);

		memset(fa,0,sizeof fa);

		memset(Set,0,sizeof Set);

		memset(marked,0,sizeof marked);

		ans=0;

		Set[1]=1;fa[1]=1;

		for(int i=2;i<=N;i++)

		{

			scanf("%d",fa+i);

			Set[i]=fa[i];

		}

		for(int i=1;i<=Q;i++)

		{

			scanf("%s%d",op,&id);

			if(op[0]=='Q')Que[i][0]=1;

			else {Que[i][0]=2;Set[id]=id;marked[id]++;}

			Que[i][1]=id;

		}

		for(int i=Q;i>=1;i--)

		{

			if(Que[i][0]==1)

				ans+=1LL*Root(Que[i][1]);

			else

			{

				marked[Que[i][1]]--;

				if(!marked[Que[i][1]])

					Set[Que[i][1]]=fa[Que[i][1]];

			}

		}

		printf("%lld\n",ans);

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.214796604654646x):
#include<cstdio>

#include<iostream>

#include<algorithm>

#include<cmath>



using namespace std;

const int maxn = 5e5+5;

int n,m;

int far[maxn];

int Rank[maxn];

int find(int x){

	if(far[x] == x)return x;

	else return find(far[x]);

}

void unite(int x,int y){

	x = find(x);

	y = find(y);

	if(x == y) return;

	

	if(Rank[x] < Rank[y]) far[x] = y;

	else

	{

		far[y] = x;

		if(Rank[x] == Rank[y]) Rank[x]++;

	}

}

bool check(int x,int y){

	return find(x) == find(y);

}

void init(int n){

	for(int i = 0;i <= n;i++)

	{

		far[i] = i;

		//Rank[i] = 0;

	}

}



int main()

{

	while(~scanf("%d %d",&n,&m) &&(m||n))

	{

		init(n);

		int a;

		long long ans = 0;

		for(int i = 2;i <= n;i++)

		{

			scanf("%d",&far[i]);

		}

		char c[3];

		for(int i = 0;i <m;i++)

		{

			scanf("%s %d",c,&a);

			if(c[0] == 'M') far[a] = a;

			else  

			{

				ans += find(a);

			}

		}

		printf("%lld\n",ans);

	}

	

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <string.h>

#include <vector>

#include <queue>

#include <functional>

#include <algorithm>

using namespace std;



const int MAXN = 100010;

const int MAXM = 100010;

const int INF = 0x3f3f3f3f;

typedef long long LL;

typedef pair<int, int> P;



int N, Q;

int father[MAXN];

bool vis[MAXN];



void Init() {

	for (int i = 0; i < MAXN; ++i) {

		father[i] = i;

	}

}

int Find(int x) {

	while (!vis[x])

		x = father[x];

	return x;

}



int main(int argc, char const *argv[]) {

#ifdef LOCAL

	freopen("input.txt", "r", stdin);

	freopen("output.txt", "w", stdout);

#endif

	while (scanf("%d%d", &N, &Q) != EOF && !(N == 0 && Q == 0)) {

		int a, b;

		LL res = 0;

		char opt[5];

		Init();

		fill(vis, vis + MAXN, false);

		vis[1] = true;

		for (int i = 2; i <= N; ++i) {

			scanf("%d", &father[i]);

		}

		for (int i = 0; i < Q; ++i) {

			scanf("%s %d", opt, &b);

			if (opt[0] == 'Q') {

				res += Find(b);

			}

			else

				vis[b] = true;

		}

		printf("%lld\n", res);

	}

	//system("pause");

	return 0;

}




// Faster C++ solution for the same problem (measured speedup: 1.505011946140832x):
#include<cstdio>

#include<iostream>

#include<algorithm>

#include<cmath>



using namespace std;

const int maxn = 5e5+5;

int n,m;

int far[maxn];

int Rank[maxn];

int find(int x){

	if(far[x] == x)return x;

	else return find(far[x]);

}

void unite(int x,int y){

	x = find(x);

	y = find(y);

	if(x == y) return;

	

	if(Rank[x] < Rank[y]) far[x] = y;

	else

	{

		far[y] = x;

		if(Rank[x] == Rank[y]) Rank[x]++;

	}

}

bool check(int x,int y){

	return find(x) == find(y);

}

void init(int n){

	for(int i = 0;i <= n;i++)

	{

		far[i] = i;

		//Rank[i] = 0;

	}

}



int main()

{

	while(~scanf("%d %d",&n,&m) &&(m||n))

	{

		init(n);

		int a;

		long long ans = 0;

		for(int i = 2;i <= n;i++)

		{

			scanf("%d",&far[i]);

		}

		char c[3];

		for(int i = 0;i <m;i++)

		{

			scanf("%s %d",c,&a);

			if(c[0] == 'M') far[a] = a;

			else  

			{

				ans += find(a);

			}

		}

		printf("%lld\n",ans);

	}

	

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <string.h>

#include <vector>

#include <queue>

#include <functional>

#include <algorithm>

using namespace std;



const int MAXN = 100010;

const int MAXM = 100010;

const int INF = 0x3f3f3f3f;

typedef long long LL;

typedef pair<int, int> P;



int N, Q;

int father[MAXN];

bool vis[MAXN];



void Init() {

	for (int i = 0; i < MAXN; ++i) {

		father[i] = i;

	}

}

int Find(int x) {

	if (father[x] == x)

		return x;

	return Find(father[x]);

}



int main(int argc, char const *argv[]) {

#ifdef LOCAL

	freopen("input.txt", "r", stdin);

	freopen("output.txt", "w", stdout);

#endif

	while (scanf("%d%d", &N, &Q) != EOF && !(N == 0 && Q == 0)) {

		int a, b;

		LL res = 0;

		char opt[5];

		Init();

		for (int i = 2; i <= N; ++i) {

			scanf("%d", &father[i]);

		}

		for (int i = 0; i < Q; ++i) {

			scanf("%s %d", opt, &b);

			if (opt[0] == 'Q') {

				res += Find(b);

			}

			else

				father[b] = b;

		}

		printf("%lld\n", res);

	}

	//system("pause");

	return 0;

}




// Faster C++ solution for the same problem (measured speedup: 1.4039043696044387x):
#include<cstdio>

#include<iostream>

#include<algorithm>

#include<cmath>



using namespace std;

const int maxn = 5e5+5;

int n,m;

int far[maxn];

int Rank[maxn];

int find(int x){

	if(far[x] == x)return x;

	else return find(far[x]);

}

void unite(int x,int y){

	x = find(x);

	y = find(y);

	if(x == y) return;

	

	if(Rank[x] < Rank[y]) far[x] = y;

	else

	{

		far[y] = x;

		if(Rank[x] == Rank[y]) Rank[x]++;

	}

}

bool check(int x,int y){

	return find(x) == find(y);

}

void init(int n){

	for(int i = 0;i <= n;i++)

	{

		far[i] = i;

		//Rank[i] = 0;

	}

}



int main()

{

	while(~scanf("%d %d",&n,&m) &&(m||n))

	{

		init(n);

		int a;

		long long ans = 0;

		for(int i = 2;i <= n;i++)

		{

			scanf("%d",&far[i]);

		}

		char c[3];

		for(int i = 0;i <m;i++)

		{

			scanf("%s %d",c,&a);

			if(c[0] == 'M') far[a] = a;

			else  

			{

				ans += find(a);

			}

		}

		printf("%lld\n",ans);

	}

	

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <string.h>

#include <vector>

#include <queue>

#include <stack>

#include <functional>

#include <algorithm>

using namespace std;



const int MAXN = 100010;

const int MAXM = 100010;

const int INF = 0x3f3f3f3f;

typedef long long LL;

typedef pair<char, int> P;



int N, Q;

int father[MAXN]; // 并查集中的父节点

int parent[MAXN]; // 树中的父节点

int vis[MAXN]; // 结点标记次数

vector<int> e[MAXN]; // 记录边，即树的结构e[i][j]表示i的第j+1孩子

stack<P> st; // 存储操作



void dfs(int i, int f = 1) {

	if (vis[i])

		f = i;

	father[i] = f;

	vector<int> &ei = e[i];

	for (vector<int>::iterator it = ei.begin(); it != ei.end(); ++it) {

		dfs(*it, f);

	}

}



//void Init() {

//	for (int i = 0; i < MAXN; ++i) {

//		father[i] = i;

//	}

//}

int Find(int x) {

	if (father[x] == x)

		return x;

	return father[x] = Find(father[x]);

}



int main(int argc, char const *argv[]) {

#ifdef LOCAL

	freopen("input.txt", "r", stdin);

	freopen("output.txt", "w", stdout);

#endif

	while (scanf("%d%d", &N, &Q) != EOF && !(N == 0 && Q == 0)) {

		int i, j, q;

		LL res = 0;

		char opt[5];

		for (i = 1; i <= N; ++i) {

			e[i].clear();

		}

		fill(vis, vis + MAXN, 0);

		vis[1] = 1;

		for (i = 2; i <= N; ++i) {

			scanf("%d", &j);

			e[j].push_back(i);// 结点j的孩子i,用于dfs

			parent[i] = j; // 结点i的父亲j

		}

		for (int i = 0; i < Q; ++i) {

			scanf("%s %d", opt, &j);

			if (opt[0] == 'M') {

				++vis[j];// 结点j标记次数

			}

			st.push(P(opt[0], j));// 将操作入栈，操作名和结点

		}

		dfs(1); // 寻找最近染色祖先

		// 逆序处理操作

		while (!st.empty()) {

			i = st.top().second; // 被操作结点

			if (st.top().first == 'M') { 

				if (--vis[i] == 0) // 直到多余标记结束

					father[i] = Find(parent[i]); // 取消染色

			} else {

				res += Find(i); // 查询操作，返回最近染色祖先

			}

			st.pop(); // 处理下一操作

		}

		printf("%lld\n", res);

	}

	//system("pause");

	return 0;

}




// Faster C++ solution for the same problem (measured speedup: 3.9749258675884733x):
#include<cstdio>

#include<iostream>

#include<algorithm>

#include<cmath>



using namespace std;

const int maxn = 5e5+5;

int n,m;

int far[maxn];

int Rank[maxn];

int find(int x){

	if(far[x] == x)return x;

	else return find(far[x]);

}

void unite(int x,int y){

	x = find(x);

	y = find(y);

	if(x == y) return;

	

	if(Rank[x] < Rank[y]) far[x] = y;

	else

	{

		far[y] = x;

		if(Rank[x] == Rank[y]) Rank[x]++;

	}

}

bool check(int x,int y){

	return find(x) == find(y);

}

void init(int n){

	for(int i = 0;i <= n;i++)

	{

		far[i] = i;

		//Rank[i] = 0;

	}

}



int main()

{

	while(~scanf("%d %d",&n,&m) &&(m||n))

	{

		init(n);

		int a;

		long long ans = 0;

		for(int i = 2;i <= n;i++)

		{

			scanf("%d",&far[i]);

		}

		char c[3];

		for(int i = 0;i <m;i++)

		{

			scanf("%s %d",c,&a);

			if(c[0] == 'M') far[a] = a;

			else  

			{

				ans += find(a);

			}

		}

		printf("%lld\n",ans);

	}

	

	return 0;

}
// Slower C++ solution:
/* 

 author:hdsdogge 

 begin: 

 end: 

 cost: 

 */

#include<iostream>

#include<string>

#include<queue>

#include<map>

#include<stack>

#include<algorithm>

#include<cmath>

#include<cstdio>

#include<cstring>

#include<vector>

#include<bitset>

#include<cstdlib>

#include<list>

#include <sstream>

#include<ctype.h>

using namespace std;

const int maxn=1000000+10;

typedef pair<int,int> P;

typedef long long ll;

const double PI = acos(-1.0);

const double eps = 1e-6;

const int INF = 1000000000;

int T,n,m;

int par[maxn];

int vis[maxn];

int main() {

   // freopen("test","r",stdin);

   // freopen("out","w",stdout);

    std::ios::sync_with_stdio(false);

    std::cin.tie(0);

    while(cin>>n>>m&&(n+m)){

        memset(vis,0, sizeof(vis));

        par[1]=1;

        vis[1]=1;

        ll ans=0;

        for(int i=2;i<=n;i++){

            int x;

            cin>>x;

            par[i]=x;

        }

        for(int i=0;i<m;i++){

            char c;

            int x;

            cin>>c>>x;

            if(c=='Q'){

                while(vis[x]==0){

                    x=par[x];

                }

                ans+=x;

            }

            else if(c=='M'){

               vis[x]=1;

            }

        }

        cout<<ans<<endl;

    }

    return 0;

} 






// Faster C++ solution for the same problem (measured speedup: 5.146046942058302x):
#include<cstdio>

#include<iostream>

#include<algorithm>

#include<cmath>



using namespace std;

const int maxn = 5e5+5;

int n,m;

int far[maxn];

int Rank[maxn];

int find(int x){

	if(far[x] == x)return x;

	else return find(far[x]);

}

void unite(int x,int y){

	x = find(x);

	y = find(y);

	if(x == y) return;

	

	if(Rank[x] < Rank[y]) far[x] = y;

	else

	{

		far[y] = x;

		if(Rank[x] == Rank[y]) Rank[x]++;

	}

}

bool check(int x,int y){

	return find(x) == find(y);

}

void init(int n){

	for(int i = 0;i <= n;i++)

	{

		far[i] = i;

		//Rank[i] = 0;

	}

}



int main()

{

	while(~scanf("%d %d",&n,&m) &&(m||n))

	{

		init(n);

		int a;

		long long ans = 0;

		for(int i = 2;i <= n;i++)

		{

			scanf("%d",&far[i]);

		}

		char c[3];

		for(int i = 0;i <m;i++)

		{

			scanf("%s %d",c,&a);

			if(c[0] == 'M') far[a] = a;

			else  

			{

				ans += find(a);

			}

		}

		printf("%lld\n",ans);

	}

	

	return 0;

}
// Slower C++ solution:
/*

 ━━━━━?

 ┓┏┓┏┓┃μ'sic foever!!

 ┛┗┛┗┛┃＼○／

 ┓┏┓┏┓┃ /

 ┛┗┛┗┛┃ノ)

 ┓┏┓┏┓┃

 ┛┗┛┗┛┃

 ┓┏┓┏┓┃

 ┛┗┛┗┛┃

 ┓┏┓┏┓┃

 ┛┗┛┗┛┃

 ┓┏┓┏┓┃

 ┃┃┃┃┃┃

 ┻┻┻┻┻┻

 */

#include <iostream>

#include <cstdio>

#include <cstring>

#include <algorithm>

#include <cmath>

#include <map>

#include <stack>

#include <vector>

using namespace std;

const int maxn=1e5+10;

bool vis[maxn];

int pre[maxn];

long long res;

bool flag;

int find(int x){

    int r = x;

    while(pre[r]!=r){

        if(!flag&&vis[pre[r]]){

            res += pre[r];

            flag = true;

            return -1;

        }

        r = pre[r];

    }

    return r;

}

void join(int a,int b){

    pre[b]=a;

}

int main(){

    int n,q,i,j,a,b;

    char op;

    while(~scanf("%d%d",&n,&q)&&n&&q){

        res = 0;

        memset(vis, false, sizeof(vis));

        for(i=1;i<=n;i++){

            pre[i] = i;

        }

        for(i=2;i<=n;i++){

            scanf("%d",&a);

            join(a,i);

        }

        getchar();

        for(i=1;i<=q;i++){

            scanf("%c %d",&op,&a);

            if(op=='Q'){

                if(!vis[a]){

                    flag = false;

                    if(find(a)==-1){

                        

                    }else{

                        res += 1;

                    }

                }else{

                    res += a;

                }

            }else{

                vis[a] = true;

            }

            getchar();

        }

        printf("%lld\n",res);

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1098071953749615x):
#include<bits/stdc++.h>

#define ll long long

using namespace std;



const int maxn=1e5+10;

int m[maxn],par[maxn];

int n,i,j,q,v;



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



int main(){

	scanf("%d%d",&n,&q);

	while (n!=0&&q!=0){

	  ll ans=0; par[1]=1;

	  for (i=2;i<=n;i++) scanf("%d",&par[i]);

	  for (i=1;i<=q;i++){

        char ch; getchar();

	    scanf("%c %d",&ch,&v);

	    if (ch=='M') par[v]=v; else ans+=find(v);

      }

      printf("%lld\n",ans);

      scanf("%d%d",&n,&q);

	}

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <algorithm>

#include <vector>

#include <queue>

#include <stack>

using namespace std;

 

//思路：并?集 + ? + ?列 （??取出方式） 



int parent[100016];

int height[100016];



//并?集 

void init(int n)

{

	for(int i = 1; i <= n; i++){

		parent[i] = i;

		height[i] = 0;

	}

}

 

int find(int x)

{

	if(parent[x] == x)  return x;

	else  return parent[x] = find(parent[x]);

}

 

//用来描述原始? 

vector<int> children[100016];

int parent_tree[100016];

bool marked[100016];

int ancestor[100016];

// ?条指令被拆分? 操作 + 目?

stack<char> operation;

stack<int>  target;

 

void bfs(int index, int the_ancestor)

{

	queue<int> q_index;

	queue<int> q_ancestor;

	q_index.push(index);

	q_ancestor.push(the_ancestor);

	while (!q_index.empty())

	{

		the_ancestor = q_ancestor.front(); 

		q_ancestor.pop();

		index = q_index.front(); 

		q_index.pop();

		if(marked[index] == true)  the_ancestor = index;  //如果?点自身被??，?它的染色祖先是自己，它的自己不??的儿子的最近染色祖先是它

		ancestor[index] = the_ancestor;  //??index的最近染色祖先 

		for(vector<int>::iterator it = children[index].begin(); it != children[index].end(); it++){  //将index的儿子加入?列 

			q_index.push(*it);

			q_ancestor.push(the_ancestor);

		}

	}

}

 

int main()

{

 

	int N, Q;

	while(scanf("%d %d", &N, &Q) && (N || Q)){

		for(int i = 1; i <= N; i++){

			children[i].clear();

			marked[i] = false;

		}

		marked[1] = true;

		parent_tree[1] = 1;

		int p;

		for(int i = 2; i <= N; i++){

			scanf("%d", &p);

			parent_tree[i] = p;

			children[p].push_back(i);

		}

		getchar(); 

		for(int i = 0; i < Q; i++){

			char ch;

			int t;

			scanf("%c %d", &ch, &t);

			getchar();

			if(ch == 'M'){

				if(marked[t])  continue;  //不?存重?的 "M v"，防止之后重?反染色 

				else  marked[t] = true;

			}

			operation.push(ch);

			target.push(t);

		}

		bfs(1, 1);

		init(N);

		for(int i = 1; i <= N; i++){

			parent[i] = ancestor[i];

		}

		unsigned long long result = 0;

		while(!operation.empty()){  //从后往前取出操作

			char ch = operation.top(); 

			operation.pop();

			int t = target.top(); 

			target.pop();

			if(ch == 'Q')  result += find(t);  //?目index从1?始

			else{  //??点t反染色（取消染色），t的祖先??其父?点的祖先

				parent[t] = find(parent_tree[t]);

			}

		}

		printf("%lld\n", result);

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.572463365732588x):
#include<bits/stdc++.h>

#define ll long long

using namespace std;



const int maxn=1e5+10;

int m[maxn],par[maxn];

int n,i,j,q,v;



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



int main(){

	scanf("%d%d",&n,&q);

	while (n!=0&&q!=0){

	  ll ans=0; par[1]=1;

	  for (i=2;i<=n;i++) scanf("%d",&par[i]);

	  for (i=1;i<=q;i++){

        char ch; getchar();

	    scanf("%c %d",&ch,&v);

	    if (ch=='M') par[v]=v; else ans+=find(v);

      }

      printf("%lld\n",ans);

      scanf("%d%d",&n,&q);

	}

	return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<cstring>

#include<vector>

using namespace std;

#define MAXN 100005

int Set[MAXN];

int Root(int x)

{return Set[x]==x?x:Set[x]=Root(Set[x]);}

int N,Q;

int fa[MAXN];

int Que[MAXN][2];

int marked[MAXN];

int main()

{

	char op[3];

	int id;

	long long ans;

	while(~scanf("%d%d",&N,&Q)&&N&&Q)

	{

		memset(Que,0,sizeof Que);

		memset(fa,0,sizeof fa);

		memset(Set,0,sizeof Set);

		memset(marked,0,sizeof marked);

		ans=0;

		Set[1]=1;fa[1]=1;

		for(int i=2;i<=N;i++)

		{

			scanf("%d",fa+i);

			Set[i]=fa[i];

		}

		for(int i=1;i<=Q;i++)

		{

			scanf("%s%d",op,&id);

			if(op[0]=='Q')Que[i][0]=1;

			else {Que[i][0]=2;Set[id]=id;marked[id]++;}

			Que[i][1]=id;

		}

		for(int i=Q;i>=1;i--)

		{

			if(Que[i][0]==1)

				ans+=1LL*Root(Que[i][1]);

			else

			{

				marked[Que[i][1]]--;

				if(!marked[Que[i][1]])

					Set[Que[i][1]]=fa[Que[i][1]];

			}

		}

		printf("%lld\n",ans);

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.2162185854321708x):
#include<bits/stdc++.h>

#define ll long long

using namespace std;



const int maxn=1e5+10;

int m[maxn],par[maxn];

int n,i,j,q,v;



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



int main(){

	scanf("%d%d",&n,&q);

	while (n!=0&&q!=0){

	  ll ans=0; par[1]=1;

	  for (i=2;i<=n;i++) scanf("%d",&par[i]);

	  for (i=1;i<=q;i++){

        char ch; getchar();

	    scanf("%c %d",&ch,&v);

	    if (ch=='M') par[v]=v; else ans+=find(v);

      }

      printf("%lld\n",ans);

      scanf("%d%d",&n,&q);

	}

	return 0;

}