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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PI 4 * atan(1.0)
typedef long long ll;
typedef long double ld;
using namespace std;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
const int SIZE = 100001;
// int arr[SIZE]={};
//???????????§???????????¬
const int MAX_V = 100001;
const int MAX_LOGV = 32;
//??°???????????£??\\???????????¨???????????????v??¨???????????£????????????????????\\??£????????????
vector<int> G[MAX_V];
//??????2^k????????£????????????????????????
int parent[MAX_LOGV][MAX_V] = {};
//??????????????±???
int depth[MAX_V] = {};
//?????????????????????
int root = 0;
void dfs(int v, int p, int d);
void init(int v);
int lca(int u, int v);

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int c;
      cin >> c;
      G[i].push_back(c);
      G[c].push_back(i);
    }
  }
  init(N);
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int a, b;
    cin >> a >> b;
    cout << lca(a, b) << endl;
  }
  return 0;
}

//?????°v,p,d?????????????????§??±???????????¢?´¢?????????
void dfs(int v, int p, int d) {
  //parent[0][v]????????????v??????1????????£??????????????????p?????£??\\
	parent[0][v]=p;
  //depth[v]????????????v?????±?????????d?????£??\\
	depth[v]=d;
  // G[v]??????????????????G[v][i]???p??§????????????
  // dfs(G[v][i],v,d+1)????????¶
  //?????????????????????????????????????????£?????°??????????????????????????§??????
  for (int i = 0; i < G[v].size(); i++) {
    if (G[v][i] != p) {
      dfs(G[v][i], v, d + 1);
    }
  }
}
//?????????
// V?????????????????°
void init(int V) {
  //?????????dfs???????????????
  //????????????????????????????????????parent[0]??¨depth?????????
  dfs(root, -1, 0);
  // parent????????????
  for (int k = 0; k + 1 < MAX_LOGV; k++) {
    for (int v = 0; v < V; v++) {
      if (parent[k][v] < 0)
        parent[k + 1][v] = -1;
      else
        parent[k + 1][v] = parent[k][parent[k][v]];
    }
  }
}
// u??¨v???LCA????±???????
int lca(int u, int v) {
  // u??¨v?????±??????????????????????????§????????????
  // u??¨v??????????????????????????????????????????????????????????????????
  if (depth[u] > depth[v])
    swap(u, v);
  for (int k = 0; k < MAX_LOGV; k++) {
    if (((depth[v] - depth[u]) >> k) & 1)
      v = parent[k][v];
  }
  // u=v??????????????????LCA??§????????????v?????????
  if (u == v)
    return v;
  //???????????????????????¢?´¢??§LCA????±???????
  // u,v???????????????????????§??????
  for (int k = MAX_LOGV - 1; k >= 0; k--) {
    if (parent[k][u] != parent[k][v]) {
      u = parent[k][u];
      v = parent[k][v];
    }
  }
  return parent[0][u];
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PI 4 * atan(1.0)
typedef long long ll;
typedef long double ld;
using namespace std;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
const int SIZE = 100001;
// int arr[SIZE]={};
//???????????§???????????¬
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;
struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};
struct Query {
  int u, v, w;
  Query(int u, int v) : u(u), v(v), w(-1) {}
};
void visit(const Graph &g, int u, int w, vector<Query> &qs, vector<int> &color,
           vector<int> &ancestor, UnionFind &uf) {
  ancestor[uf.root(u)] = u;
  FOR(e, g[u]) if (e->dst != w) {
    visit(g, e->dst, u, qs, color, ancestor, uf);
    uf.unionSet(e->src, e->dst);
    ancestor[uf.root(u)] = u;
  }
  color[u] = 1;
  FOR(q, qs) {
    int w = (q->v == u ? q->u : q->u == u ? q->v : -1);
    if (w >= 0 && color[w])
      q->w = ancestor[uf.root(w)];
  }
}
void leastCommonAncestor(const Graph &g, int r, vector<Query> &qs) {
  UnionFind uf(g.size());
  vector<int> color(g.size()), ancestor(g.size());
  visit(g, r, -1, qs, color, ancestor, uf);
}
int main() {
  int N;
  cin >> N;
  Graph G;
  for (int i = 0; i < 2 * N; i++) {
    Edges E;
    G.push_back(E);
  }
  for (int i = 0; i < N; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int l;
      cin >> l;
      G[i].push_back(Edge(i, l, 1));
      G[l].push_back(Edge(l, i, 1));
    }
  }
  int Q;
  cin >> Q;
  vector<Query> VQ;
  for (int i = 0; i < Q; i++) {
    int a, b;
    cin >> a >> b;
    VQ.push_back(Query(a, b));
  }

  leastCommonAncestor(G, 0, VQ);

  for (int i = 0; i < Q; i++) {
    cout << VQ[i].w << endl;
  }
  return 0;
}