Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define REP(i, n)                                                              \
  for (long long(i) = 0, i_bound = (long long)(n); (i) < i_bound; ++i)
#define FOR(i, a, b) for (long long(i) = (a); (i) < (b); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
const long long MOD = static_cast<long long>(1e9) + 7LL;
const long long INF = 1234567891234567890LL;

class UF {
public:
  int N;
  vector<int> par;
  vector<int> rank;
  vector<int> diff_weight;
  UF(int N = 1) {
    par.resize(N);
    diff_weight.resize(N);
    rank.resize(N);
    for (int i = 0; i < N; i++)
      par[i] = i, rank[i] = diff_weight[i] = 0;
  }
  int root(int x) {
    if (par[x] == x)
      return x;
    else {
      int r = root(par[x]);
      diff_weight[x] += diff_weight[par[x]];
      return par[x] = r;
    }
  }
  int weight(int x) {
    root(x);
    return diff_weight[x];
  }
  int diff(int x, int y) { return weight(y) - weight(x); }
  bool isSame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y, int w = 0) {
    w += weight(x);
    w -= weight(y);
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return false;
    if (rank[rx] < rank[ry])
      swap(rx, ry), w = -w;
    if (rank[rx] == rank[ry])
      ++rank[rx];
    par[ry] = rx;
    diff_weight[ry] = w;
    return true;
  }
};

signed main() {
  int N;
  cin >> N;
  UF uf(N);
  int M;
  cin >> M;
  vector<int> L(N);
  vector<int> R(N);
  vector<int> D(N);
  REP(i, M) {
    int l, r, d;
    cin >> l >> r >> d;
    L[i] = l;
    R[i] = r;
    D[i] = d;
    uf.merge(l, r, d);
  }
  bool res = true;
  REP(i, M) {
    if (uf.diff(L[i], R[i]) != D[i])
      res = false;
    if (!res)
      break;
  }
  if (res)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define REP(i, n)                                                              \
  for (long long(i) = 0, i_bound = (long long)(n); (i) < i_bound; ++i)
#define FOR(i, a, b) for (long long(i) = (a); (i) < (b); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
const long long MOD = static_cast<long long>(1e9) + 7LL;
const long long INF = 1234567891234567890LL;

class UF {
public:
  int N;
  vector<int> par;
  vector<int> rank;
  vector<int> diff_weight;
  UF(int N = 1) {
    par.resize(N);
    diff_weight.resize(N);
    rank.resize(N);
    for (int i = 0; i < N; i++)
      par[i] = i, rank[i] = diff_weight[i] = 0;
  }
  int root(int x) {
    if (par[x] == x)
      return x;
    else {
      int r = root(par[x]);
      diff_weight[x] += diff_weight[par[x]];
      return par[x] = r;
    }
  }
  int weight(int x) {
    root(x);
    return diff_weight[x];
  }
  int diff(int x, int y) { return weight(y) - weight(x); }
  bool isSame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y, int w = 0) {
    w += weight(x);
    w -= weight(y);
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return false;
    if (rank[rx] < rank[ry])
      swap(rx, ry), w = -w;
    if (rank[rx] == rank[ry])
      ++rank[rx];
    par[ry] = rx;
    diff_weight[ry] = w;
    return true;
  }
};

signed main() {
  int N;
  cin >> N;
  UF uf(N);
  int M;
  cin >> M;
  vector<int> L(M);
  vector<int> R(M);
  vector<int> D(M);
  REP(i, M) {
    int l, r, d;
    cin >> l >> r >> d;
    L[i] = l;
    R[i] = r;
    D[i] = d;
    uf.merge(l, r, d);
  }
  bool res = true;
  REP(i, M) {
    if (uf.diff(L[i], R[i]) != D[i])
      res = false;
    if (!res)
      break;
  }
  if (res)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}