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

#include <algorithm>
#include <iostream>
#include <vector>

// float inf = std::numeric_limits<float>::infinity();
#define INF 100000000

using namespace std;

typedef struct UnionFind_ {
  vector<int> p;
  vector<int> r;
  vector<int> diff_weight;

  UnionFind_(int n = 1) { init(n); }

  void init(int n = 1) {
    p.resize(n);
    r.resize(n);
    diff_weight.resize(n);
    for (int i = 0; i < n; ++i) {
      p[i] = i;
      r[i] = 0;
      diff_weight[i] = 0;
    }
  }

  int findSet(int x) {
    int tmp;
    if (x != p[x]) {
      tmp = findSet(p[x]);
      diff_weight[x] += diff_weight[p[x]];
      p[x] = tmp;
    }
    return p[x];
  }

  bool issame(int x, int y) { return findSet(x) == findSet(y); }

  int weight(int x) {
    findSet(x);
    return diff_weight[x];
  }

  int diff(int x, int y) {
    if (!issame(x, y))
      return INF;
    return weight(y) - weight(x);
  }

  bool merge(int x, int y, int w) {
    w += weight(x);
    w -= weight(y);

    x = findSet(x);
    y = findSet(y);
    if (x == y)
      return false;

    if (r[x] < r[y]) {
      swap(x, y);
      w = -w;
    }

    if (r[x] == r[y])
      ++r[x];
    p[y] = x;

    diff_weight[y] = w;

    return true;
  }

} UnionFind;

int main() {
  int n, q;
  int com, x, y, z;
  int count = 0;

  cin >> n >> q;
  UnionFind uf(n);
  int judge[q];

  for (int i = 0; i < q; ++i) {
    cin >> com;
    if (com == 0) {
      cin >> x >> y >> z;
      uf.merge(x, y, z);
    }

    else {
      cin >> x >> y;
      judge[count++] = uf.diff(x, y);
    }
  }

  for (int i = 0; i < count; ++i) {
    if (judge[i] >= INF)
      cout << "?" << endl;
    else
      cout << judge[i] << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef struct UnionFind_ {
  vector<int> p;
  vector<int> r;
  vector<int> diff_weight;

  UnionFind_(int n = 1) { init(n); }

  void init(int n = 1) {
    p.resize(n);
    r.resize(n);
    diff_weight.resize(n);
    for (int i = 0; i < n; ++i) {
      p[i] = i;
      r[i] = 0;
      diff_weight[i] = 0;
    }
  }

  int findSet(int x) {
    int tmp;
    if (x != p[x]) {
      tmp = findSet(p[x]);
      diff_weight[x] += diff_weight[p[x]];
      p[x] = tmp;
    }
    return p[x];
  }

  bool issame(int x, int y) { return findSet(x) == findSet(y); }

  int weight(int x) {
    findSet(x);
    return diff_weight[x];
  }

  int diff(int x, int y) {
    if (!issame(x, y))
      return 0;
    return weight(y) - weight(x);
  }

  bool merge(int x, int y, int w) {
    w += weight(x);
    w -= weight(y);

    x = findSet(x);
    y = findSet(y);
    if (x == y)
      return false;

    if (r[x] < r[y]) {
      swap(x, y);
      w = -w;
    }

    if (r[x] == r[y])
      ++r[x];
    p[y] = x;

    diff_weight[y] = w;

    return true;
  }

} UnionFind;

int main() {
  int n, q;
  int com, x, y, z;
  int count = 0, judge[100000];

  cin >> n >> q;
  UnionFind uf(n);

  for (int i = 0; i < q; ++i) {
    cin >> com;
    if (com == 0) {
      cin >> x >> y >> z;
      uf.merge(x, y, z);
    }

    else {
      cin >> x >> y;
      judge[count++] = uf.diff(x, y);
    }
  }

  for (int i = 0; i < count; ++i) {
    if (judge[i] == 0)
      cout << "?" << endl;
    else
      cout << judge[i] << endl;
  }

  return 0;
}