Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<P> vp;
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
#define per(i, a, n) for (ll i = (a); i > (n); i--)
#define lep(i, a, n) for (ll i = (a); i <= (n); i++)
#define pel(i, a, n) for (ll i = (a); i >= (n); i--)
#define clr(a, b) memset((a), (b), sizeof(a))
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define sz size()
#define print(X) cout << (X) << endl
const ll INF = 1e+8 + 7;
ll n, m, l;
string s, t;
ll d[100010], dp[500][500];

int par[1000];
int uni_rank[1000];

void init(int n) {
  rep(i, 0, n) {
    par[i] = i;
    uni_rank[i] = 0;
  }
  return;
}

int find(int x) {
  if (par[x] == x)
    return x;
  int p = par[x];
  par[x] = find(par[x]);
  uni_rank[x] += uni_rank[p];
  return par[p];
}

bool unite(int x, int y, int p) {
  int a = find(x);
  int b = find(y);
  if (a == b) {
    if (uni_rank[x] + p != uni_rank[y])
      return false;
    return true;
  }
  // if(uni_rank[x] < uni_rank[y])par[x] = y;
  // else{
  //   par[y] = x;
  //   if(uni_rank[x] == uni_rank[y])uni_rank[x]++;
  // }
  par[b] = a;
  uni_rank[b] = uni_rank[x] + p - uni_rank[y];
  return true;
}

bool same(int x, int y) { return find(x) == find(y); }

int main() {
  cin >> n >> m;
  init(n);
  rep(i, 0, m) {
    int a, b, c;
    cin >> a >> b >> c;
    if (!unite(a, b, c)) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<P> vp;
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
#define per(i, a, n) for (ll i = (a); i > (n); i--)
#define lep(i, a, n) for (ll i = (a); i <= (n); i++)
#define pel(i, a, n) for (ll i = (a); i >= (n); i--)
#define clr(a, b) memset((a), (b), sizeof(a))
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define sz size()
#define print(X) cout << (X) << endl
const ll INF = 1e+8 + 7;
ll n, m, l;
string s, t;
ll d[100010], dp[500][500];

int par[200000];
int uni_rank[200000];

void init(int n) {
  rep(i, 0, n) {
    par[i] = i;
    uni_rank[i] = 0;
  }
  return;
}

int find(int x) {
  if (par[x] == x)
    return x;
  int p = par[x];
  par[x] = find(par[x]);
  uni_rank[x] += uni_rank[p];
  return par[p];
}

bool unite(int x, int y, int p) {
  int a = find(x);
  int b = find(y);
  if (a == b) {
    if (uni_rank[x] + p != uni_rank[y])
      return false;
    return true;
  }
  // if(uni_rank[x] < uni_rank[y])par[x] = y;
  // else{
  //   par[y] = x;
  //   if(uni_rank[x] == uni_rank[y])uni_rank[x]++;
  // }
  par[b] = a;
  uni_rank[b] = uni_rank[x] + p - uni_rank[y];
  return true;
}

bool same(int x, int y) { return find(x) == find(y); }

int main() {
  cin >> n >> m;
  init(n);
  rep(i, 0, m) {
    int a, b, c;
    cin >> a >> b >> c;
    if (!unite(a, b, c)) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  return 0;
}