Problem p03452 (C++)

Buggy submission (status: Runtime Error):
/*
 * Created by KeigoOgawa
 */

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>

#define INF (int)1e8
#define EPS 1e-10
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define MIN(a, b) (a > b ? b : a)
#define MAX(a, b) (a > b ? a : b)
#define debug(x) cerr << #x << ": " << x << endl
#define all(a) (a).begin(), (a).end()

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int, int> PII;

int N, M;

int UF[110000];
int dis[110000];
pair<int, int> FIND(int a) {
  if (UF[a] < 0)
    return {a, 0};
  pair<int, int> par = FIND(UF[a]);
  dis[a] += par.second;
  UF[a] = par.first;
  return {par.first, dis[a]};
}

bool check(int l, int r, int d) {
  pair<int, int> L = FIND(l);
  pair<int, int> R = FIND(r);
  if (L.first == R.first) {
    if (L.second + d != R.second) {
      return false;
    }
  }
  UF[R.first] = L.first;
  dis[R.first] = d + L.second - R.second;
  return true;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> M;

  REP(i, N) { UF[i] = -1; }
  REP(i, M) {
    int l, r, d;
    cin >> l >> r >> d;
    l--;
    r--;

    bool res = check(l, r, d);
    if (!res) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;

  return 0;
}


Fixed submission:
/*
 * Created by KeigoOgawa
 */

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>

#define INF (int)1e8
#define EPS 1e-10
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define MIN(a, b) (a > b ? b : a)
#define MAX(a, b) (a > b ? a : b)
#define debug(x) cerr << #x << ": " << x << endl
#define all(a) (a).begin(), (a).end()

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int, int> PII;

int N, M;

int UF[110000];
int dis[110000];
pair<int, int> FIND(int a) {
  if (UF[a] < 0)
    return {a, 0};
  pair<int, int> par = FIND(UF[a]);
  dis[a] += par.second;
  UF[a] = par.first;
  return {par.first, dis[a]};
}

bool check(int l, int r, int d) {
  pair<int, int> L = FIND(l);
  pair<int, int> R = FIND(r);
  if (L.first == R.first) {
    if (L.second + d != R.second) {
      return false;
    } else {
      return true;
    }
  }
  UF[R.first] = L.first;
  dis[R.first] = d + L.second - R.second;
  return true;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> M;

  REP(i, N) { UF[i] = -1; }
  REP(i, M) {
    int l, r, d;
    cin >> l >> r >> d;
    l--;
    r--;

    bool res = check(l, r, d);
    if (!res) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;

  return 0;
}