Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
//******************************************************
#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;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring> //by  @山?叶??     A
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
typedef long long ll;
double dinf = 99999999.9, mm = 1e-8;
const int N = 100008, inf = 0x3f3f3f3f;
int f[N], vis[N], near[N];
int getf(int v) //?找父?点
{
  if (f[v] == v)
    return v;
  else
    return f[v] = getf(f[v]);
}
int getnear(int v) {
  if (vis[f[v]] == 1)
    return near[v] = f[v];
  else
    return near[v] = getnear(f[v]);
}

int main() {
  int i, q, m, n, k;
  ll sum;
  char ch[4];
  while (scanf("%d%d", &n, &q), n + q) {
    sum = 0;
    k = 0;
    for (i = 1; i <= n; i++) {
      scanf("%d", &m);
      f[i] = m;
      vis[i] = 0;
      near[i] = 1;
    }
    vis[1] = 1; /// 初始化
    for (i = 1; i <= q; i++) {
      scanf("%s%d", ch, &m);
      if (ch[0] == 'Q') {
        if (k == 0)
          sum += (ll)near[m];
        else
          sum += (ll)getnear(m);
        k = 0;
      } else {
        vis[m] = 1;
        k = 1;
      }
    }
    printf("%lld\\n", sum);
  }
  return 0;
}