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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
const int MAXN = 100010;

vector<int> G[MAXN];
int qtype[MAXN], vertex[MAXN], marked[MAXN];
int par[MAXN], pp[MAXN];

void init() {
  for (int i = 0; i < MAXN; i++)
    par[i] = i;
}

int find(int x) {
  if (par[x] == x)
    return x;
  return par[x] = find(par[x]);
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  par[y] = x;
}

void dfs(int v, int p, int m) {
  int nm = m;
  if (marked[v])
    nm = v;
  pp[v] = p;
  unite(nm, v);
  for (size_t i = 0; i < G[v].size(); i++) {
    dfs(G[v][i], v, nm);
  }
}

int main(void) {
  int N, Q;
  while (cin >> N >> Q) {
    if (N == 0 && Q == 0)
      break;
    init();
    for (int i = 0; i < MAXN; i++)
      marked[i] = 0;
    marked[1] = 1;
    for (int i = 0; i < MAXN; i++)
      G[i].clear();
    for (int i = 2; i <= N; i++) {
      int v;
      scanf("%d", &v);
      G[v].push_back(i);
    }
    for (int i = 0; i < Q; i++) {
      char c;
      scanf(" %c %d", &c, vertex + i);
      if (c == 'Q')
        qtype[i] = 1;
      else {
        qtype[i] = 0;
        marked[vertex[i]]++;
      }
    }
    dfs(1, 0, 0);
    ll ans = 0;
    for (int i = Q - 1; i >= 0; i--) {
      if (qtype[i]) {
        ans += find(vertex[i]);
        //                cout << "now ans is " << ans << endl;
      } else {
        marked[vertex[i]]--;
        if (!marked[vertex[i]])
          unite(pp[vertex[i]], vertex[i]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

// union-find
const int MAXN = 100010;
int par[MAXN];
int num[MAXN];
bool exist[MAXN];
int parent[MAXN];

void init(int n) {
  for (int i = 0; i <= n; i++) {
    par[i] = i;
    num[i] = 0;
    exist[i] = false;
  }
  exist[1] = true;
}

int find(int x) {
  if (exist[x])
    return x;
  return par[x] = find(parent[x]);
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (num[x] < num[y])
    par[x] = y;
  else {
    par[y] = x;
    if (num[x] == num[y])
      num[x]++;
  }
}

bool same(int x, int y) { return find(x) == find(y); }

int query[MAXN];
int number[MAXN];

int main(void) {
  int N, Q;
  while (cin >> N >> Q) {
    if (N == 0 && Q == 0)
      break;
    init(N);
    for (int i = 2; i <= N; i++) {
      scanf("%d", parent + i);
    }
    for (int i = 0; i < Q; i++) {
      char c;
      scanf(" %c %d", &c, number + i);
      if (c == 'Q')
        query[i] = 0;
      else {
        query[i] = 1;
        exist[number[i]] = true;
      }
    }
    ll ans = 0;
    for (int i = Q - 1; i >= 0; i--) {
      if (query[i] == 0) {
        ans += find(number[i]);
        //                cout << "now ans is " << ans << endl;
      } else {
        exist[number[i]] = false;
      }
    }
    cout << ans << endl;
  }
  return 0;
}