Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
const int MOD = 1000000007;
typedef long long ll;
typedef pair<ll, ll> p;
const int INF = (1 << 28);
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
// setprecision(15)有効数字15桁
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
struct edge {
  int to, cost;
};
int n, m;
vector<bool> used(100000, false);
vector<vector<pair<int, int>>> g(100000);
vector<int> score(100000);
bool dfs(int n) {
  used[n] = true;
  for (int i = 0; i < g[n].size(); i++) {
    if (!used[g[n][i].first]) {
      score[g[n][i].first] = g[n][i].second + score[n];
      if (!dfs(g[n][i].first))
        return false;
    } else {
      if (score[g[n][i].first] != g[n][i].second + score[n]) {
        return false;
      } else {
      }
    }
  }
  return true;
}
const int MAX_N = 100000;
int par[MAX_N];   // 親 **親を探すときはfind
int rank1[MAX_N]; // 木の深さ
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rank1[i] = 0;
  }
}
// 木の根を求める
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}
// 併合
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (rank1[x] < rank1[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rank1[x] == rank1[y])
      rank1[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -par[find(x)]; }
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  vector<bool> usedr(n, false);
  init(n);
  for (int i = 0; i < m; i++) {
    score[i] = 0;
    int l, r, d;
    cin >> l >> r >> d;
    l--;
    r--;
    g[l].push_back(make_pair(r, d));
    g[r].push_back(make_pair(l, -d));
    unite(l, r);
  }
  for (int i = 0; i < n; i++) {
    if (find(i) == i) {
      if (!dfs(i))
        return no, 0;
    }
  }
  yes;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
const int MOD = 1000000007;
typedef long long ll;
typedef pair<ll, ll> p;
const int INF = (1 << 28);
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
// setprecision(15)有効数字15桁
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
struct edge {
  int to, cost;
};
int n, m;
vector<bool> used(1000000, false);
vector<vector<pair<int, int>>> g(1000000);
vector<int> score(1000000);
bool dfs(int n) {
  used[n] = true;
  for (int i = 0; i < g[n].size(); i++) {
    if (!used[g[n][i].first]) {
      score[g[n][i].first] = g[n][i].second + score[n];
      if (!dfs(g[n][i].first))
        return false;
    } else {
      if (score[g[n][i].first] != g[n][i].second + score[n]) {
        return false;
      } else {
      }
    }
  }
  return true;
}
const int MAX_N = 100000;
int par[MAX_N];   // 親 **親を探すときはfind
int rank1[MAX_N]; // 木の深さ
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rank1[i] = 0;
  }
}
// 木の根を求める
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}
// 併合
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (rank1[x] < rank1[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rank1[x] == rank1[y])
      rank1[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
int size(int x) { return -par[find(x)]; }
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  vector<bool> usedr(n, false);
  init(n);
  for (int i = 0; i < m; i++) {
    score[i] = 0;
    int l, r, d;
    cin >> l >> r >> d;
    l--;
    r--;
    g[l].push_back(make_pair(r, d));
    g[r].push_back(make_pair(l, -d));
    unite(l, r);
  }
  for (int i = 0; i < n; i++) {
    if (find(i) == i) {
      if (!dfs(i))
        return no, 0;
    }
  }
  yes;
}