Problem p02373 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define fore(i, a) for (auto &i : a)
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
//---------------------------------------------------------------------------------------------------
class LCA {
public:
  int NV, logNV;
  vector<int> D;
  vector<vector<int>> P;
  vector<vector<int>> E;

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

  void add_edge(int a, int b) {
    E[a].push_back(b);
    E[b].push_back(a);
  }

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

  void dfs(int v, int par, int d) {
    D[v] = d;
    P[0][v] = par;
    for (int i : E[v])
      if (i != par)
        dfs(i, v, d + 1);
  }

  void build() {
    for (int k = 0; k < logNV - 1; 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 lca(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];
  }
};
/*---------------------------------------------------------------------------------------------------
????????????????????????????????? ??§?????§
??????????????? ??§?????§ ???????´<_??? ?????? Welcome to My Coding Space!
???????????? ??? ?´_???`??????/??? ???i
?????????????????????????????? ???  |???|
????????? /?????? /??£??£??£??£/??????|
??? ???_(__??????/???    ???/ .| .|????????????
??? ????????????/????????????/??????u??????
---------------------------------------------------------------------------------------------------*/

//---------------------------------------------------------------------------------------------------
void _main() {
  int N;
  cin >> N;
  LCA lca;
  lca.init(N);
  rep(i, 0, N) {
    int K;
    cin >> K;
    rep(j, 0, K) {
      int c;
      cin >> c;
      lca.add_edge(i, c);
    }
  }
  lca.make();

  int Q;
  cin >> Q;
  rep(q, 0, Q) {
    int a, b;
    cin >> a >> b;
    int ans = lca.lca(a, b);
    printf("%d\n", ans);
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define fore(i, a) for (auto &i : a)
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
//---------------------------------------------------------------------------------------------------
class LCA {
public:
  int NV, logNV;
  vector<int> D;
  vector<vector<int>> P;
  vector<vector<int>> E;

  LCA() {}
  void init(int N) {
    NV = N;
    logNV = 1;
    while (NV > (1LL << logNV))
      logNV++;
    D = vector<int>(NV);
    P = vector<vector<int>>(logNV, vector<int>(NV));
    E = vector<vector<int>>(N);
  }

  void add_edge(int a, int b) {
    E[a].push_back(b);
    E[b].push_back(a);
  }

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

  void dfs(int v, int par, int d) {
    D[v] = d;
    P[0][v] = par;
    for (int i : E[v])
      if (i != par)
        dfs(i, v, d + 1);
  }

  void build() {
    for (int k = 0; k < logNV - 1; 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 lca(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];
  }
};
/*---------------------------------------------------------------------------------------------------
????????????????????????????????? ??§?????§
??????????????? ??§?????§ ???????´<_??? ?????? Welcome to My Coding Space!
???????????? ??? ?´_???`??????/??? ???i
?????????????????????????????? ???  |???|
????????? /?????? /??£??£??£??£/??????|
??? ???_(__??????/???    ???/ .| .|????????????
??? ????????????/????????????/??????u??????
---------------------------------------------------------------------------------------------------*/

//---------------------------------------------------------------------------------------------------
void _main() {
  int N;
  cin >> N;
  LCA lca;
  lca.init(N);
  rep(i, 0, N) {
    int K;
    cin >> K;
    rep(j, 0, K) {
      int c;
      cin >> c;
      lca.add_edge(i, c);
    }
  }
  lca.make();

  int Q;
  cin >> Q;
  rep(q, 0, Q) {
    int a, b;
    cin >> a >> b;
    int ans = lca.lca(a, b);
    printf("%d\n", ans);
  }
}