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

#include <bits/stdc++.h>
using namespace std;

#define int long long
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
template <class T, class U> inline void chmin(T &t, U f) {
  if (t > f)
    t = f;
}
template <class T, class U> inline void chmax(T &t, U f) {
  if (t < f)
    t = f;
}

const int INF = 1001001001;
int N, M, K, Q;
vpint G[100000];
int dist[100000];

int par[100000];
set<int> querys[100000];

int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); }

int ans[100000];

signed main() {
  scanf("%lld%lld%lld%lld", &N, &M, &K, &Q);
  rep(i, M) {
    int a, b, c;
    scanf("%lld%lld%lld", &a, &b, &c);
    a--;
    b--;
    G[a].pb(pint(b, c));
    G[b].pb(pint(a, c));
  }

  fill_n(dist, N, INF);
  priority_queue<pint, vector<pint>, greater<pint>> que;
  rep(i, K) {
    int v;
    scanf("%lld", &v);
    v--;
    dist[v] = 0;
    que.push(pint(0, v));
  }

  while (que.size()) {
    int c, v;
    tie(c, v) = que.top();
    que.pop();
    if (dist[v] < c)
      continue;
    for (auto p : G[v]) {
      if (dist[p.fi] <= c + p.se)
        continue;
      dist[p.fi] = c + p.se;
      que.push(pint(dist[p.fi], p.fi));
    }
  }

  vector<tuple<int, int, int>> edges;

  rep(i, N) {
    par[i] = i;
    for (auto p : G[i]) {
      if (i > p.fi)
        continue;
      edges.pb(make_tuple(min(dist[i], dist[p.fi]), i, p.fi));
    }
  }

  rep(i, Q) {
    int a, b;
    scanf("%lld%lld", &a, &b);
    a--;
    b--;
    querys[a].insert(i);
    querys[b].insert(i);
  }

  sort(all(edges));
  reverse(all(edges));

  rep(i, edges.size()) {
    int a, b, c;
    tie(c, a, b) = edges[i];
    a = find(a);
    b = find(b);
    if (a == b)
      continue;
    if (querys[a].size() < querys[b].size())
      swap(querys[a], querys[b]);
    each(it, querys[b]) {
      if (querys[a].find(*it) != querys[a].end()) {
        ans[*it] = c;
        querys[a].erase(*it);
      } else {
        querys[a].insert(*it);
      }
    }
    par[b] = a;
  }

  rep(i, Q) printf("%lld\\n", ans[i]);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
template <class T, class U> inline void chmin(T &t, U f) {
  if (t > f)
    t = f;
}
template <class T, class U> inline void chmax(T &t, U f) {
  if (t < f)
    t = f;
}

struct UF {
  int N;
  vint par, rank, ti;
  void init(int n) {
    N = n;
    par.resize(N);
    rank.resize(N);
    ti.resize(N);
    rep(i, N) par[i] = i, rank[i] = 0, ti[i] = -1;
  }
  int find(int x, int t = 1145141919) {
    if (x == par[x])
      return x;
    if (ti[x] > t)
      return x;
    return find(par[x]);
  }
  void unite(int x, int y, int t) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y])
      swap(x, y);
    par[y] = x;
    ti[y] = t;
    rank[x] += rank[x] == rank[y];
  }
  bool ok(int a, int b, int t) { return find(a, t) == find(b, t); }
};

struct edge {
  int to, cost;
  edge(int to, int cost) : to(to), cost(cost) {}
};
const int SIZE = 100000;
vector<edge> G[SIZE];

int N, M, K, Q;
int A[SIZE], B[SIZE];
int dist[SIZE];
signed main() {
  scanf("%lld%lld%lld%lld", &N, &M, &K, &Q);
  rep(i, M) {
    int a, b, c;
    scanf("%lld%lld%lld", &a, &b, &c);
    a--;
    b--;
    G[a].pb(edge(b, c));
    G[b].pb(edge(a, c));
    A[i] = a;
    B[i] = b;
  }

  fill_n(dist, N, 1ll << 50);
  priority_queue<pint, vector<pint>, greater<pint>> que;
  rep(i, K) {
    int a;
    scanf("%lld", &a);
    a--;
    dist[a] = 0;
    que.push(pint(0, a));
  }
  while (que.size()) {
    pint p = que.top();
    que.pop();
    if (dist[p.se] < p.fi)
      continue;
    for (auto &e : G[p.se]) {
      if (dist[e.to] <= p.fi + e.cost)
        continue;
      dist[e.to] = p.fi + e.cost;
      que.push(pint(dist[e.to], e.to));
    }
  }

  UF uf;
  uf.init(N);
  vector<pint> vec;
  rep(i, M) vec.pb(pint(min(dist[A[i]], dist[B[i]]), i));
  sort(all(vec));
  reverse(all(vec));

  rep(i, vec.size()) {
    int j = vec[i].se;
    uf.unite(A[j], B[j], i);
  }

  while (Q--) {
    int a, b;
    scanf("%lld%lld", &a, &b);
    a--;
    b--;
    int lb = -1, ub = M - 1;
    while (ub - lb > 1) {
      int mid = (ub + lb) / 2;
      if (uf.ok(a, b, mid))
        ub = mid;
      else
        lb = mid;
    }
    int i = vec[ub].se;
    printf("%lld\\n", min(dist[A[i]], dist[B[i]]));
  }
  return 0;
}