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

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using Weight = int;
struct Edge {
  int s, d;
  Weight w;
  Edge(int s, int d, Weight w = 1) : s(s), d(d), w(w){};
  bool operator<(const Edge &e) const { return w < e.w; }
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

struct LowestCommonAncestor {
  const int n, log2_n;
  vector<vector<int>> parent;
  vector<int> depth;
  LowestCommonAncestor(const Graph &g, int root)
      : n(g.size()), log2_n(log2(n) + 1), parent(log2_n, vector<int>(n)),
        depth(n) {
    dfs(g, root, -1, 0);
    for (int k = 0; k + 1 < log2_n; k++) {
      for (int v = 0; v < (int)g.size(); v++) {
        if (parent[k][v] < 0)
          parent[k + 1][v] = -1;
        else
          parent[k + 1][v] = parent[k][parent[k][v]];
      }
    }
  }

  void dfs(const Graph &g, int v, int p, int d) {
    parent[0][v] = p;
    depth[v] = d;
    for (auto &e : g[v]) {
      if (e.d != p)
        dfs(g, e.d, v, d + 1);
    }
  }

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

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  Graph g(n);
  rep(i, 0, n) {
    int k;
    cin >> k;
    rep(j, 0, k) {
      int c;
      cin >> c;
      g[i].emplace_back(i, c);
    }
  }
  int q;
  cin >> q;
  LowestCommonAncestor lca(g, 0);
  rep(i, 0, q) {
    int u, v;
    cin >> u >> v;
    cout << lca.get(u, v) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using Weight = int;
struct Edge {
  int s;    // source
  int d;    // destination
  Weight w; // weight
  Edge(int s, int d, Weight w = 1) : s(s), d(d), w(w){};
  bool operator<(const Edge &e) const { return w < e.w; }
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

struct UnionFind {
  vector<int> parent;
  int size;
  UnionFind(int n) : parent(n, -1), size(n) {}
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (set_size(x) < set_size(y))
      swap(x, y);
    parent[x] += parent[y];
    parent[y] = x;
    size--;
    return true;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int root(int x) { return parent[x] < 0 ? x : parent[x] = root(parent[x]); }
  int set_size(int x) { return -parent[root(x)]; }
};

// Tarjan's off-line lowest common ancestors
struct Query {
  int u, v;
  Query(int u, int v) : u(u), v(v) {}
};
struct LeastCommonAncestor {
  vector<vector<pair<int, Query>>> query_set;
  Graph g;
  vector<int> color;
  vector<int> ancestor;
  vector<int> res;
  UnionFind uf;
  LeastCommonAncestor(const Graph &g, vector<Query> query)
      : g(g), color(g.size()), ancestor(g.size()), uf(g.size()),
        res(query.size()), query_set(g.size()) {
    int n = query.size();
    for (int i = 0; i < n; i++) {
      query_set[query[i].u].emplace_back(i, query[i]);
      query_set[query[i].v].emplace_back(i, query[i]);
    }
  }
  void visit(int s, int prev) {
    ancestor[uf.root(s)] = s;
    for (auto &e : g[s]) {
      if (e.d == prev)
        continue;
      visit(e.d, s);
      uf.unite(e.s, e.d);
      ancestor[uf.root(s)] = s;
    }
    color[s] = 1;
    for (auto &p : query_set[s]) {
      Query q = p.second;
      int w = (q.v == s ? q.u : q.u == s ? q.v : -1);
      if (w == -1 || !color[w])
        continue;
      res[p.first] = ancestor[uf.root(w)];
    }
  }
  vector<int> solve(int root) {
    int n = g.size();
    UnionFind uf(n);
    vector<int> color(n), ancestor(n);
    visit(root, -1);
    return res;
  }
};
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  Graph g(n);
  rep(i, 0, n) {
    int k;
    cin >> k;
    rep(j, 0, k) {
      int c;
      cin >> c;
      g[i].emplace_back(i, c);
      g[c].emplace_back(c, i);
    }
  }
  int q;
  cin >> q;
  vector<Query> query;
  rep(i, 0, q) {
    int u, v;
    cin >> u >> v;
    query.emplace_back(u, v);
  }
  LeastCommonAncestor lca(g, query);
  vector<int> res = lca.solve(0);
  for (auto &e : res)
    cout << e << endl;
  return 0;
}