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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (n); (i)++)
#define INF 5000000000000000
using namespace std;

struct Edge {
  int to;
  long weight;
  Edge(int t, long w) : to(t), weight(w) {}
};

long modpow(long a, long n, long mod) {
  long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// a^{-1} mod を計算する
long modinv(long a, long mod) { return modpow(a, mod - 2, mod); }

struct compare1 {
  bool operator()(const pair<long, long> &value, const long &key) {
    return (value.first < key);
  }
  bool operator()(const long &key, const pair<long, long> &value) {
    return (key < value.first);
  }
};

struct UnionFind {
  vector<int> par;
  vector<int> rank;

  UnionFind(int n = 1) { init(n); }

  void init(int n = 1) {
    par.resize(n);
    rank.resize(n);
    REP(i, n) par[i] = i, rank[i] = 0;
  }

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

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (rank[x] < rank[y])
      swap(x, y);
    if (rank[x] == rank[y])
      rank[x]++;
    par[y] = x;
    return true;
  }
};

template <class Abel> struct weightedUnionFind {
  vector<int> par;
  vector<int> rank;
  vector<Abel> diff_weight;

  weightedUnionFind(int n = 1, Abel SUM_UNITY = 0) { init(n, SUM_UNITY); }

  void init(int n = 1, Abel SUM_UNITY = 0) {
    par.resize(n);
    rank.resize(n);
    diff_weight.resize(n);
    REP(i, n) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    else {
      int r = root(par[x]);
      diff_weight[x] += diff_weight[par[x]];
      return par[x] = r;
    }
  }

  Abel weight(int x) {
    root(x);
    return diff_weight[x];
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y, Abel w) {
    w += weight(x);
    w -= weight(y);
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (rank[x] < rank[y])
      swap(x, y), w = -w;
    if (rank[x] == rank[y])
      rank[x]++;
    par[y] = x;
    diff_weight[y] = w;
    return true;
  }

  Abel diff(int x, int y) { return weight(y) - weight(x); }
};

using Graph = vector<vector<int>>;
using P = pair<long, long>;

/*
void dijkstra(int s, int V, Graph &G, long* d){
    priority_queue<P, vector<P>, greater<P>> pque;
    fill(d, d + V, INF);
    d[s] = 0;
    pque.push(P(0, s));

    while(!pque.empty()){
        P p = pque.top(); pque.pop();
        int now = p.second;
        if(d[now] < p.first) continue;
        REP(i, G[now].size()){
            Edge e = G[now][i];
            if(d[e.to] > d[now] + e.weight){
                d[e.to] = d[now] + e.weight;
                pque.push(P(d[e.to], e.to));
            }
        }
    }
}
*/

long GCD(long a, long b) {
  if (b == 0l)
    return a;
  if (a < b)
    return GCD(b, a);
  else
    return GCD(b, a % b);
}

int main() {
  int N;
  cin >> N;
  int ans[200005];
  REP(i, N) ans[i] = 0;
  REP(i, N - 1) {
    int a;
    cin >> a;
    a--;
    ans[a]++;
  }
  REP(i, N) cout << ans[i] << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (n); (i)++)
#define INF 5000000000000000
using namespace std;

struct Edge {
  int to;
  long weight;
  Edge(int t, long w) : to(t), weight(w) {}
};

long modpow(long a, long n, long mod) {
  long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// a^{-1} mod を計算する
long modinv(long a, long mod) { return modpow(a, mod - 2, mod); }

struct compare1 {
  bool operator()(const pair<long, long> &value, const long &key) {
    return (value.first < key);
  }
  bool operator()(const long &key, const pair<long, long> &value) {
    return (key < value.first);
  }
};

struct UnionFind {
  vector<int> par;
  vector<int> rank;

  UnionFind(int n = 1) { init(n); }

  void init(int n = 1) {
    par.resize(n);
    rank.resize(n);
    REP(i, n) par[i] = i, rank[i] = 0;
  }

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

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (rank[x] < rank[y])
      swap(x, y);
    if (rank[x] == rank[y])
      rank[x]++;
    par[y] = x;
    return true;
  }
};

template <class Abel> struct weightedUnionFind {
  vector<int> par;
  vector<int> rank;
  vector<Abel> diff_weight;

  weightedUnionFind(int n = 1, Abel SUM_UNITY = 0) { init(n, SUM_UNITY); }

  void init(int n = 1, Abel SUM_UNITY = 0) {
    par.resize(n);
    rank.resize(n);
    diff_weight.resize(n);
    REP(i, n) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    else {
      int r = root(par[x]);
      diff_weight[x] += diff_weight[par[x]];
      return par[x] = r;
    }
  }

  Abel weight(int x) {
    root(x);
    return diff_weight[x];
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y, Abel w) {
    w += weight(x);
    w -= weight(y);
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (rank[x] < rank[y])
      swap(x, y), w = -w;
    if (rank[x] == rank[y])
      rank[x]++;
    par[y] = x;
    diff_weight[y] = w;
    return true;
  }

  Abel diff(int x, int y) { return weight(y) - weight(x); }
};

using Graph = vector<vector<int>>;
using P = pair<long, long>;

/*
void dijkstra(int s, int V, Graph &G, long* d){
    priority_queue<P, vector<P>, greater<P>> pque;
    fill(d, d + V, INF);
    d[s] = 0;
    pque.push(P(0, s));

    while(!pque.empty()){
        P p = pque.top(); pque.pop();
        int now = p.second;
        if(d[now] < p.first) continue;
        REP(i, G[now].size()){
            Edge e = G[now][i];
            if(d[e.to] > d[now] + e.weight){
                d[e.to] = d[now] + e.weight;
                pque.push(P(d[e.to], e.to));
            }
        }
    }
}
*/

long GCD(long a, long b) {
  if (b == 0l)
    return a;
  if (a < b)
    return GCD(b, a);
  else
    return GCD(b, a % b);
}

int main() {
  int N;
  cin >> N;
  int ans[20005];
  REP(i, N) ans[i] = 0;
  REP(i, N - 1) {
    int a;
    cin >> a;
    a--;
    ans[a]++;
  }
  REP(i, N) cout << ans[i] << endl;

  return 0;
}