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

#include <iostream>
#include <stdio.h>
#define MAX_N 100000
#define MAX_Q 100000
#define M 0
using namespace std;

long long par[MAX_N + 1];  // parents
long long Rank[MAX_N + 1]; // depth of tree

// initialize
void init(long long n) {
  for (long long i = 0; i < n; i++) {
    par[i] = i;
    Rank[i] = 0;
  }
}

// find root
long long find(long long x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

// unite group of x and group of y
void unite(long long x, long long y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (Rank[x] < Rank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (Rank[x] == Rank[y])
      Rank[x]++;
    // in other cases we don't have to change rank[x] cos
    // rank of x is needed only if x is root of tree
  }
}

// check if x and y belong to the same group
bool same(long long x, long long y) { return find(x) == find(y); }

long long N, Q;
long long sum = 0;
long long tree[MAX_N + 1];

long long nearestAncstr(long long num) {
  if (same(num, M))
    return num;
  nearestAncstr(tree[num]);
}

void operate(char symbol, long long num) {
  if (symbol == 'Q') {
    sum += nearestAncstr(num);
    sum++; // 1-indexed
  } else
    unite(num, M);
}

long long num;
char symbol;

void solve() {
  init(N);
  sum = 0;
  tree[0] = 0;
  for (long long i = 1; i < N; i++) {
    cin >> tree[i];
    tree[i]--;
  } // 0-indexed
  for (int i = 0; i < Q; i++) {
    cin >> symbol >> num;
    num--;
    operate(symbol, num);
  }
  printf("%lld\\n", sum);
}

int main() {
  while (true) {
    cin >> N >> Q;
    if (N == 0 && Q == 0)
      break;
    solve();
  }

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <stdio.h>
#define MAX_N 100000
#define MAX_Q 100000
#define M 0
using namespace std;

long long par[MAX_N + 1];  // parents
long long Rank[MAX_N + 1]; // depth of tree

// initialize
void init(long long n) {
  for (long long i = 0; i < n; i++) {
    par[i] = i;
    Rank[i] = 0;
  }
}

// find root
long long find(long long x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

// unite group of x and group of y
void unite(long long x, long long y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (Rank[x] < Rank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (Rank[x] == Rank[y])
      Rank[x]++;
    // in other cases we don't have to change rank[x] cos
    // rank of x is needed only if x is root of tree
  }
}

// check if x and y belong to the same group
bool same(long long x, long long y) { return find(x) == find(y); }

long long N, Q;
long long sum = 0;
long long tree[MAX_N + 1];

long long nearestAncstr(long long num) {
  if (same(num, M))
    return num;
  nearestAncstr(tree[num]);
}

void operate(char symbol, long long num) {
  if (symbol == 'Q') {
    sum += nearestAncstr(num);
    sum++; // 1-indexed
  } else
    unite(num, M);
}

long long num;
char symbol;

void solve() {
  init(N);
  tree[0] = 0;
  for (long long i = 1; i < N; i++) {
    cin >> tree[i];
    tree[i]--;
  } // 0-indexed
  cin >> symbol >> num;
  num--;
  // scanf("%c%d",&symbol,&num);
  while (true) {
    if (symbol == '0' && num == 0)
      break;
    operate(symbol, num);
    cin >> symbol >> num;
    num--;
    // scanf("%c%d",&symbol,&num);
  }
  printf("%lld\\n", sum);
}

int main() {
  cin >> N >> Q;
  solve();

  return 0;
}