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

#include <cstdio>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;
int N, Q;
char buf[9];
int main() {
  scanf("%d%d", &N, &Q);
  vector<int> tp(Q), a(Q), b(Q), c(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%s%d%d", &buf, &a[i], &b[i]);
    a[i]--, b[i]--;
    if (buf[0] == 'C')
      tp[i] = 1;
    if (tp[i] == 0)
      scanf("%d", &c[i]);
  }
  vector<int> x(N);
  vector<bool> vis(N);
  vector<vector<int>> g(N);
  vector<int> r(N);
  for (int i = 0; i < N; i++)
    g[i] = vector<int>({i}), r[i] = i;
  for (int i = 0; i < Q; i++) {
    if (tp[i] == 0) {
      if (!vis[b[i]]) {
        x[a[i]] += c[i];
        x[b[i]] = x[a[i]] + c[i];
        r[b[i]] = r[a[i]];
        g[r[a[i]]].push_back(b[i]);
        g[b[i]].clear();
      } else if (!vis[a[i]] && vis[b[i]]) {
        x[b[i]] += c[i];
        x[a[i]] = x[b[i]] - c[i];
        r[a[i]] = r[b[i]];
        g[r[b[i]]].push_back(a[i]);
        g[a[i]].clear();
      } else {
        if (r[a[i]] != r[b[i]]) {
          int z1 = r[a[i]], z2 = r[b[i]], e = x[a[i]] - x[b[i]] + c[i];
          if (g[z1].size() < g[z2].size())
            swap(z1, z2), e = -e;
          g[z1].insert(g[z1].end(), g[z2].begin(), g[z2].end());
          for (int i : g[z2])
            x[i] += e, r[i] = r[z1];
          g[z2].clear();
        }
        x[a[i]] += c[i];
        x[b[i]] += c[i];
      }
      vis[a[i]] = vis[b[i]] = true;
    } else {
      if (r[a[i]] != r[b[i]])
        puts("WARNING");
      else
        printf("%d\\n", x[b[i]] - x[a[i]]);
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;

class UnionFind {
private:
  unsigned size_;
  std::vector<unsigned> par, rank;

public:
  UnionFind()
      : size_(0), par(std::vector<unsigned>()), rank(std::vector<unsigned>()){};
  UnionFind(unsigned size__) : size_(size__) {
    par.resize(size_);
    rank.resize(size_);
    for (unsigned i = 0; i < size_; i++)
      par[i] = i, rank[i] = 0;
  }
  unsigned size() { return size_; }
  unsigned root(unsigned x) { return par[x] == x ? x : par[x] = root(par[x]); }
  bool same(unsigned x, unsigned y) { return root(x) == root(y); }
  void unite(unsigned x, unsigned y) {
    x = root(x), y = root(y);
    if (x == y)
      return;
    if (rank[x] < rank[y])
      par[x] = y;
    else if (rank[x] == rank[y])
      par[y] = x, rank[x]++;
    else
      par[y] = x;
  }
  bool operator==(const UnionFind &u) { return par == u.par; }
  bool operator!=(const UnionFind &u) { return par != u.par; }
};

int N, Q;
char buf[9];
int main() {
  scanf("%d%d", &N, &Q);
  vector<int> tp(Q), a(Q), b(Q), c(Q, -1);
  for (int i = 0; i < Q; i++) {
    scanf("%s%d%d", &buf, &a[i], &b[i]);
    a[i]--, b[i]--;
    if (buf[0] == 'C')
      tp[i] = 1;
    if (tp[i] == 0)
      scanf("%d", &c[i]);
  }
  vector<int> x(N);
  vector<bool> vis(N);
  vector<vector<int>> g(N);
  for (int i = 0; i < N; i++)
    g[i] = vector<int>({i});
  UnionFind uf(N);
  for (int i = 0; i < Q; i++) {
    if (tp[i] == 0) {
      if (uf.same(a[i], b[i])) {
        x[a[i]] += c[i];
        x[b[i]] += c[i];
      } else {
        if (!vis[b[i]]) {
          x[a[i]] += c[i];
          x[b[i]] = x[a[i]] + c[i];
        }
        if (!vis[a[i]] && vis[b[i]]) {
          x[b[i]] += c[i];
          x[a[i]] = x[b[i]] - c[i];
        }
        if (vis[a[i]] && vis[b[i]]) {
          int e = c[i];
          if (g[a[i]].size() < g[b[i]].size())
            swap(a[i], b[i]), e = -e;
          g[a[i]].insert(g[a[i]].end(), g[b[i]].begin(), g[b[i]].end());
          for (int i : g[b[i]])
            x[i] += e;
          g[b[i]].clear();
          x[a[i]] += c[i];
          x[b[i]] += c[i];
        }
        vis[a[i]] = vis[b[i]] = true;
        uf.unite(a[i], b[i]);
      }
    } else {
      if (!uf.same(a[i], b[i]))
        puts("WARNING");
      else
        printf("%d\\n", x[b[i]] - x[a[i]]);
    }
  }
  return 0;
}