Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll, ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll, ll>>>
#define PL pair<ll, ll>
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

struct union_find {
  vector<ll> parent;
  union_find(ll n) {
    parent.resize(n);
    rep(i, 0, n) parent[i] = i;
  }

  ll root(ll x) {
    if (parent[x] == x)
      return x;
    parent[x] = root(parent[x]);
    return parent[x];
  }

  void unite(ll x, ll y) {
    ll rx = root(x);
    ll ry = root(y);
    if (rx != ry)
      parent[rx] = ry;
  }

  bool same(ll x, ll y) {
    ll rx = root(x);
    ll ry = root(y);
    if (rx == ry)
      return true;
    return false;
  }
};

ll N, M;

int main() {
  cin >> N >> M;
  union_find t(N + 1);
  VL L(N + 1), R(N + 1), D(N + 1);
  vector<VP> G(N + 1);
  rep(i, 0, M) {
    cin >> L[i] >> R[i] >> D[i];
    t.unite(L[i], R[i]);
    G[L[i]].push_back({R[i], D[i]});
    G[R[i]].push_back({L[i], -D[i]});
  }

  VL x(N + 1, -1e18);
  queue<ll> que;
  rep(i, 1, N + 1) {
    ll r = t.root(i);
    if (x[r] == -1e18) {
      x[r] = 0;
      que.push(r);
    }
  }

  while (!que.empty()) {
    auto q = que.front();
    que.pop();
    for (auto g : G[q]) {
      ll next = x[q] + g.second;
      if (next != x[g.first] && x[g.first] != -1e18) {
        cout << "No" << endl;
        return 0;
      }
      if (x[g.first] == -1e18) {
        x[g.first] = next;
        que.push(g.first);
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll, ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll, ll>>>
#define PL pair<ll, ll>
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

struct union_find {
  vector<ll> parent;
  union_find(ll n) {
    parent.resize(n);
    rep(i, 0, n) parent[i] = i;
  }

  ll root(ll x) {
    if (parent[x] == x)
      return x;
    parent[x] = root(parent[x]);
    return parent[x];
  }

  void unite(ll x, ll y) {
    ll rx = root(x);
    ll ry = root(y);
    if (rx != ry)
      parent[rx] = ry;
  }

  bool same(ll x, ll y) {
    ll rx = root(x);
    ll ry = root(y);
    if (rx == ry)
      return true;
    return false;
  }
};

ll N, M;

int main() {
  cin >> N >> M;
  union_find t(N + 1);
  VL L(M + 1), R(M + 1), D(M + 1);
  vector<VP> G(N + 1);
  rep(i, 0, M) {
    cin >> L[i] >> R[i] >> D[i];
    t.unite(L[i], R[i]);
    G[L[i]].push_back({R[i], D[i]});
    G[R[i]].push_back({L[i], -D[i]});
  }

  VL x(N + 1, -1e18);
  queue<ll> que;
  rep(i, 1, N + 1) {
    ll r = t.root(i);
    if (x[r] == -1e18) {
      x[r] = 0;
      que.push(r);
    }
  }

  while (!que.empty()) {
    auto q = que.front();
    que.pop();
    for (auto g : G[q]) {
      ll next = x[q] + g.second;
      if (next != x[g.first] && x[g.first] != -1e18) {
        cout << "No" << endl;
        return 0;
      }
      if (x[g.first] == -1e18) {
        x[g.first] = next;
        que.push(g.first);
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}