stablegradients/pie-gem5-pairs huggingface dataset:
// 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: 5.688035709434038x):
#include<cstdio>

#include<cstring>

#include<algorithm>



#define N 100010



int a[N];



long long Find(int x){

	return a[x]==x? a[x]:Find(a[x]);

}



int main(){

	while(true){

		int n,q;

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

		if(!(n||q))

			break;

		a[1]=1;

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

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

		long long ans=0ll;

		while(q--){

			char tmp[10]={};

			int x;

			scanf("%s%d",tmp,&x);

			if(*tmp=='Q')

				ans+=Find(x);

			else

				a[x]=x;

		}

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

	}

	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: 18.30975621676918x):
#include<cstdio>

#include<cstring>

#include<algorithm>



#define N 100010



int a[N];



long long Find(int x){

	return a[x]==x? a[x]:Find(a[x]);

}



int main(){

	while(true){

		int n,q;

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

		if(!(n||q))

			break;

		a[1]=1;

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

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

		long long ans=0ll;

		while(q--){

			char tmp[10]={};

			int x;

			scanf("%s%d",tmp,&x);

			if(*tmp=='Q')

				ans+=Find(x);

			else

				a[x]=x;

		}

		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: 11.358667078735703x):
#include<cstdio>

#include<cstring>

#include<algorithm>



#define N 100010



int a[N];



long long Find(int x){

	return a[x]==x? a[x]:Find(a[x]);

}



int main(){

	while(true){

		int n,q;

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

		if(!(n||q))

			break;

		a[1]=1;

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

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

		long long ans=0ll;

		while(q--){

			char tmp[10]={};

			int x;

			scanf("%s%d",tmp,&x);

			if(*tmp=='Q')

				ans+=Find(x);

			else

				a[x]=x;

		}

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

	}

	return 0;

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

#include <cstring>

#include <iostream>

#include <algorithm>

using namespace std;

const int maxn = 2e5 + 5;

#define bug(x) printf("  %s = %d \n",#x,x);

#define mem(arr,x) memset(arr,x,sizeof arr);

int par[maxn];



int dfs(int pos){

	if(par[pos] != pos)

		return dfs(par[pos]);

	else

	 	return pos;

}



int main() {

#ifdef LOCAL

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

#endif

	ios_base::sync_with_stdio(0);

	int N,Q;

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

		if(N == 0 && Q == 0) break;

		par[1] = 1;

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

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

		}

		char op;

		int  pos;

		long long  Sum = 0;

		getchar();

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

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

//			bug(op);

			if(op == 'Q'){

				Sum += dfs(pos);

			}

			else{

				par[pos] = pos;

			}

			getchar();

		}

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

	}

}

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

#include<cstring>

#include<algorithm>



#define N 100010



int a[N];



long long Find(int x){

	return a[x]==x? a[x]:Find(a[x]);

}



int main(){

	while(true){

		int n,q;

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

		if(!(n||q))

			break;

		a[1]=1;

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

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

		long long ans=0ll;

		while(q--){

			char tmp[10]={};

			int x;

			scanf("%s%d",tmp,&x);

			if(*tmp=='Q')

				ans+=Find(x);

			else

				a[x]=x;

		}

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

	}

	return 0;

}
// Slower C++ solution:
#include <stdio.h>

#include <stdlib.h>

#include <cmath>

#include <string.h>

#include <iostream>

#include<algorithm>

#include <queue>

#include <vector>

#include<string>

//******************************************************

#define lrt (rt*2)

#define rrt  (rt*2+1)

#define LL long long

#define inf 0x3f3f3f3f

#define pi acos(-1.0)

#define exp 1e-8

const double temp=(1+sqrt(5))/2;

//***************************************************

#define eps             1e-8

#define inf             0x3f3f3f3f

#define INF             2e18

#define LL              long long

#define ULL             unsigned long long

#define PI              acos(-1.0)

#define pb              push_back

#define mk              make_pair



#define all(a)          a.begin(),a.end()

#define rall(a)         a.rbegin(),a.rend()

#define SQR(a)          ((a)*(a))

#define Unique(a)       sort(all(a)),a.erase(unique(all(a)),a.end())

#define min3(a,b,c)     min(a,min(b,c))

#define max3(a,b,c)     max(a,max(b,c))

#define min4(a,b,c,d)   min(min(a,b),min(c,d))

#define max4(a,b,c,d)   max(max(a,b),max(c,d))

#define max5(a,b,c,d,e) max(max3(a,b,c),max(d,e))

#define min5(a,b,c,d,e) min(min3(a,b,c),min(d,e))

#define Iterator(a)     __typeof__(a.begin())

#define rIterator(a)    __typeof__(a.rbegin())

#define FastRead        ios_base::sync_with_stdio(0);cin.tie(0)

#define CasePrint       pc('C'); pc('a'); pc('s'); pc('e'); pc(' '); write(qq++,false); pc(':'); pc(' ')

#define vi              vector <int>

#define vL              vector <LL>

#define For(I,A,B)      for(int I = (A); I < (B); ++I)

#define rFor(I,A,B)     for(int I = (A); I >= (B); --I)

#define Rep(I,N)        For(I,0,N)

using namespace std;

const int maxn=1e5+10;

int pre[maxn];

void iint()

{

    for(int i=0;i<maxn;i++) pre[i]=i;

}

int find(int x)

{

    return x==pre[x]?x:find(pre[x]);

}

void join(int x,int y)

{

    int fx=find(x),fy=find(y);

    if(fx!=fy)  pre[fx]=fy;

}

int main()

{

    int n,k,num;

    char str[5];

    while(cin>>n>>k)

    {

        LL sum=0;

        if(n+k==0)  break;

        iint();

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

        {

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

        }

        while(k--)

        {

            scanf("%s%d",str,&num);

            if(str[0]=='M') pre[num]=num;

            else if(str[0]=='Q')    sum+=find(num);

        }

        cout<<sum<<endl;

    }

    return 0;

}

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

#include<cstring>

#include<algorithm>



#define N 100010



int a[N];



long long Find(int x){

	return a[x]==x? a[x]:Find(a[x]);

}



int main(){

	while(true){

		int n,q;

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

		if(!(n||q))

			break;

		a[1]=1;

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

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

		long long ans=0ll;

		while(q--){

			char tmp[10]={};

			int x;

			scanf("%s%d",tmp,&x);

			if(*tmp=='Q')

				ans+=Find(x);

			else

				a[x]=x;

		}

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

	}

	return 0;

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

#include <cstdio>

using namespace std;



const int N = 100000;



int n, p;

int pre[N+1];



int find(int x)

{

    while (x != pre[x])

        x = pre[x];

    return x;

}



int main(void)

{

    while (scanf("%d%d", &n, &p) != EOF) {

        if (!n && !p) break;

        pre[1] = 1;

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

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

        long long sum = 0;

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

            char s[2];

            int x;

            scanf("%s%d", s, &x);

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

                pre[x] = x;

            else

                sum += find(x);

        }

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

    }



    return 0;

}

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

#include<cstring>

#include<algorithm>



#define N 100010



int a[N];



long long Find(int x){

	return a[x]==x? a[x]:Find(a[x]);

}



int main(){

	while(true){

		int n,q;

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

		if(!(n||q))

			break;

		a[1]=1;

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

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

		long long ans=0ll;

		while(q--){

			char tmp[10]={};

			int x;

			scanf("%s%d",tmp,&x);

			if(*tmp=='Q')

				ans+=Find(x);

			else

				a[x]=x;

		}

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

	}

	return 0;

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

#include<iostream>

#include<cstring>

const int MAX_N=100000+16;

int a[MAX_N*2];

bool mark[MAX_N*2];

int N,Q;

int main(){

    //freopen("1.txt","r",stdin);

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

        if(N==0&&Q==0)break;

        memset(mark,false,sizeof(bool)*(N+10));

        a[1]=0;

        mark[1]=1;

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

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

        }

        long ans=0;

        while(Q--){

            char c[2];

            int x;

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

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

                while(!mark[x]){

                    x=a[x];

                }

                ans+=x;

            }

            else {

                mark[x]=true;

            }

        }

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

    }

    return 0;

}

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

#include<cstring>

#include<algorithm>



#define N 100010



int a[N];



long long Find(int x){

	return a[x]==x? a[x]:Find(a[x]);

}



int main(){

	while(true){

		int n,q;

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

		if(!(n||q))

			break;

		a[1]=1;

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

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

		long long ans=0ll;

		while(q--){

			char tmp[10]={};

			int x;

			scanf("%s%d",tmp,&x);

			if(*tmp=='Q')

				ans+=Find(x);

			else

				a[x]=x;

		}

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

	}

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define inf 0x3f3f3f3f

using namespace std;

typedef long long ll;

typedef pair<int,int>P;

const int MAXN=100010;



int f[MAXN], mark[MAXN];

P Q[MAXN];



int getf(int k, int id)

{

	return mark[k] < id ? k : f[k] = getf(f[k], id);

	/*如果k?点被??的??早于????，就返回k。否?????k的父?点，

	且因???路径上有一些?点未被??或者???期，可以路径??,直接?被

	???点到最近???点路径上的所有?点都指向最近???点*/

}



int main()

{

	int n, q;

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

	{

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

		{

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

			mark[i] = inf;

		}

		int cnt = 0;

		char c;

		int x;

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

		{

            getchar();

			scanf("%c %d", &c, &x);

			if(c == 'Q')

			Q[cnt++] = P(i, x);//P(i,x)表示在i?刻??x的最近??点

			else

			mark[x] = min(mark[x], i);//mark[i]表示i?点第一次被??的??，inf表示未被??

		}

		ll ans = 0;

		for(int i = cnt - 1; i >= 0; i--)

		ans += getf(Q[i].second, Q[i].first);

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

	}

 	return 0;

}

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

#include<cstring>

#include<algorithm>



#define N 100010



int a[N];



long long Find(int x){

	return a[x]==x? a[x]:Find(a[x]);

}



int main(){

	while(true){

		int n,q;

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

		if(!(n||q))

			break;

		a[1]=1;

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

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

		long long ans=0ll;

		while(q--){

			char tmp[10]={};

			int x;

			scanf("%s%d",tmp,&x);

			if(*tmp=='Q')

				ans+=Find(x);

			else

				a[x]=x;

		}

		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.1659929571612424x):
#include<iostream>

#include<cstdio>

#include<queue>

#include<cstring>

#include<algorithm>

#include<cmath>

#include<stack>

#define lson l, m, rt<<1

#define rson m+1, r, rt<<1|1

#define IO ios::sync_with_stdio(false);cin.tie(0);

#define INF 0x3f3f3f3f

typedef unsigned long long ll;

using namespace std;

int n, q, m, pre[100010];

char c;

int find(int x)

{

	while(x != pre[x]){

		x = pre[x];

	}

	return x;

}

int main()

{

	IO;

	while(cin >> n >> q){ 

		if(!n&&!q) break;

		ll sum=0;

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

			pre[i] = i;

		}

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

			cin >> pre[i];

		}

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

			cin >> c >> m;

			if(c == 'Q') 

				sum += find(m);

			else {

				pre[m] = m;

			}

		}

		cout << sum << endl;

	}

	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: 1.4716139697986128x):
#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;

}
// 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: 1.5775979137942868x):
#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;

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

#include<iostream>

#include<string.h>

#include<sstream>

#include<stdio.h>

#include<math.h>

#include<vector>

#include<string>

#include<queue>

#include<set>

#include<map>

//#pragma comment(linker,"/STACK:1024000000,1024000000")

using namespace std;

const int INF=0x3f3f3f3f;

const double eps=1e-8;

const double PI=acos(-1.0);

typedef long long ll;

const int maxn=100010;

int tree[maxn];

int Find(int x)

{

    if(tree[x]==x)

        return x;

    return Find(tree[x]);

}

int main()

{

    int n,m,id,i;

    char com[10];

    ll ans;



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

    {

        tree[1]=1;

        ans=0;

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

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

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

        {

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

            if(com[0]=='M')

                tree[id]=id;

            else

                ans+=Find(id);

        }

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

    }

    return 0;

}

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

using namespace std;

int n, q, f[100005];

long long sum;

int find(int v)

{

    int r = v;

    while(r != f[r])

        r = f[r];

    return r;

}

int main()

{

    while (scanf("%d %d", &n, &q) != EOF)

    {

        if (n == 0 && q == 0)

            break;

        sum = 0;

        f[1] = 1;

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

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

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

        {

            getchar();

            char s[10];

            int a;

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

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

                f[a] = a;

            else if (s[0] == 'Q')

                sum += find(a);

        }

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

    }

    return 0;

}