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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

struct Edge {
  int to, cost;

  Edge(int t, int c = 0) : to(t), cost(c) {}
  bool operator>(const Edge &rhs) const { return cost > rhs.cost; }
  bool operator<(const Edge &rhs) const { return cost < rhs.cost; }
};
using Graph = vector<vector<Edge>>;

// calc Lowest Common Ancestor
// O(N logN)
struct LCA {
  const Graph &G;
  int N, R;
  int LOG_MAX;
  vector<int> depth;
  vector<vector<int>> parent;

  LCA(const Graph &G_, int N_, int R_) : G(G_), N(N_), R(R_) {
    LOG_MAX = log2(N) + 1;
    depth.assign(N, -1);
    parent.resize(LOG_MAX);
    for (int i = 0; i < LOG_MAX; ++i)
      parent[i].resize(N);

    function<void(int, int, int)> dfs = [&](int u, int p, int d) {
      depth[u] = d;
      parent[0][u] = p;
      for (const auto &e : G[u])
        if (e.to != p)
          dfs(e.to, u, d + 1);
    };
    dfs(R, -1, 0);

    for (int i = 0; i + 1 < LOG_MAX; ++i) {
      for (int v = 0; v < N; ++v) {
        if (parent[i][v] == -1)
          parent[i + 1][v] = -1;
        else
          parent[i + 1][v] = parent[i][parent[i][v]];
      }
    }
  }

  int query(int u, int v) {
    if (depth[u] > depth[v])
      swap(u, v);
    // assume: depth[u] <= depth[v]
    for (int i = 0; i < LOG_MAX; ++i)
      if ((depth[v] - depth[u]) >> i & 1)
        v = parent[i][v];
    if (u == v)
      return u;

    for (int i = LOG_MAX - 1; i >= 0; --i) {
      if (parent[i][u] != parent[i][v]) {
        u = parent[i][u];
        v = parent[i][v];
      }
    }
    return parent[0][u];
  }
};

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

  int V;
  cin >> V;
  Graph G(V);
  REP(i, V) {
    int k;
    cin >> k;
    REP(j, k) {
      int c;
      cin >> c;
      G[i].PB(c);
      G[c].PB(i);
    }
  }
  LCA lca(G, V, 0);

  int Q;
  cin >> Q;
  while (Q--) {
    int u, v;
    cin >> u >> v;
    cout << lca.query(u, v) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

struct Edge {
  int to, cost;

  Edge(int t, int c = 0) : to(t), cost(c) {}
  bool operator>(const Edge &rhs) const { return cost > rhs.cost; }
  bool operator<(const Edge &rhs) const { return cost < rhs.cost; }
};
using Graph = vector<vector<Edge>>;

// calc Lowest Common Ancestor
// O(N logN)
struct LCA {
  const Graph &G;
  int N, R;
  int LOG_MAX;
  vector<int> depth;
  vector<vector<int>> parent;

  LCA(const Graph &G_, int N_, int R_) : G(G_), N(N_), R(R_) {
    LOG_MAX = log2(N);
    depth.assign(N, -1);
    parent.resize(LOG_MAX);
    for (int i = 0; i < LOG_MAX; ++i)
      parent[i].resize(N);

    function<void(int, int, int)> dfs = [&](int u, int p, int d) {
      depth[u] = d;
      parent[0][u] = p;
      for (const auto &e : G[u])
        if (e.to != p)
          dfs(e.to, u, d + 1);
    };
    dfs(R, -1, 0);

    for (int i = 0; i + 1 < LOG_MAX; ++i) {
      for (int v = 0; v < N; ++v) {
        if (parent[i][v] == -1)
          parent[i + 1][v] = -1;
        else
          parent[i + 1][v] = parent[i][parent[i][v]];
      }
    }
  }

  int query(int u, int v) {
    if (depth[u] > depth[v])
      swap(u, v);
    // depth[u] <= depth[v]
    for (int i = 0; i < LOG_MAX; ++i)
      if ((depth[v] - depth[u]) >> i & 1)
        v = parent[i][v];
    if (u == v)
      return u;

    for (int i = LOG_MAX - 1; i >= 0; --i) {
      if (parent[i][u] != parent[i][v]) {
        u = parent[i][u];
        v = parent[i][v];
      }
    }
    return parent[0][u];
  }
};

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

  int V;
  cin >> V;
  Graph G(V);
  REP(i, V) {
    int k;
    cin >> k;
    REP(j, k) {
      int c;
      cin >> c;
      G[i].PB(c);
      G[c].PB(i);
    }
  }
  LCA lca(G, V, 0);

  int Q;
  cin >> Q;
  while (Q--) {
    int u, v;
    cin >> u >> v;
    cout << lca.query(u, v) << endl;
  }

  return 0;
}