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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reg(i, a, b) for (int i = (a); i <= (b); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = (b); i >= (a); i--)
typedef long long int lli;
typedef pair<int, int> mp;
typedef pair<int, mp> mmp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX

int n, m, fn, qn;

set<int> sets[100005];

struct unifo {
  int uni[100010];
  void init(int n) { rep(i, n) uni[i] = i; }
  int find(int p) {
    if (uni[p] == p)
      return p;
    else
      return (uni[p] = find(uni[p]));
  }
  void mer(int p, int q) {
    if (!same(p, q))
      uni[find(p)] = find(q);
  }
  bool same(int p, int q) { return find(p) == find(q); }
};

unifo uni;

vector<mp> vs[100005];
mmp es[200005];
int dist[100005];

priority_queue<mp, vector<mp>, greater<mp>> que;

void dijkstra() {
  reg(i, 1, n) dist[i] = IINF;

  while (!que.empty()) {
    mp pa = que.top();
    que.pop();
    int no = pa.sec, co = pa.fir;
    if (dist[no] <= co)
      continue;
    dist[no] = co;
    rep(i, vs[no].size()) {
      mp pb = vs[no][i];
      int to = pb.fir, tc = co + pb.sec;
      if (dist[to] <= tc)
        continue;
      que.push(mp(tc, to));
    }
  }
}

int anss[100005];

int main(void) {

  scanf("%d%d%d%d", &n, &m, &fn, &qn);
  uni.init(n + 3);

  rep(i, m) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    vs[a].push_back(mp(b, c));
    vs[b].push_back(mp(a, c));
    es[i] = mmp(-1, mp(a, b));
  }

  rep(i, fn) {
    int a;
    scanf("%d", &a);
    que.push(mp(0, a));
  }

  rep(i, qn) {
    int a, b;
    scanf("%d%d", &a, &b);
    sets[a].insert(i);
    sets[b].insert(i);
  }

  dijkstra();

  /*
  reg(i,1,n){
          printf("dist .. %d\\n",dist[i]);
  }*/

  rep(i, m) {
    mmp &pa = es[i];
    pa.fir = min(dist[pa.sec.fir], dist[pa.sec.sec]);
    // printf("edgs .. %d %d %d\\n",pa.sec.fir,pa.sec.sec,pa.fir);
  }

  sort(es, es + m, greater<mmp>());

  rep(i, m) {
    mmp pa = es[i];
    int a = es[i].sec.fir, b = es[i].sec.sec, d = es[i].fir;
    // printf("edg .. %d %d %d\\n",a,b,d);
    a = uni.find(a);
    b = uni.find(b);
    if (uni.same(a, b))
      continue;

    set<int> sa, sb;
    swap(sa, sets[uni.find(a)]);
    swap(sb, sets[uni.find(b)]);

    if (sa.size() > sb.size())
      swap(sa, sb);

    set<int>::iterator ite;

    for (ite = sa.begin(); ite != sa.end(); ite++) {
      int no = *ite;
      // printf("set .. %d no .. %d\\n",a,no);
      if (sb.find(no) != sb.end()) {
        // printf("merge %d %d %d\\n",a,b,no);
        sb.erase(no);
        anss[no] = d;
      } else
        sb.insert(no);
    }

    uni.mer(a, b);
    swap(sb, sets[uni.find(a)]);
  }

  rep(i, qn) { printf("%d\\n", anss[i]); }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reg(i, a, b) for (int i = (a); i <= (b); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = (b); i >= (a); i--)
typedef long long int lli;
typedef pair<int, int> mp;
typedef pair<int, mp> mmp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX

int n, m, fn, qn;

set<int> sets[100005];

struct unifo {
  int uni[100005];
  void init(int n) { rep(i, n) uni[i] = i; }
  int find(int p) {
    if (uni[p] == p)
      return p;
    else
      return (uni[p] = find(uni[p]));
  }
  void mer(int p, int q) {
    if (!same(p, q))
      uni[find(p)] = find(q);
  }
  bool same(int p, int q) { return find(p) == find(q); }
};

unifo uni;

vector<mp> vs[100005];
mmp es[100005];
int dist[100005];

priority_queue<mp, vector<mp>, greater<mp>> que;

void dijkstra() {
  reg(i, 1, n) dist[i] = IINF;

  while (!que.empty()) {
    mp pa = que.top();
    que.pop();
    int no = pa.sec, co = pa.fir;
    if (dist[no] <= co)
      continue;
    dist[no] = co;
    rep(i, vs[no].size()) {
      mp pb = vs[no][i];
      int to = pb.fir, tc = co + pb.sec;
      if (dist[to] <= tc)
        continue;
      que.push(mp(tc, to));
    }
  }
}

int anss[100005];

int main(void) {
  scanf("%d%d%d%d", &n, &m, &fn, &qn);
  uni.init(n + 3);

  rep(i, m) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    vs[a].push_back(mp(b, c));
    vs[b].push_back(mp(a, c));
    es[i] = mmp(-1, mp(a, b));
  }

  rep(i, fn) {
    int a;
    scanf("%d", &a);
    que.push(mp(0, a));
  }

  rep(i, qn) {
    int a, b;
    scanf("%d%d", &a, &b);
    sets[a].insert(i);
    sets[b].insert(i);
  }

  dijkstra();

  /*
  reg(i,1,n){
          printf("dist .. %d\\n",dist[i]);
  }
  */

  rep(i, m) {
    mmp &pa = es[i];
    pa.fir = min(dist[pa.sec.fir], dist[pa.sec.sec]);
    // printf("edgs .. %d %d %d\\n",pa.sec.fir,pa.sec.sec,pa.fir);
  }

  sort(es, es + m, greater<mmp>());

  rep(i, m) {
    mmp pa = es[i];
    int a = es[i].sec.fir, b = es[i].sec.sec, d = es[i].fir;
    // printf("edg .. %d %d %d\\n",a,b,d);
    a = uni.find(a);
    b = uni.find(b);
    if (uni.same(a, b))
      continue;

    set<int> sa, sb;
    swap(sa, sets[uni.find(a)]);
    swap(sb, sets[uni.find(b)]);

    if (sa.size() > sb.size())
      swap(sa, sb);

    set<int>::iterator ite;

    for (ite = sa.begin(); ite != sa.end(); ite++) {
      int no = *ite;
      // printf("set .. %d no .. %d\\n",a,no);
      if (sb.find(no) != sb.end()) {
        // printf("merge %d %d %d\\n",a,b,no);
        sb.erase(no);
        anss[no] = d;
      } else
        sb.insert(no);
    }

    uni.mer(a, b);
    swap(sb, sets[uni.find(a)]);
  }

  rep(i, qn) { printf("%d\\n", anss[i]); }

  return 0;
}