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

#include <bits/stdc++.h>

using namespace std;

#define INF (1 << 29)
#define MAX_V 100010
#define MAX_LOG_V 110
typedef long long ll;

vector<int> G[MAX_V];
int root;
int deg[MAX_V];

int parent[MAX_LOG_V][MAX_V];
int depth[MAX_V];

void dfs(int v, int p, int d) {
  parent[0][v] = p;
  depth[v] = d;
  for (int i = 0; i < (int)G[v].size(); i++) {
    if (G[v][i] != p) {
      dfs(G[v][i], v, d + 1);
    }
  }
}

void init(int V) {
  root = 0;
  dfs(root, -1, 0);
  for (int k = 0; k + 1 < MAX_LOG_V; k++) {
    for (int v = 0; v < V; v++) {
      if (parent[k][v] < 0) {
        parent[k + 1][v] = -1;
      } else {
        parent[k + 1][v] = parent[k][parent[k][v]];
      }
    }
  }
}

int lca(int u, int v) {
  if (depth[u] > depth[v]) {
    swap(u, v);
  }
  for (int k = 0; k < MAX_LOG_V; k++) {
    if ((depth[v] - depth[u]) >> k & 1) {
      v = parent[k][v];
    }
  }
  if (u == v) {
    return u;
  }
  for (int k = MAX_LOG_V - 1; k >= 0; k--) {
    if (parent[k][u] != parent[k][v]) {
      u = parent[k][u];
      v = parent[k][v];
    }
  }
  return parent[0][u];
}

int main() {
  int n, p;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    cin >> p;
    --p;
    G[p].push_back(i + 1);
  }

  queue<int> Q;
  Q.push(0);
  vector<ll> d(MAX_V, INF);
  d[0] = 0;
  vector<int> path;
  path.push_back(0);
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    for (int i = 0; i < (int)G[v].size(); i++) {
      int to = G[v][i];
      if (d[v] + 1 < d[to]) {
        d[to] = d[v] + 1;
        Q.push(to);
        path.push_back(to);
      }
    }
  }

  init(n);

  ll res = 0;
  for (int i = 1; i < n; i++) {
    int l = lca(path[i - 1], path[i]);
    res += d[path[i - 1]] + d[path[i]] - 2 * d[l];
  }
  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)
typedef long long ll;

int N;
vector<pair<int, int>> E[101010];

class LCA {
public:
  int NV;
  int logNV;
  vector<int> D;
  vector<long long> L;
  vector<vector<int>> P;

  LCA(int N) {
    NV = N;
    logNV = 0;
    while (NV > (1LL << logNV))
      logNV++;
    D = vector<int>(NV);
    L = vector<long long>(NV);
    P = vector<vector<int>>(logNV, vector<int>(NV));
  }

  void init() {
    dfs(0, -1, 0, 0);
    build();
  }

  void dfs(int v, int par, int d, long long l) {
    D[v] = d;
    P[0][v] = par;
    L[v] = l;

    for (int i = 0; i < (int)E[v].size(); i++) {
      int w = E[v][i].first;
      int lc = E[v][i].second;
      if (w == par)
        continue;
      dfs(w, v, d + 1, lc + l);
    }
  }

  void build() {
    for (int k = 0; k + 1 < logNV; k++) {
      for (int v = 0; v < NV; v++) {
        if (P[k][v] < 0)
          P[k + 1][v] = -1;
        else
          P[k + 1][v] = P[k][P[k][v]];
      }
    }
  }

  int query(int u, int v) {
    if (D[u] > D[v])
      swap(u, v);
    for (int k = 0; k < logNV; k++) {
      if ((D[v] - D[u]) >> k & 1)
        v = P[k][v];
    }
    if (u == v)
      return u;

    for (int k = logNV - 1; k >= 0; k--) {
      if (P[k][u] != P[k][v]) {
        u = P[k][u];
        v = P[k][v];
      }
    }
    return P[0][u];
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> N) {
    rep(i, 0, N) E[i].clear();
    rep(i, 1, N) {
      int p;
      cin >> p;
      p--;
      E[p].push_back(make_pair(i, 1));
      E[i].push_back(make_pair(p, 1));
    }

    LCA lca(N);
    lca.init();

    ll ans = 0;

    queue<pair<int, int>> que;
    que.push(make_pair(0, -1));
    int back = -1;
    while (!que.empty()) {
      auto p = que.front();
      que.pop();

      for (auto pp : E[p.first]) {
        if (pp.first == p.second)
          continue;
        que.push(make_pair(pp.first, p.first));
      }

      if (0 <= back) {
        int z = lca.query(p.first, back);
        ll ret = lca.L[p.first] + lca.L[back] - 2 * lca.L[z];
        ans += ret;
      }

      back = p.first;
    }
    cout << ans << endl;
  }
}