#include <iostream>
using namespace std;
typedef long long ll;
ll ans = 0LL;
int par[100007], ranks[100007], N, Q;
void init()
{
ans = 0LL;
par[0] = 1;
par[1] = 1;
ranks[0] = 1;
ranks[1] = 0x3f3f3f3f;
for (int i = 2; i <= N; i++)
{
scanf("%d", &par[i]);
ranks[i] = 1;
}
}
int find(int x)
{
if (x == par[x])
{
return par[x];
}
else
{
return find(par[x]);
}
}
void solve()
{
char op;
int v;
for (int i = 1; i <= Q; i++)
{
scanf("\n%c %d", &op, &v);
if (op == 'Q')
{
ans += find(v);
}
else
{
par[v] = v;
}
}
printf("%lld\n", ans);
}
int main()
{
while (true)
{
scanf("%d%d", &N, &Q);
if (N == 0 && Q == 0)
{
break;
}
init();
solve();
}
return 0;
}
AOJ 2170 Marked Ancestor 并查集
最新推荐文章于 2024-11-10 09:30:35 发布