Created
October 16, 2015 05:58
-
-
Save popon-01/2065c125ee48afd6691a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<vector> | |
#include<limits> | |
#include<utility> | |
#include<algorithm> | |
using namespace std; | |
typedef long long ll; | |
typedef pair< int,int > P; | |
const int inf = numeric_limits< int >::max(); | |
const int mod = 1e9 + 7; | |
int parent[100050]; | |
int uf[100050]; | |
vector< P > query; | |
int find(int a){ | |
if(uf[a] < 0)return a; | |
return uf[a] = find(uf[a]); | |
} | |
ll solve(int n,int q){ | |
query.clear(); | |
for(int i = 0;i <= n;++i){ | |
parent[i] = -1; | |
uf[i] = -1; | |
} | |
for(int i = 2;i <= n;++i){ | |
cin >> parent[i]; | |
uf[i] = parent[i]; | |
} | |
for(int i = 0;i < q;++i){ | |
int type,node; | |
char c; | |
cin >> c >> node; | |
if(c == 'M'){ | |
query.push_back(P(0,node)); | |
uf[node] = -1; | |
}else{ | |
query.push_back(P(1,node)); | |
} | |
} | |
ll res = 0; | |
for(int i = q-1;i >= 0;--i){ | |
int qfirst = query[i].first,qsecond =query[i].second; | |
if(qfirst == 0){ | |
uf[qsecond] = parent[qsecond]; | |
}else{ | |
res += (ll)find(qsecond); | |
} | |
} | |
return res; | |
} | |
int main(void){ | |
int n,q; | |
while(cin >> n >> q,n+q){ | |
cout << solve(n,q) << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment