Skip to content

Instantly share code, notes, and snippets.

@zeptometer
Created June 4, 2012 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeptometer/2867713 to your computer and use it in GitHub Desktop.
Save zeptometer/2867713 to your computer and use it in GitHub Desktop.
2170.cpp
#include <cstdio>
#include <queue>
#define MAX 100001
using namespace std;
int mark[MAX],t[MAX];
int query(int v){
if(mark[v]) return v;
else{
int a = query(t[v]);
t[v] = a;
return a;
}
}
int main(){
int n,q;
while(scanf("%d %d",&n,&q),n){
int ans=0;
deque<pair<char,int> > qs;
for(int i=1; i<=n; i++) mark[i]=0; mark[1]=1;
for(int i=2; i<=n; i++){
int p;
scanf("%d",&p);
t[i] = p;
}
for(int i=0; i<q; i++){
char order; int v;
scanf(" %c %d",&order,&v);
if(order=='M') mark[v]++;
qs.push_front(make_pair(order,v));
}
for(int i=0; i<qs.size(); i++)
if(qs[i].first=='Q')
ans += query(qs[i].second);
else
mark[qs[i].second]--;
printf("%d\n",ans);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment