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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define rforeach(it, c)                                                        \\
  for (__typeof__((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CL(arr, val) memset(arr, val, sizeof(arr))
#define COPY(dest, src) memcpy(dest, src, sizeof(dest))
#define ten(n) ((long long)(1e##n))
#define bin(n) (1ull << (n))
#define erep(i, n) for (int i = 0; i <= (int)(n); ++i)
#define revrep(i, n) for (int i = (n); i >= 0; --i)
#define pb push_back

template <class T> void chmax(T &a, const T &b) { a = max(a, b); }
template <class T> void chmin(T &a, const T &b) { a = min(a, b); }

template <class T> T round_up(T a, T b) { return (a + b - 1) / b; }

template <class T> void uniq(vector<T> &c) {
  sort(c.begin(), c.end());
  c.erase(unique(c.begin(), c.end()), c.end());
}

template <class T> string to_s(const T &a) {
  ostringstream os;
  os << a;
  return os.str();
}
template <class T> T to_T(const string &s) {
  istringstream is(s);
  T res;
  is >> res;
  return res;
}

template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> &p) {
  os << "( " << p.first << ", " << p.second << " )";
  return os;
}

template <class T>
void print(T a, int n, const string &deli = " ", int br = 1) {
  for (int i = 0; i < n; ++i) {
    cout << a[i];
    if (i + 1 != n)
      cout << deli;
  }
  while (br--)
    cout << endl;
}
template <class T>
void print(const T &c, const string &deli = " ", int br = 1) {
  foreach (it, c) {
    cout << *it;
    if (++it != c.end())
      cout << deli;
    --it;
  }
  while (br--)
    cout << endl;
}
template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) {
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (width != -1)
        cout.width(width);
      cout << a[i][j] << ' ';
    }
    cout << endl;
  }
  while (br--)
    cout << endl;
}

template <class T> void input(T &a, int n) {
  for (int i = 0; i < n; ++i)
    cin >> a[i];
}
template <class T> void input(T *a, int n) {
  for (int i = 0; i < n; ++i)
    cin >> a[i];
}

void fix_pre(int n) {
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(10);
}
void fast_io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
#define trace(x) (cout << #x << ": " << (x) << endl)

bool in_rect(int x, int y, int w, int h) {
  return 0 <= x && x < w && 0 <= y && y < h;
}

typedef long long ll;
typedef pair<int, int> pint;

// y(v): v>^<  y(^): ^>v<
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

const double PI = acos(-1.0);

#define F first
#define S second

class UnionFind {
private:
  vector<int> data;
  int _groups;

public:
  int n;
  UnionFind(int n) : data(n, -1), _groups(n), n(n) {}

  void unite(int x, int y) {
    x = root(x), y = root(y);
    if (x != y) {
      --_groups;
      // AOJ 0575のための修正
      // xのrootが常にrootになるように修正した
      // if (data[x] > data[y])
      //     swap(x, y);

      data[x] += data[y];
      data[y] = x;
    }
  }
  bool same(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)]; }
  int groups() const { return _groups; }
};
// LCA(Lowest Common Ancestor)
class LCA {
  friend class LCABuilder;
  friend LCA build_lca(vector<vector<int>> &g, int root);

private:
  int max_log;

  LCA(vector<int> &depth, vector<vector<int>> &parent, int max_log)
      : depth(depth), parent(parent), max_log(max_log) {}

public:
  vector<int> depth;
  vector<vector<int>> parent;

  int lca(int u, int v) {
    if (depth[u] > depth[v])
      swap(u, v);

    for (int k = 0; k < max_log; ++k)
      if ((depth[v] - depth[u]) >> k & 1)
        v = parent[k][v];

    if (u == v)
      return u;

    for (int k = max_log - 1; k >= 0; --k) {
      if (parent[k][u] != parent[k][v]) {
        u = parent[k][u];
        v = parent[k][v];
      }
    }

    return parent[0][u];
  }
};
class LCABuilder {
  friend LCA build_lca(vector<vector<int>> &g, int root);

private:
  int root, n;
  vector<vector<int>> &g;

  int max_log;
  vector<int> depth;
  vector<vector<int>> parent;

  LCABuilder(vector<vector<int>> &graph, int root)
      : g(graph), n(graph.size()), depth(n), root(root) {
    max_log = 0;
    for (int i = 1; i <= n; i *= 2)
      ++max_log;
    parent = vector<vector<int>>(max_log, vector<int>(n));

    init();
  }

  void calc_depth(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)
        calc_depth(g[v][i], v, d + 1);
  }

  void init() {
    calc_depth(root, -1, 0);

    for (int k = 0; k + 1 < max_log; ++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]];
      }
    }
  }
};
LCA build_lca(vector<vector<int>> &g, int root) {
  LCABuilder builder(g, root);
  return LCA(builder.depth, builder.parent, builder.max_log);
}
vector<int> dijkstra(const vector<vector<pint>> &g, const vector<int> &s) {
  const int _INF = 2 * ten(9);
  typedef pair<int, int> pint;

  vector<int> dis(g.size(), _INF);
  priority_queue<pint, vector<pint>, greater<pint>> q;

  for (int i = 0; i < (int)s.size(); ++i) {
    dis[s[i]] = 0;
    q.push(pint(0, s[i]));
  }
  while (!q.empty()) {
    pint t = q.top();
    q.pop();
    int cur = t.second, d = t.first;

    if (d > dis[cur])
      continue;

    for (int i = 0; i < (int)g[cur].size(); ++i) {
      const pint &e = g[cur][i];
      int to = e.first;
      int next_d = d + e.second;
      if (next_d < dis[to]) {
        dis[to] = next_d;
        q.push(pint(next_d, to));
      }
    }
  }

  return dis;
}
int main() {
  int n, m, k, Q;
  scanf("%d%d%d%d", &n, &m, &k, &Q);

  vector<vector<pint>> g(n);
  while (m--) {
    int a, b, l;
    scanf("%d%d%d", &a, &b, &l);
    --a, --b;
    g[a].pb(pint(b, l));
    g[b].pb(pint(a, l));
  }

  vector<int> start(k);
  rep(i, k) {
    int f;
    scanf("%d", &f);
    start[i] = f - 1;
  }

  vector<int> near_dis = dijkstra(g, start);
  vector<pint> city; // (near_dis, no)
  rep(i, n) city.pb(pint(near_dis[i], i));
  sort(all(city), greater<pint>());

  UnionFind uf(n);
  static int par[ten(5)];
  fill_n(par, n, -1);
  vector<vector<int>> tree(n); // 子ノードのほうがnear_disが大きいtree
  rep(i, n) {
    int d = city[i].first;
    int v = city[i].second;

    rep(j, g[v].size()) {
      int to = g[v][j].first;
      int root_to = uf.root(to);
      if (d <= near_dis[to] && !uf.same(v, to)) {
        if (d == near_dis[to] && v > to)
          continue;

        int root_v = uf.root(v);
        par[root_to] = root_v;
        uf.unite(v, to);
      }
    }
  }
  assert(uf.groups() == 1);
  rep(i, n) if (par[i] != -1) tree[par[i]].pb(i);

  LCA lca = build_lca(tree, city.back().second);
  while (Q--) {
    int s, t;
    scanf("%d%d", &s, &t);
    --s, --t;

    printf("%d\\n", near_dis[lca.lca(s, t)]);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define rforeach(it, c)                                                        \\
  for (__typeof__((c).rbegin()) it = (c).rbegin(); it != (c).rend(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CL(arr, val) memset(arr, val, sizeof(arr))
#define COPY(dest, src) memcpy(dest, src, sizeof(dest))
#define ten(n) ((long long)(1e##n))
#define bin(n) (1ull << (n))
#define erep(i, n) for (int i = 0; i <= (int)(n); ++i)
#define revrep(i, n) for (int i = (n); i >= 0; --i)
#define pb push_back

template <class T> void chmax(T &a, const T &b) { a = max(a, b); }
template <class T> void chmin(T &a, const T &b) { a = min(a, b); }

template <class T> T round_up(T a, T b) { return (a + b - 1) / b; }

template <class T> void uniq(vector<T> &c) {
  sort(c.begin(), c.end());
  c.erase(unique(c.begin(), c.end()), c.end());
}

template <class T> string to_s(const T &a) {
  ostringstream os;
  os << a;
  return os.str();
}
template <class T> T to_T(const string &s) {
  istringstream is(s);
  T res;
  is >> res;
  return res;
}

template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> &p) {
  os << "( " << p.first << ", " << p.second << " )";
  return os;
}

template <class T>
void print(T a, int n, const string &deli = " ", int br = 1) {
  for (int i = 0; i < n; ++i) {
    cout << a[i];
    if (i + 1 != n)
      cout << deli;
  }
  while (br--)
    cout << endl;
}
template <class T>
void print(const T &c, const string &deli = " ", int br = 1) {
  foreach (it, c) {
    cout << *it;
    if (++it != c.end())
      cout << deli;
    --it;
  }
  while (br--)
    cout << endl;
}
template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) {
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (width != -1)
        cout.width(width);
      cout << a[i][j] << ' ';
    }
    cout << endl;
  }
  while (br--)
    cout << endl;
}

template <class T> void input(T &a, int n) {
  for (int i = 0; i < n; ++i)
    cin >> a[i];
}
template <class T> void input(T *a, int n) {
  for (int i = 0; i < n; ++i)
    cin >> a[i];
}

void fix_pre(int n) {
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(10);
}
void fast_io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
#define trace(x) (cout << #x << ": " << (x) << endl)

bool in_rect(int x, int y, int w, int h) {
  return 0 <= x && x < w && 0 <= y && y < h;
}

typedef long long ll;
typedef pair<int, int> pint;

// y(v): v>^<  y(^): ^>v<
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

const double PI = acos(-1.0);

#define F first
#define S second

class UnionFind {
private:
  vector<int> data;
  int _groups;

public:
  int n;
  UnionFind(int n) : data(n, -1), _groups(n), n(n) {}

  void unite(int x, int y) {
    x = root(x), y = root(y);
    if (x != y) {
      --_groups;
      // AOJ 0575のための修正
      // xのrootが常にrootになるように修正した
      // if (data[x] > data[y])
      //     swap(x, y);

      data[x] += data[y];
      data[y] = x;
    }
  }
  bool same(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)]; }
  int groups() const { return _groups; }
};
// LCA(Lowest Common Ancestor)
class LCA {
  friend class LCABuilder;
  friend LCA build_lca(vector<vector<int>> &g, int root);

private:
  int max_log;

  LCA(vector<int> &depth, vector<vector<int>> &parent, int max_log)
      : depth(depth), parent(parent), max_log(max_log) {}

public:
  vector<int> depth;
  vector<vector<int>> parent;

  int lca(int u, int v) {
    if (depth[u] > depth[v])
      swap(u, v);

    for (int k = 0; k < max_log; ++k)
      if ((depth[v] - depth[u]) >> k & 1)
        v = parent[k][v];

    if (u == v)
      return u;

    for (int k = max_log - 1; k >= 0; --k) {
      if (parent[k][u] != parent[k][v]) {
        u = parent[k][u];
        v = parent[k][v];
      }
    }

    return parent[0][u];
  }
};
class LCABuilder {
  friend LCA build_lca(vector<vector<int>> &g, int root);

private:
  int root, n;
  vector<vector<int>> &g;

  int max_log;
  vector<int> depth;
  vector<vector<int>> parent;

  LCABuilder(vector<vector<int>> &graph, int root)
      : g(graph), n(graph.size()), depth(n), root(root) {
    max_log = 0;
    for (int i = 1; i <= n; i *= 2)
      ++max_log;
    parent = vector<vector<int>>(max_log, vector<int>(n));

    init();
  }

  void calc_depth(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)
        calc_depth(g[v][i], v, d + 1);
  }

  void init() {
    calc_depth(root, -1, 0);

    for (int k = 0; k + 1 < max_log; ++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]];
      }
    }
  }
};
LCA build_lca(vector<vector<int>> &g, int root) {
  LCABuilder builder(g, root);
  return LCA(builder.depth, builder.parent, builder.max_log);
}
vector<int> dijkstra(const vector<vector<pint>> &g, const vector<int> &s) {
  const int _INF = 1e9;
  typedef pair<int, int> pint;

  vector<int> dis(g.size(), _INF);
  priority_queue<pint, vector<pint>, greater<pint>> q;

  for (int i = 0; i < (int)s.size(); ++i) {
    dis[s[i]] = 0;
    q.push(pint(0, s[i]));
  }
  while (!q.empty()) {
    pint t = q.top();
    q.pop();
    int cur = t.second, d = t.first;

    if (d > dis[cur])
      continue;

    for (int i = 0; i < (int)g[cur].size(); ++i) {
      const pint &e = g[cur][i];
      int to = e.first;
      int next_d = d + e.second;
      if (next_d < dis[to]) {
        dis[to] = next_d;
        q.push(pint(next_d, to));
      }
    }
  }

  return dis;
}
int main() {
  int n, m, k, Q;
  scanf("%d%d%d%d", &n, &m, &k, &Q);

  vector<vector<pint>> g(n);
  while (m--) {
    int a, b, l;
    scanf("%d%d%d", &a, &b, &l);
    --a, --b;
    g[a].pb(pint(b, l));
    g[b].pb(pint(a, l));
  }

  vector<int> start(k);
  rep(i, k) {
    int f;
    scanf("%d", &f);
    start[i] = f - 1;
  }

  vector<int> near_dis = dijkstra(g, start);
  vector<pint> city; // (near_dis, no)
  rep(i, n) city.pb(pint(near_dis[i], i));
  sort(all(city), greater<pint>());

  UnionFind uf(n);
  vector<vector<int>> tree(n); // 子ノードのほうがnear_disが大きいtree
  static int highest_node[ten(5)];
  rep(i, n) highest_node[uf.root(i)] = i;
  rep(i, n) {
    int d = city[i].first;
    int v = city[i].second;

    rep(j, g[v].size()) {
      int to = g[v][j].first;
      if (d < near_dis[to] && !uf.same(v, to)) {
        int to_root = uf.root(to);
        tree[v].pb(highest_node[to_root]);

        uf.unite(to, v);
        highest_node[to_root] = v;
        assert(uf.root(v) == to_root);
      }
    }
  }

  assert(uf.groups() == 1);

  LCA lca = build_lca(tree, city.back().second);
  while (Q--) {
    int s, t;
    scanf("%d%d", &s, &t);
    --s, --t;

    printf("%d\\n", near_dis[lca.lca(s, t)]);
  }
}