Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
#define mod 1000000007
using ll = long long;
using namespace std;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  ll N, M;
  cin >> N >> M;
  if (M <= 2) {
    cout << "Yes" << endl;
    return 0;
  }
  vector<pair<pair<ll, ll>, ll>> LRD(M);
  rep(i, M) {
    cin >> LRD[i].first.first >> LRD[i].first.second >> LRD[i].second;
    LRD[i].first.first--;
    LRD[i].first.second--;
    if (LRD[i].first.first > LRD[i].first.second) {
      ll tmp = LRD[i].first.first;
      LRD[i].first.first = LRD[i].first.second;
      LRD[i].first.second = tmp;
      LRD[i].second *= -1;
    }
  }
  vector<vector<ll>> g(N);
  vector<vector<ll>> c(N);
  rep(i, M) {
    ll l = LRD[i].first.first, r = LRD[i].first.second, d = LRD[i].second;
    g[l].pb(r);
    c[l].pb(d);
    g[r].pb(l);
    g[r].pb(-d);
  }
  vector<ll> pos(N, 1e18);
  vector<bool> chk(N, false);
  queue<ll> q;
  rep(m, M) {
    if (pos[LRD[m].first.first] == 1e18) {
      pos[LRD[m].first.first] = 0;
      q.push(LRD[m].first.first);
    }
    while (!q.empty()) {
      ll l = q.front();
      q.pop();
      if (chk[l])
        continue;
      chk[l] = true;
      rep(i, sz(g[l])) {
        ll r = g[l][i];
        if (pos[r] != 1e18) {
          if (pos[r] - pos[l] != c[l][i]) {
            cout << "No" << endl;
            return 0;
          }
        } else {
          pos[r] = pos[l] + c[l][i];
          q.push(r);
        }
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
#define mod 1000000007
using ll = long long;
using namespace std;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  ll N, M;
  cin >> N >> M;
  if (M <= 2) {
    cout << "Yes" << endl;
    return 0;
  }
  vector<pair<pair<ll, ll>, ll>> LRD(M);
  rep(i, M) {
    cin >> LRD[i].first.first >> LRD[i].first.second >> LRD[i].second;
    LRD[i].first.first--;
    LRD[i].first.second--;
    if (LRD[i].first.first > LRD[i].first.second) {
      ll tmp = LRD[i].first.first;
      LRD[i].first.first = LRD[i].first.second;
      LRD[i].first.second = tmp;
      LRD[i].second *= -1;
    }
  }
  vector<vector<ll>> g(N);
  vector<vector<ll>> c(N);
  rep(i, M) {
    ll l = LRD[i].first.first, r = LRD[i].first.second, d = LRD[i].second;
    g[l].pb(r);
    c[l].pb(d);
    g[r].pb(l);
    c[r].pb(-d);
  }
  vector<ll> pos(N, 1e18);
  vector<bool> chk(N, false);
  queue<ll> q;
  rep(m, M) {
    if (pos[LRD[m].first.first] == 1e18) {
      pos[LRD[m].first.first] = 0;
      q.push(LRD[m].first.first);
    }
    while (!q.empty()) {
      ll l = q.front();
      q.pop();
      if (chk[l])
        continue;
      chk[l] = true;
      rep(i, sz(g[l])) {
        ll r = g[l][i];
        if (pos[r] != 1e18) {
          if (pos[r] - pos[l] != c[l][i]) {
            cout << "No" << endl;
            return 0;
          }
        } else {
          pos[r] = pos[l] + c[l][i];
          q.push(r);
        }
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}