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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

#define LL long long
#define VI vector<int>
#define VL vector<long long>
#define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define RFOR(i, a) for (int i = (a); i >= 0; --i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

using namespace std;

const int MAX_V = 100000;
const int MAX_LOG_V = 20;
// ??\\???
vector<int> G[MAX_V]; // ??°???????????£??\\???????????¨???
int root = 0;         // ?????????????????????
int parent
    [MAX_LOG_V]
    [MAX_V]; // ??????2^k????????£?????°???????????????(???????????????????????´??????-1??¨??????)
int depth[MAX_V]; // ??????????????±???

void dfs(int v, int p, int d) {
  parent[0][v] = p;
  depth[v] = d;
  for (int i = 0; i < G[v].size(); i++) {
    if (G[v][i] != p)
      dfs(G[v][i], v, d + 1);
  }
}

// ?????????
void init(int V) {
  // parent[0]??¨depth??????????????????
  dfs(root, -1, 0);
  // parent??????????????????
  for (int k = 0; k + 1 < MAX_LOG_V; 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?????±??????????????????????????§????????????
  if (depth[u] > depth[v])
    swap(u, v);
  for (int k = 0; k < MAX_LOG_V; k++) {
    if ((depth[v] - depth[u]) >> k & 1) {
      v = parent[k][v];
    }
  }
  if (u == v)
    return u;
  // ????????¢?´¢??§LCA????±???????
  for (int k = MAX_LOG_V - 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 argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, k, c;
  cin >> n;

  FOR(i, 0, n) {
    cin >> k;
    FOR(j, 0, k) {
      cin >> c;
      G[i].push_back(c);
      G[c].push_back(i);
    }
  }
  init(n);

  int q, u, v;
  cin >> q;
  FOR(i, 0, q) {
    cin >> u >> v;
    cout << lca(u, v) << "\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

#define LL long long
#define VI vector<int>
#define VL vector<long long>
#define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define RFOR(i, a) for (int i = (a); i >= 0; --i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

using namespace std;

struct Edge {
  int src, dst, weight;
  Edge(int src, int dst, int weight) : src(src), dst(dst), weight(weight) {}
};

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 vector<vector<Edge>> &g, int u, int w, vector<Query> &qs,
           vector<int> &color, vector<int> &ancestor, UnionFind &uf) {
  ancestor[uf.root(u)] = u;
  FOE(&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;
  FOE(&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 vector<vector<Edge>> &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 argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, k, c;
  cin >> n;
  vector<vector<Edge>> graph(n, vector<Edge>());
  FOR(i, 0, n) {
    cin >> k;
    FOR(j, 0, k) {
      cin >> c;
      graph[i].push_back(Edge(i, c, 1));
      graph[c].push_back(Edge(c, i, 1));
    }
  }

  int q, u, v;
  cin >> q;
  vector<Query> queries;
  FOR(i, 0, q) {
    cin >> u >> v;
    queries.push_back(Query(u, v));
  }

  leastCommonAncestor(graph, 0, queries);
  FOE(q, queries) { cout << q.w << endl; }

  return 0;
}