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

#include <bits/stdc++.h>
using namespace std;
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define _rep(i, n) _repl(i, 0, n)
#define rep(...) GET_MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string) { cout << endl; }
template <class H, class... T> void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

vector<int> tree[100005];

class LCA {
public:
  int n, ln; // number of nodes and log n
  vector<vector<int>> parent;
  vector<int> depth;
  LCA(int _n, int root = -1) : n(_n), depth(_n) {
    ln = 0;
    while (n >= (1 << ln))
      ln++; // calc log n
    parent = vector<vector<int>>(ln, vector<int>(n));
    if (root != -1)
      init(root);
  }
  void dfs(int v, int p, int d) {
    parent[0][v] = p;
    depth[v] = d;
    rep(i, tree[v].size()) if (tree[v][i] != p) dfs(tree[v][i], v, d + 1);
  }
  void init(int root) {
    dfs(root, -1, 0);
    for (int k = 0; k + 1 < ln; k++) {
      for (int v = 0; v < n; v++) {
        if (parent[k][v] < 0)
          parent[k + 1][v] = -1;
        else
          parent[k + 1][v] = parent[k][parent[k][v]];
      }
    }
  }
  int query(int u, int v) {
    if (depth[u] > depth[v])
      swap(u, v);
    for (int k = 0; k < ln; k++) {
      if ((depth[v] - depth[u]) >> k & 1)
        v = parent[k][v];
    }
    if (u == v)
      return u;
    for (int k = ln - 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;
  cin >> n;
  rep(i, n) {
    int k;
    cin >> k;
    rep(j, k) {
      int d;
      cin >> d;
      tree[i].pb(d);
    }
  }

  LCA lca(n, 0);

  int q;
  cin >> q;
  rep(_, q) {
    int a, b;
    cin >> a >> b;
    cout << lca.query(a, b) << "\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define _rep(i, n) _repl(i, 0, n)
#define rep(...) GET_MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string) { cout << endl; }
template <class H, class... T> void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

vector<int> tree[100005];

class LCA {
public:
  int n, ln; // number of nodes and log n
  vector<vector<int>> parent;
  vector<int> depth;
  LCA(int _n, int root = -1) : n(_n), depth(_n) {
    ln = 0;
    while (n > (1 << ln))
      ln++; // calc log n
    parent = vector<vector<int>>(ln, vector<int>(n));
    if (root != -1)
      init(root);
  }
  void dfs(int v, int p, int d) {
    parent[0][v] = p;
    depth[v] = d;
    rep(i, tree[v].size()) if (tree[v][i] != p) dfs(tree[v][i], v, d + 1);
  }
  void init(int root) {
    dfs(root, -1, 0);
    for (int k = 0; k + 1 < ln; k++) {
      for (int v = 0; v < n; v++) {
        if (parent[k][v] < 0)
          parent[k + 1][v] = -1;
        else
          parent[k + 1][v] = parent[k][parent[k][v]];
      }
    }
  }
  int query(int u, int v) {
    if (depth[u] > depth[v])
      swap(u, v);
    for (int k = 0; k < ln; k++) {
      if ((depth[v] - depth[u]) >> k & 1)
        v = parent[k][v];
    }
    if (u == v)
      return u;
    for (int k = ln - 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;
  cin >> n;
  rep(i, n) {
    int k;
    cin >> k;
    rep(j, k) {
      int d;
      cin >> d;
      tree[i].pb(d);
    }
  }

  LCA lca(n, 0);

  int q;
  cin >> q;
  rep(_, q) {
    int a, b;
    cin >> a >> b;
    cout << lca.query(a, b) << "\\n";
  }

  return 0;
}