Problem p02373 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
typedef vector<vector<int>> Graph;
const int inf = 1e9;
const int mod = 1e9 + 7;

class LCA {
private:
  static const int MAX_LOG = 20;
  const int n;
  Graph G;
  V depth;
  VV par;
  V cnt; // ?????????????????£????????°
public:
  LCA(int _n) : n(_n), G(_n), par(MAX_LOG, V(_n)), depth(_n), cnt(_n) {}
  // undirected
  void addEdge(int a, int b) {
    G[a].emplace_back(b);
    G[b].emplace_back(a);
  }
  // ??????????????±?????¨???1????????????????±???????
  void dfs(int v, int p, int d) {
    cerr << v << " " << p << " " << d << endl;
    par[0][v] = p;
    depth[v] = d;
    for (auto nxt : G[v]) {
      if (nxt != p) {
        dfs(nxt, v, d + 1);
      }
    }
  }
  // ????????????2^k????????????????±?????????????
  void setPar() {
    // 0????????¨??????1????????????????±???????
    dfs(0, -1, 0);

    // 2^i????????????????±???????
    rep(i, MAX_LOG - 1) {
      rep(j, n) {
        if (par[i][j] == -1) {
          par[i + 1][j] = -1;
        } else {
          par[i + 1][j] = par[i][par[i][j]];
        }
      }
    }
  }
  int lca(int a, int b) {
    // ??????a??¨b?????±???????????????
    if (depth[a] > depth[b]) {
      swap(a, b);
    }
    rep(i, MAX_LOG) {
      if ((depth[b] - depth[a]) >> i & 1) {
        b = par[i][b];
      }
    }
    if (a == b)
      return a;

    // ??¶???????????´????????§a, b????????????
    rrep(i, MAX_LOG) {
      if (par[i][a] != par[i][b]) {
        a = par[i][a];
        b = par[i][b];
      }
    }
    // a??¨b???1????????????????????´????????????
    return par[0][a];
  }

  void updateCnt(int u, int v) {
    cnt[u]++;
    cnt[v]++;
    int w = lca(u, v);
    cnt[w]--;
    if (par[0][w] != -1)
      cnt[par[0][w]]--;
  }
  int imosFinal(int v, int p) {
    int t = 0;
    for (auto &&nxt : G[v]) {
      t += imosFinal(nxt, v);
    }
    cnt[v] += t;
    return cnt[v];
  }
  int answer() {
    imosFinal(0, -1);
    int ans = 0;
    for (auto &&x : cnt) {
      ans += (x + 1) * x / 2;
    }
    return ans;
  }
};

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

  int n;
  scanf("%lld", &n);
  LCA lca(n);
  rep(i, n) {
    int k;
    scanf("%lld", &k);
    rep(j, k) {
      int c;
      scanf("%lld", &c);
      lca.addEdge(i, c);
    }
  }

  lca.setPar();

  int Q;
  scanf("%lld", &Q);
  rep(i, Q) {
    int u, v;
    scanf("%lld%lld", &u, &v);
    printf("%lld\n", lca.lca(u, v));
  }
}

// signed main() {
//     std::ios::sync_with_stdio(false);
//     std::cin.tie(0);
//
//     int N;
//     cin >> N;
//     LCA lca(N);
//     rep(i, N) {
//         int u, v;
//         cin >> u >> v;
//         u--, v--;
//         lca.addEdge(u, v);
//     }
//
//     cerr << "hey!" << endl;
//
//     lca.setPar();
//
//     cerr << "hey!" << endl;
//
//     int Q;
//     cin >> Q;
//     rep(i, Q) {
//         int u, v;
//         cin >> u >> v;
//         u--, v--;
//         lca.updateCnt(u, v);
//     }
//
//     cerr << "hey!" << endl;
//
//     cout << lca.answer() << endl;
// }

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
typedef vector<vector<int>> Graph;
const int inf = 1e9;
const int mod = 1e9 + 7;

class LCA {
private:
  static const int MAX_LOG = 20;
  const int n;
  Graph G;
  V depth;
  VV par;
  V cnt; // ?????????????????£????????°
public:
  LCA(int _n) : n(_n), G(_n), par(MAX_LOG, V(_n)), depth(_n), cnt(_n) {}
  // undirected
  void addEdge(int a, int b) {
    G[a].emplace_back(b);
    G[b].emplace_back(a);
  }
  // ??????????????±?????¨???1????????????????±???????
  void dfs(int v, int p, int d) {
    // cerr << v << " " << p << " " << d << endl;
    par[0][v] = p;
    depth[v] = d;
    for (auto nxt : G[v]) {
      if (nxt != p) {
        dfs(nxt, v, d + 1);
      }
    }
  }
  // ????????????2^k????????????????±?????????????
  void setPar() {
    // 0????????¨??????1????????????????±???????
    dfs(0, -1, 0);

    // 2^i????????????????±???????
    rep(i, MAX_LOG - 1) {
      rep(j, n) {
        if (par[i][j] == -1) {
          par[i + 1][j] = -1;
        } else {
          par[i + 1][j] = par[i][par[i][j]];
        }
      }
    }
  }
  int lca(int a, int b) {
    // ??????a??¨b?????±???????????????
    if (depth[a] > depth[b]) {
      swap(a, b);
    }
    rep(i, MAX_LOG) {
      if ((depth[b] - depth[a]) >> i & 1) {
        b = par[i][b];
      }
    }
    if (a == b)
      return a;

    // ??¶???????????´????????§a, b????????????
    rrep(i, MAX_LOG) {
      if (par[i][a] != par[i][b]) {
        a = par[i][a];
        b = par[i][b];
      }
    }
    // a??¨b???1????????????????????´????????????
    return par[0][a];
  }

  void updateCnt(int u, int v) {
    cnt[u]++;
    cnt[v]++;
    int w = lca(u, v);
    cnt[w]--;
    if (par[0][w] != -1)
      cnt[par[0][w]]--;
  }
  int imosFinal(int v, int p) {
    int t = 0;
    for (auto &&nxt : G[v]) {
      t += imosFinal(nxt, v);
    }
    cnt[v] += t;
    return cnt[v];
  }
  int answer() {
    imosFinal(0, -1);
    int ans = 0;
    for (auto &&x : cnt) {
      ans += (x + 1) * x / 2;
    }
    return ans;
  }
};

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

  int n;
  scanf("%lld", &n);
  LCA lca(n);
  rep(i, n) {
    int k;
    scanf("%lld", &k);
    rep(j, k) {
      int c;
      scanf("%lld", &c);
      lca.addEdge(i, c);
    }
  }

  lca.setPar();

  int Q;
  scanf("%lld", &Q);
  rep(i, Q) {
    int u, v;
    scanf("%lld%lld", &u, &v);
    printf("%lld\n", lca.lca(u, v));
  }
}

// signed main() {
//     std::ios::sync_with_stdio(false);
//     std::cin.tie(0);
//
//     int N;
//     cin >> N;
//     LCA lca(N);
//     rep(i, N) {
//         int u, v;
//         cin >> u >> v;
//         u--, v--;
//         lca.addEdge(u, v);
//     }
//
//     cerr << "hey!" << endl;
//
//     lca.setPar();
//
//     cerr << "hey!" << endl;
//
//     int Q;
//     cin >> Q;
//     rep(i, Q) {
//         int u, v;
//         cin >> u >> v;
//         u--, v--;
//         lca.updateCnt(u, v);
//     }
//
//     cerr << "hey!" << endl;
//
//     cout << lca.answer() << endl;
// }