prithivMLmods/Coder-Stat huggingface dataset:
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

/*
    Weighted Union Find Tree
        difference manegement
*/

template <class Abel> struct WeightedUnionFind {
  vector<int> data;
  vector<Abel> cost;
  int sz;
  WeightedUnionFind(int sz, Abel unit = 0)
      : data(sz, -1), cost(sz, unit), sz(sz) {}
  bool unionSet(int x, int y, Abel w) {
    w += (weight(x) - weight(y));
    if ((x = root(x)) == (y = root(y))) {
      return false;
    }
    if (data[x] > data[y]) {
      swap(x, y);
      w = -w;
    }
    data[x] += data[y];
    data[y] = x;
    cost[y] = w;
    sz--;
    return true;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) {
    if (data[x] < 0)
      return x;
    int r = root(data[x]);
    cost[x] += cost[data[x]];
    return data[x] = r;
  }
  Abel weight(int x) {
    root(x);
    return cost[x];
  }
  Abel diff(int x, int y) { return weight(y) - weight(x); }
  int size(int x) { return -data[root(x)]; }
  int size() { return sz; }
};

int main() {
  int N, M;
  cin >> N >> M;
  bool judge = true;
  WeightedUnionFind<int> uf(N);

  for (int i = 0; i < N; i++) {
    int L, R, D;
    cin >> L >> R >> D;
    L--;
    R--;
    if (!uf.unionSet(L, R, D)) {
      int diff = uf.diff(L, R);
      if (D != diff)
        judge = false;
    }
  }

  cout << ((judge) ? "Yes" : "No") << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

/*
    Weighted Union Find Tree
        difference manegement
*/

template <class Abel> struct WeightedUnionFind {
  vector<int> data;
  vector<Abel> cost;
  int sz;
  WeightedUnionFind(int sz, Abel unit = 0)
      : data(sz, -1), cost(sz, unit), sz(sz) {}
  bool unionSet(int x, int y, Abel w) {
    w += (weight(x) - weight(y));
    if ((x = root(x)) == (y = root(y))) {
      return false;
    }
    if (data[x] > data[y]) {
      swap(x, y);
      w = -w;
    }
    data[x] += data[y];
    data[y] = x;
    cost[y] = w;
    sz--;
    return true;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) {
    if (data[x] < 0)
      return x;
    int r = root(data[x]);
    cost[x] += cost[data[x]];
    return data[x] = r;
  }
  Abel weight(int x) {
    root(x);
    return cost[x];
  }
  Abel diff(int x, int y) { return weight(y) - weight(x); }
  int size(int x) { return -data[root(x)]; }
  int size() { return sz; }
};

int main() {
  int N, M;
  cin >> N >> M;
  bool judge = true;
  WeightedUnionFind<int> uf(N);

  for (int i = 0; i < M; i++) {
    int L, R, D;
    cin >> L >> R >> D;
    L--;
    R--;
    if (!uf.unionSet(L, R, D)) {
      int diff = uf.diff(L, R);
      if (D != diff)
        judge = false;
    }
  }

  cout << ((judge) ? "Yes" : "No") << endl;
  return 0;
}
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 100005;
/* global variables */
vector<vector<pii>> Adj(MAX);
vector<int> d(10, -INF);
bool can = true;
/* function */
void bfs(int u);

/* main */
int main() {

  int V, E, v, u, w;
  cin >> V >> E;

  for (int i = 0; i < E; i++) {
    cin >> v >> u >> w;
    Adj[v].push_back(pii(u, w));
    Adj[u].push_back(pii(v, -w));
  }

  for (int u = 0; u < V; u++)
    if (d[u] == -INF)
      bfs(u);

  cout << (can ? "Yes" : "No") << '\n';
}

void bfs(int u) {
  queue<int> Q;
  int s, t;
  Q.push(u);
  d[u] = 0;
  while (!Q.empty()) {
    s = Q.front();
    Q.pop();
    for (pii p : Adj[s]) {
      int t = p.first, w = p.second;
      if (d[t] == -INF) {
        d[t] = d[s] + w;
        Q.push(t);
      } else if (d[t] != d[s] + w)
        can = false;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 100005;
/* global variables */
vector<vector<pii>> Adj(MAX);
vector<int> d(MAX, -INF);
bool can = true;
/* function */
void bfs(int u);

/* main */
int main() {

  int V, E, v, u, w;
  cin >> V >> E;

  for (int i = 0; i < E; i++) {
    cin >> v >> u >> w;
    Adj[v].push_back(pii(u, w));
    Adj[u].push_back(pii(v, -w));
  }

  for (int u = 0; u < V; u++)
    if (d[u] == -INF)
      bfs(u);

  cout << (can ? "Yes" : "No") << '\n';
}

void bfs(int u) {
  queue<int> Q;
  int s, t;
  Q.push(u);
  d[u] = 0;
  while (!Q.empty()) {
    s = Q.front();
    Q.pop();
    for (pii p : Adj[s]) {
      int t = p.first, w = p.second;
      if (d[t] == -INF) {
        d[t] = d[s] + w;
        Q.push(t);
      } else if (d[t] != d[s] + w)
        can = false;
    }
  }
}
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

template <class Abel> struct UnionFind {
  vector<int> par;
  vector<int> rank;
  vector<Abel> diff_weight;

  UnionFind(int n = 1, Abel SUM_UNITY = 0) { init(n, SUM_UNITY); }

  void init(int n = 1, Abel SUM_UNITY = 0) {
    par.resize(n);
    rank.resize(n);
    diff_weight.resize(n);
    for (int i = 0; i < n; ++i)
      par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
  }

  int root(int x) {
    if (par[x] == x) {
      return x;
    } else {
      int r = root(par[x]);
      diff_weight[x] += diff_weight[par[x]];
      return par[x] = r;
    }
  }

  Abel weight(int x) {
    root(x);
    return diff_weight[x];
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y, Abel w) {
    w += weight(x);
    w -= weight(y);
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (rank[x] < rank[y])
      swap(x, y), w = -w;
    if (rank[x] == rank[y])
      ++rank[x];
    par[y] = x;
    diff_weight[y] = w;
    return true;
  }

  Abel diff(int x, int y) { return weight(y) - weight(x); }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind<int> uf(n);

  for (int i = 0; i < n; i++) {
    int l, r, d;
    cin >> l >> r >> d;
    --l;
    --r;

    if (uf.issame(l, r)) {
      int diff = uf.diff(l, r);
      if (diff != d) {
        cout << "No" << endl;
        return 0;
      }
    } else {
      uf.merge(l, r, d);
    }
  }
  cout << "Yes" << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

template <class Abel> struct UnionFind {
  vector<int> par;
  vector<int> rank;
  vector<Abel> diff_weight;

  UnionFind(int n = 1, Abel SUM_UNITY = 0) { init(n, SUM_UNITY); }

  void init(int n = 1, Abel SUM_UNITY = 0) {
    par.resize(n);
    rank.resize(n);
    diff_weight.resize(n);
    for (int i = 0; i < n; ++i)
      par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
  }

  int root(int x) {
    if (par[x] == x) {
      return x;
    } else {
      int r = root(par[x]);
      diff_weight[x] += diff_weight[par[x]];
      return par[x] = r;
    }
  }

  Abel weight(int x) {
    root(x);
    return diff_weight[x];
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  bool merge(int x, int y, Abel w) {
    w += weight(x);
    w -= weight(y);
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (rank[x] < rank[y])
      swap(x, y), w = -w;
    if (rank[x] == rank[y])
      ++rank[x];
    par[y] = x;
    diff_weight[y] = w;
    return true;
  }

  Abel diff(int x, int y) { return weight(y) - weight(x); }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind<int> uf(n);

  for (int i = 0; i < m; i++) {
    int l, r, d;
    cin >> l >> r >> d;
    --l;
    --r;

    if (uf.issame(l, r)) {
      int diff = uf.diff(l, r);
      if (diff != d) {
        cout << "No" << endl;
        return 0;
      }
    } else {
      uf.merge(l, r, d);
    }
  }
  cout << "Yes" << endl;
}
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
#define MAX 10001

int N, M;

vector<pair<int, int>> adj[MAX];
int visited[MAX];
int d[MAX];
bool wrong = 0;

void dfs(int s) {
  visited[s] = 1;

  for (auto u : adj[s]) {
    // d[u.first] = d[s] + u.second;
    // cout << s << ":" << "d:" << u.first << ":" << d[u.first] << endl;
    if (visited[u.first] == 1) {
      if ((d[u.first] - d[s] - u.second) != 0) {
        wrong = 1;
        return;
      }
    } else {
      d[u.first] = d[s] + u.second;
      dfs(u.first);
    }
  }
}

int main(void) {
  // Your code here!
  cin >> N >> M;
  for (int i = 1; i <= M; i++) {
    int l, r, d;
    cin >> l >> r >> d;
    adj[l].push_back({r, d});
    adj[r].push_back({l, -d});
  }

  for (int i = 1; i <= N; i++) {
    if (visited[i] == 1)
      continue;
    dfs(i);
  }

  if (wrong == 1)
    cout << "No" << endl;
  else
    cout << "Yes" << endl;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
#define MAX 100001

int N, M;

vector<pair<int, int>> adj[MAX];
int visited[MAX];
int d[MAX];
bool wrong = 0;

void dfs(int s) {
  visited[s] = 1;

  for (auto u : adj[s]) {
    // d[u.first] = d[s] + u.second;
    // cout << s << ":" << "d:" << u.first << ":" << d[u.first] << endl;
    if (visited[u.first] == 1) {
      if ((d[u.first] - d[s] - u.second) != 0) {
        wrong = 1;
        return;
      }
    } else {
      d[u.first] = d[s] + u.second;
      dfs(u.first);
    }
  }
}

int main(void) {
  // Your code here!
  cin >> N >> M;
  for (int i = 1; i <= M; i++) {
    int l, r, d;
    cin >> l >> r >> d;
    adj[l].push_back({r, d});
    adj[r].push_back({l, -d});
  }

  for (int i = 1; i <= N; i++) {
    if (visited[i] == 1)
      continue;
    dfs(i);
  }

  if (wrong == 1)
    cout << "No" << endl;
  else
    cout << "Yes" << endl;
}
Problem p03450 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <climits>
#include <cstdio>
#include <map>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define MAX 100100
#define INF INT_MAX

bool flag = true;

void search(vector<vector<pair<int, int>>> G, int D[MAX], int x, int N, int d) {
  D[x] = d;
  if (G[x].empty())
    return;

  auto it = G[x].begin();
  for (; it != G[x].end(); it++) {
    if (D[it->first] == INF)
      search(G, D, it->first, N, d + it->second);
    else if (D[it->first] != d + it->second)
      flag = false;
  }
}

int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  vector<vector<pair<int, int>>> G(MAX);
  int D[MAX];
  FOR(i, 0, MAX) D[i] = INF;

  FOR(i, 0, M) {
    int l, r, d;
    scanf("%d%d%d", &l, &r, &d);
    G[l].push_back(make_pair(r, d));
    G[r].push_back(make_pair(l, -d));
  }

  FOR(i, 1, N + 1) {
    if (D[i] == INF)
      search(G, D, i, N, 0);
  }

  if (flag)
    printf("Yes\n");
  else
    printf("No\n");

  return 0;
}


Fixed submission:
#include <climits>
#include <cstdio>
#include <map>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define MAX 100100
#define INF INT_MAX

bool flag = true;

void search(vector<vector<pair<int, int>>> &G, int D[MAX], int x, int N,
            int d) {
  D[x] = d;
  if (G[x].empty())
    return;

  auto it = G[x].begin();
  for (; it != G[x].end(); it++) {
    if (D[it->first] == INF)
      search(G, D, it->first, N, d + it->second);
    else if (D[it->first] != d + it->second)
      flag = false;
  }
}

int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  vector<vector<pair<int, int>>> G(MAX);
  int D[MAX];
  FOR(i, 0, MAX) D[i] = INF;

  FOR(i, 0, M) {
    int l, r, d;
    scanf("%d%d%d", &l, &r, &d);
    G[l].push_back(make_pair(r, d));
    G[r].push_back(make_pair(l, -d));
  }

  FOR(i, 1, N + 1) {
    if (D[i] == INF)
      search(G, D, i, N, 0);
  }

  if (flag)
    printf("Yes\n");
  else
    printf("No\n");

  return 0;
}
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
// review

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e16;
const int MAXN = 210000;
const int MAXM = 210000;

ll l[MAXM], r[MAXM], d[MAXM];
ll x[MAXN];
vector<P> G[MAXN];
ll n, m;

bool dfs(ll s) {
  for (ll i = 0; i < G[s].size(); i++) {
    P p = G[s][i];
    if (x[p.first] == INF) {
      x[p.first] = x[s] + p.second;
      if (!dfs(p.first)) {
        return false;
      }
    }
    if (x[p.first] != x[s] + p.second) {
      return false;
    }
  }
  return true;
}

void solve() {
  for (ll i = 0; i < m; i++) {
    G[r[i] - 1].push_back(P(l[i] - 1, d[i]));
    G[l[i] - 1].push_back(P(r[i] - 1, -d[i]));
  }
  for (ll i = 0; i < n; i++) {
    x[i] = INF;
  }
  bool success = true;
  for (ll i = 0; i < n; i++) {
    if (x[i] == INF) {
      if (!dfs(i)) {
        success = false;
        break;
      }
    }
  }
  cout << (success ? "Yes" : "No") << endl;
}

int main() {
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    cin >> l[i] >> r[i] >> d[i];
  }
  solve();
}


Fixed submission:
// review

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e16;
const int MAXN = 210000;
const int MAXM = 210000;

ll l[MAXM], r[MAXM], d[MAXM];
ll x[MAXN];
vector<P> G[MAXN];
ll n, m;

bool dfs(ll s) {
  for (ll i = 0; i < G[s].size(); i++) {
    P p = G[s][i];
    if (x[p.first] == INF) {
      x[p.first] = x[s] + p.second;
      if (!dfs(p.first)) {
        return false;
      }
    }
    if (x[p.first] != x[s] + p.second) {
      return false;
    }
  }
  return true;
}

void solve() {
  for (ll i = 0; i < m; i++) {
    G[r[i] - 1].push_back(P(l[i] - 1, d[i]));
    G[l[i] - 1].push_back(P(r[i] - 1, -d[i]));
  }
  for (ll i = 0; i < n; i++) {
    x[i] = INF;
  }
  bool success = true;
  for (ll i = 0; i < n; i++) {
    if (x[i] == INF) {
      x[i] = 0;
      if (!dfs(i)) {
        success = false;
        break;
      }
    }
  }
  cout << (success ? "Yes" : "No") << endl;
}

int main() {
  cin >> n >> m;
  for (ll i = 0; i < m; i++) {
    cin >> l[i] >> r[i] >> d[i];
  }
  solve();
}
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, m, n) for (int(i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
using namespace std;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1e9 + 7;
const ll LINF = 1LL << 60;

const int MAX_N = 200000;

int n, m;
vector<vector<P>> G;
int dist[MAX_N];

bool dfs(int v) {
  for (auto p : G[v]) {
    int nv = p.first;
    int nd = p.second;
    if (dist[nv] != INF) {
      if (dist[nv] != dist[v] + nd)
        return false;
    } else {
      dist[nv] = dist[v] + nd;
      if (!dfs(nv))
        return false;
    }
  }
  return true;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);

  cin >> n >> m;
  G.resize(n);
  int s;
  rep(i, m) {
    int l, r, d;
    cin >> l >> r >> d;
    --l, --r;
    if (i == 0)
      s = l;
    G[l].push_back(make_pair(r, d));
    G[r].push_back(make_pair(l, -d));
  }

  rep(i, MAX_N) dist[i] = INF;

  bool ok = true;
  rep(i, n) {
    if (dist[i] != INF)
      continue;
    dist[i] = 0;
    if (!dfs(s))
      ok = false;
  }

  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, m, n) for (int(i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
using namespace std;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1e9 + 7;
const ll LINF = 1LL << 60;

const int MAX_N = 200000;

int n, m;
vector<vector<P>> G;
int dist[MAX_N];

bool dfs(int v) {
  for (auto p : G[v]) {
    int nv = p.first;
    int nd = p.second;
    if (dist[nv] != INF) {
      if (dist[nv] != dist[v] + nd)
        return false;
    } else {
      dist[nv] = dist[v] + nd;
      if (!dfs(nv))
        return false;
    }
  }
  return true;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);

  cin >> n >> m;
  G.resize(n);
  int s;
  rep(i, m) {
    int l, r, d;
    cin >> l >> r >> d;
    --l, --r;
    if (i == 0)
      s = l;
    G[l].push_back(make_pair(r, d));
    G[r].push_back(make_pair(l, -d));
  }

  rep(i, MAX_N) dist[i] = INF;

  bool ok = true;
  rep(i, n) {
    if (dist[i] != INF)
      continue;
    dist[i] = 0;
    if (!dfs(i))
      ok = false;
  }

  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define lint long long
#define P pair<int, int>

int main() {
  int N, M;
  cin >> N >> M;

  bool used[M + 1];
  memset(used, false, sizeof used);

  vector<vector<P>> List(N);
  for (int i = 0; i < M; ++i) {
    int l, r, d;
    cin >> l >> r >> d;
    List[l - 1].push_back(make_pair(r - 1, d));
    List[r - 1].push_back(make_pair(l - 1, -d));
  }

  //    for(int i = 0; i < N; ++i){
  //        for(auto e : List[i]){
  //            cout << i << " " << e.first << " " << e.second << endl;
  //        }
  //    }
  //    cout << endl;

  for (int i = 0; i < N; ++i) {
    queue<P> que;
    map<int, int> res;
    if (!used[i]) {
      used[i] = true;
      res[i] = 0;
      for (auto e : List[i]) {
        que.push(e);
      }

      while (!que.empty()) {
        P tmp = que.front();
        que.pop();
        //                cout << tmp.first << " " << tmp.second << " " <<
        //                used[tmp.first]<< endl;
        if (used[tmp.first]) {
          if (res[tmp.first] != tmp.second) {
            cout << "No" << endl;
            return 0;
          }
        } else {
          used[tmp.first] = true;
          res[tmp.first] = tmp.second;

          for (auto e : List[tmp.first]) {
            que.push(make_pair(e.first, e.second + tmp.second));
          }
        }
      }
    }
  }
  cout << "Yes" << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define lint long long
#define P pair<int, int>

int main() {
  int N, M;
  cin >> N >> M;

  bool used[N + 1];
  memset(used, false, sizeof used);

  vector<vector<P>> List(N);
  for (int i = 0; i < M; ++i) {
    int l, r, d;
    cin >> l >> r >> d;
    List[l - 1].push_back(make_pair(r - 1, d));
    List[r - 1].push_back(make_pair(l - 1, -d));
  }

  //    for(int i = 0; i < N; ++i){
  //        for(auto e : List[i]){
  //            cout << i << " " << e.first << " " << e.second << endl;
  //        }
  //    }
  //    cout << endl;

  for (int i = 0; i < N; ++i) {
    queue<P> que;
    map<int, int> res;
    if (!used[i]) {
      used[i] = true;
      res[i] = 0;
      for (auto e : List[i]) {
        que.push(e);
      }

      while (!que.empty()) {
        P tmp = que.front();
        que.pop();
        //                cout << tmp.first << " " << tmp.second << " " <<
        //                used[tmp.first]<< endl;
        if (used[tmp.first]) {
          if (res[tmp.first] != tmp.second) {
            cout << "No" << endl;
            return 0;
          }
        } else {
          used[tmp.first] = true;
          res[tmp.first] = tmp.second;

          for (auto e : List[tmp.first]) {
            que.push(make_pair(e.first, e.second + tmp.second));
          }
        }
      }
    }
  }
  cout << "Yes" << endl;
}
Problem p03450 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define N 100001
#define int long long
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ '0'), c = getchar();
  return x;
}
int n, m, fa[N], dis[N];
inline int find(const int &x) {
  if (x == fa[x])
    return x;
  int y = find(fa[x]);
  dis[x] += dis[fa[x]];
  return fa[x] = y;
}
signed main() {
#ifndef ONLINE_JUDGE
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
  n = read(), m = read();
  for (register int i = 1; i <= n; i++)
    fa[i] = i;
  while (m--) {
    int l = read(), r = read(), d = read();
    int x = find(l), y = find(r);
    if (x != y) {
      fa[y] = x;
      dis[y] = dis[l] + d - dis[r];
    } else {
      if (dis[r] - dis[l] != d) {
        printf("No\n");
        return 0;
      }
    }
  }
  printf("Yes\n");
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 100001
#define int long long
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ '0'), c = getchar();
  return x;
}
int n, m, fa[N], dis[N];
inline int find(const int &x) {
  if (x == fa[x])
    return x;
  int y = find(fa[x]);
  dis[x] += dis[fa[x]];
  return fa[x] = y;
}
signed main() {
  n = read(), m = read();
  for (register int i = 1; i <= n; i++)
    fa[i] = i;
  while (m--) {
    int l = read(), r = read(), d = read();
    int x = find(l), y = find(r);
    if (x != y) {
      fa[y] = x;
      dis[y] = dis[l] + d - dis[r];
    } else {
      if (dis[r] - dis[l] != d) {
        printf("No\n");
        return 0;
      }
    }
  }
  printf("Yes\n");
}
Problem p03450 (C++)

Buggy submission (status: Time Limit Exceeded):
#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>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

int main() {
  ll N, M;
  cin >> N >> M;
  VVP G(N + 1);
  rep(i, 0, M) {
    ll l, r, d;
    cin >> l >> r >> d;
    G[l].push_back({r, d});
    G[r].push_back({l, -d});
  }

  VB seen(N + 1, false);
  rep(i, 1, N + 1) {
    if (seen[i])
      continue;
    ll srt = i;
    VL d(N + 1, -1e18);
    {
      queue<PL> que;
      que.push({0, srt});
      d[srt] = 0;
      seen[srt] = true;
      while (!que.empty()) {
        auto q = que.front();
        que.pop();
        for (auto g : G[q.second]) {
          if (d[g.first] != -1e18 && d[g.first] != q.first + g.second) {
            cout << "No" << endl;
            return 0;
          }
          if (d[g.first] == -1e18) {
            d[g.first] = q.first + g.second;
            seen[g.first] = true;
            que.push({d[g.first], 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>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

int main() {
  ll N, M;
  cin >> N >> M;
  VVP G(N + 1);
  rep(i, 0, M) {
    ll l, r, d;
    cin >> l >> r >> d;
    G[l].push_back({r, d});
    G[r].push_back({l, -d});
  }

  VB seen(N + 1, false);
  rep(i, 1, N + 1) {
    if (seen[i] || G[i].empty())
      continue;
    ll srt = i;
    VL d(N + 1, -1e18);
    {
      queue<PL> que;
      que.push({0, srt});
      d[srt] = 0;
      seen[srt] = true;
      while (!que.empty()) {
        auto q = que.front();
        que.pop();
        for (auto g : G[q.second]) {
          if (d[g.first] != -1e18 && d[g.first] != q.first + g.second) {
            cout << "No" << endl;
            return 0;
          }
          if (d[g.first] == -1e18) {
            d[g.first] = q.first + g.second;
            seen[g.first] = true;
            que.push({d[g.first], g.first});
          }
        }
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<pair<ll, ll>>> vvpll;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define exrep(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;

vvpll G;
vl visited;
vl d(MAX_N);
bool ok = true;

void dfs(ll v, ll dist) {
  if (visited[v]) {
    if (d[v] != dist) {
      ok = false;
    }
  }
  visited[v] = 1;
  d[v] = dist;
  for (auto p : G[v]) {
    ll u = p.first;
    ll cost = p.second;
    dfs(u, d[v] + cost);
  }
}

int main() {
  ll n, m;
  cin >> n >> m;

  G.resize(n);
  rep(i, m) {
    ll L, R, D;
    cin >> L >> R >> D;
    L--;
    R--;
    G[L].pb(make_pair(R, D));
    G[R].pb(make_pair(L, -D));
  }

  visited.resize(n);
  rep(v, n) {
    if (!visited[v]) {
      dfs(v, 0);
    }
  }

  if (ok) {
    out("Yes");
  } else {
    out("No");
  }

  re0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<pair<ll, ll>>> vvpll;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define exrep(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;

vvpll G;
vl visited;
vl d(MAX_N);
bool ok = true;

void dfs(ll v, ll dist) {
  if (visited[v]) {
    if (d[v] != dist) {
      ok = false;
    }
    return;
  }
  visited[v] = 1;
  d[v] = dist;
  for (auto p : G[v]) {
    ll u = p.first;
    ll cost = p.second;
    dfs(u, d[v] + cost);
  }
}

int main() {
  ll n, m;
  cin >> n >> m;

  G.resize(n);
  rep(i, m) {
    ll L, R, D;
    cin >> L >> R >> D;
    L--;
    R--;
    G[L].pb(make_pair(R, D));
    G[R].pb(make_pair(L, -D));
  }

  visited.resize(n);
  rep(v, n) {
    if (!visited[v]) {
      dfs(v, 0);
    }
  }

  if (ok) {
    out("Yes");
  } else {
    out("No");
  }

  re0;
}
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 0;
const ll INF = 1LL << 60; // MAX 9223372036854775807

struct edge {
  ll to;
  ll cost;
  edge() {}
  edge(ll to, ll cost) : to(to), cost(cost){};
};
int main() {
  std::ifstream in("input");
  std::cin.rdbuf(in.rdbuf());
  ll n, m;
  cin >> n >> m;
  vector<vector<edge>> g(n);
  rep(i, m) {
    ll a, b, c;
    cin >> a >> b >> c;
    g[a - 1].emplace_back(b - 1, c);
    g[b - 1].emplace_back(a - 1, -c);
  }
  vector<ll> dist(n, INF);
  vector<ll> see(n);
  queue<ll> q;
  rep(i, n) {
    if (see[i])
      continue;
    dist[i] = 0;
    see[i] = 1;
    q.push(i);
    while (!q.empty()) {
      auto p = q.front();
      q.pop();
      for (auto r : g[p]) {
        if (dist[r.to] == INF)
          dist[r.to] = dist[p] + r.cost;
        else if (dist[r.to] != dist[p] + r.cost) {
          cout << "No" << endl;
          return 0;
        }
        if (see[r.to] != 0)
          continue;
        see[r.to] = 1;
        q.push(r.to);
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 0;
const ll INF = 1LL << 60; // MAX 9223372036854775807

struct edge {
  ll to;
  ll cost;
  edge() {}
  edge(ll to, ll cost) : to(to), cost(cost){};
};
int main() {
  ll n, m;
  cin >> n >> m;
  vector<vector<edge>> g(n);
  rep(i, m) {
    ll a, b, c;
    cin >> a >> b >> c;
    g[a - 1].emplace_back(b - 1, c);
    g[b - 1].emplace_back(a - 1, -c);
  }
  vector<ll> dist(n, INF);
  vector<ll> see(n);
  queue<ll> q;
  rep(i, n) {
    if (see[i])
      continue;
    dist[i] = 0;
    see[i] = 1;
    q.push(i);
    while (!q.empty()) {
      auto p = q.front();
      q.pop();
      for (auto r : g[p]) {
        if (dist[r.to] == INF)
          dist[r.to] = dist[p] + r.cost;
        else if (dist[r.to] != dist[p] + r.cost) {
          cout << "No" << endl;
          return 0;
        }
        if (see[r.to] != 0)
          continue;
        see[r.to] = 1;
        q.push(r.to);
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}
Problem p03450 (Python)

Buggy submission (status: Runtime Error):
N, M = map(int, input().split())
E = [[] for i in range(N + 1)]
for _ in range(M):
    l, r, d = map(int, input().split())
    E[l].append((r, d))
    E[r].append((l, -d))
INF = 10**10
dp = [INF for i in range(N + 1)]


def dfs(i):
    for r, d in E[i]:
        if dp[r] == INF:
            dp[r] = dp[i] + d
            if not dfs(r):
                return False
        elif dp[r] != dp[i] + d:
            return False
    return True


for i in range(1, N + 1):
    if dp[i] == INF:
        dp[i] = 0
        if not dfs(i):
            print("No")
            break
else:
    print("Yes")


Fixed submission:
import sys

sys.setrecursionlimit(2 * 10**5)
N, M = map(int, input().split())
E = [[] for i in range(N + 1)]
for _ in range(M):
    l, r, d = map(int, input().split())
    E[l].append((r, d))
    E[r].append((l, -d))
INF = 10**10
dp = [INF for i in range(N + 1)]


def dfs(i):
    for r, d in E[i]:
        if dp[r] == INF:
            dp[r] = dp[i] + d
            if not dfs(r):
                return False
        elif dp[r] != dp[i] + d:
            return False
    return True


for i in range(1, N + 1):
    if dp[i] == INF:
        dp[i] = 0
        if not dfs(i):
            print("No")
            break
else:
    print("Yes")
Problem p03450 (Python)

Buggy submission (status: Runtime Error):
N, M = map(int, input().split())
nl = [[] for _ in range(N + 1)]
for _ in range(M):
    L, R, D = map(int, input().split())
    nl[L].append((R, D))
    nl[R].append((L, -D))

seen = [None for _ in range(N + 1)]


def dfs(v, d):
    seen[v] = d
    for next_v, D in nl[v]:
        next_d = d + D
        if seen[next_v] is None:
            dfs(next_v, next_d)
        else:
            if seen[next_v] != next_d:
                print("No")
                exit(0)


for v in range(1, N + 1):  # すべての連結成分に対してDFS
    if seen[v] is None:
        dfs(v, 0)

print("Yes")


Fixed submission:
import sys

sys.setrecursionlimit(300000)

N, M = map(int, input().split())
nl = [[] for _ in range(N + 1)]
for _ in range(M):
    L, R, D = map(int, input().split())
    nl[L].append((R, D))
    nl[R].append((L, -D))

seen = [None for _ in range(N + 1)]


def dfs(v, d):
    seen[v] = d
    for next_v, D in nl[v]:
        next_d = d + D
        if seen[next_v] is None:
            dfs(next_v, next_d)
        else:
            if seen[next_v] != next_d:
                print("No")
                exit(0)


for v in range(1, N + 1):  # すべての連結成分に対してDFS
    if seen[v] is None:
        dfs(v, 0)

print("Yes")
Problem p03450 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

long long MOD = 1000000000 + 7;

int main() {
  cout << setprecision(10);
  int N, M;
  cin >> N >> M;
  vector<vector<int>> v(N);

  map<pii, ll> m;

  vector<pii> e; // 枝情報

  int tmp1, tmp2;
  for (int i = 0; i < M; i++) {

    ll d;
    cin >> tmp1 >> tmp2 >> d;
    tmp1--;
    tmp2--;
    v.at(tmp1).push_back(tmp2);
    v.at(tmp2).push_back(tmp1);

    m[make_pair(tmp1, tmp2)] = d;
    m[make_pair(tmp2, tmp1)] = -d;
    e.push_back(make_pair(tmp1, tmp2));
    e.push_back(make_pair(tmp2, tmp1));
  }

  queue<int> q;

  ll INF = 1000000000000000000;
  vector<ll> dist(N, INF);
  vector<bool> check(N, false);

  bool flg = true;
  for (int i = 0; i <= N; i++) {

    while (!q.empty()) {
      int now = q.front();
      check[now] = true;
      q.pop();

      for (auto next : v.at(now)) {
        if (dist[next] == INF) {
          dist[next] = dist[now] + m[make_pair(now, next)];
          q.push(next);
        } else {
          if (dist[next] == dist[now] + m[make_pair(now, next)]) {
            // ok
            if (!check[next]) {
              check[now] = true;
              q.push(next);
            }
          } else {
            flg = false;
            break;
          }
        }
      }
      if (!flg)
        break;
    }

    if (i < N && check[i] == false) {
      q.push(i);
      dist[i] = 0;
    }
  }
  cout << (flg ? "Yes" : "No") << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

long long MOD = 1000000000 + 7;

int main() {
  cout << setprecision(10);
  int N, M;
  cin >> N >> M;
  vector<vector<int>> v(N);

  map<pii, ll> m;

  vector<pii> e; // 枝情報

  int tmp1, tmp2;
  for (int i = 0; i < M; i++) {

    ll d;
    cin >> tmp1 >> tmp2 >> d;
    tmp1--;
    tmp2--;
    v.at(tmp1).push_back(tmp2);
    v.at(tmp2).push_back(tmp1);

    m[make_pair(tmp1, tmp2)] = d;
    m[make_pair(tmp2, tmp1)] = -d;
    e.push_back(make_pair(tmp1, tmp2));
    e.push_back(make_pair(tmp2, tmp1));
  }

  queue<int> q;

  ll INF = 1000000000000000000;
  vector<ll> dist(N, INF);
  vector<bool> check(N, false);

  bool flg = true;
  for (int i = 0; i <= N; i++) {

    while (!q.empty()) {
      int now = q.front();
      check[now] = true;
      q.pop();

      for (auto next : v.at(now)) {
        if (dist[next] == INF) {
          dist[next] = dist[now] + m[make_pair(now, next)];
          q.push(next);
        } else {
          if (dist[next] == dist[now] + m[make_pair(now, next)]) {
            // ok
            if (!check[next]) {
            }
          } else {
            flg = false;
            break;
          }
        }
      }
      if (!flg)
        break;
    }

    if (i < N && check[i] == false) {
      q.push(i);
      dist[i] = 0;
    }
  }
  cout << (flg ? "Yes" : "No") << endl;
}
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;

const int INF = (1 << 30) - 1;
const ll INF64 = ((ll)1 << 62) - 1;
const double PI = 3.1415926535897932384626433832795;

const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};

bool solve(int n, int m, const vector<int> &l, const vector<int> &r,
           const vector<int> &d) {
  vector<vector<pair<int, int>>> graph(n);
  for (int i = 0; i < m; i++) {
    graph[l[i]].emplace_back(r[i], d[i]);
    graph[r[i]].emplace_back(l[i], -d[i]);
  }

  vector<bool> used(n);
  vector<int> dist(n);
  for (int i = 0; i < n; i++) {
    if (used[i]) {
      continue;
    }

    queue<int> que;
    que.push(i);
    used[i] = true;
    dist[i] = 0;
    while (!que.empty()) {
      int now = que.front();
      que.pop();
      for (auto e : graph[now]) {
        int next = e.first;
        int cost = e.second;

        if (used[next] && dist[now] + cost != dist[next]) {
          return false;
        } else if (!used[next]) {
          used[next] = true;
          dist[next] = dist[now] + cost;
          que.push(next);
        }
      }
    }
  }
  return true;
}

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

  int n, m;
  cin >> n >> m;

  vector<int> l(n), r(n), d(n);
  for (int i = 0; i < m; i++) {
    cin >> l[i] >> r[i] >> d[i];
    l[i]--;
    r[i]--;
  }

  cout << (solve(n, m, l, r, d) ? "Yes" : "No") << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;

const int INF = (1 << 30) - 1;
const ll INF64 = ((ll)1 << 62) - 1;
const double PI = 3.1415926535897932384626433832795;

const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};

bool solve(int n, int m, const vector<int> &l, const vector<int> &r,
           const vector<int> &d) {
  vector<vector<pair<int, int>>> graph(n);
  for (int i = 0; i < m; i++) {
    graph[l[i]].emplace_back(r[i], d[i]);
    graph[r[i]].emplace_back(l[i], -d[i]);
  }

  vector<bool> used(n);
  vector<int> dist(n);
  for (int i = 0; i < n; i++) {
    if (used[i]) {
      continue;
    }

    queue<int> que;
    que.push(i);
    used[i] = true;
    dist[i] = 0;
    while (!que.empty()) {
      int now = que.front();
      que.pop();
      for (auto e : graph[now]) {
        int next = e.first;
        int cost = e.second;

        if (used[next] && dist[now] + cost != dist[next]) {
          return false;
        } else if (!used[next]) {
          used[next] = true;
          dist[next] = dist[now] + cost;
          que.push(next);
        }
      }
    }
  }
  return true;
}

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

  int n, m;
  cin >> n >> m;

  vector<int> l(m), r(m), d(m);
  for (int i = 0; i < m; i++) {
    cin >> l[i] >> r[i] >> d[i];
    l[i]--;
    r[i]--;
  }

  cout << (solve(n, m, l, r, d) ? "Yes" : "No") << endl;
  return 0;
}
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef priority_queue<int> HEAP;
typedef priority_queue<int, vector<int>, greater<int>> RHEAP;

const int N = 100010, M = 200010;

int n, m;
int h[N], e[M], w[M], ne[M], idx;
int q[M];
int dist[N];
bool st[N];

void add(int a, int b, int c) {
  e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

bool bfs(int u) {
  int hh = 0, tt = -1;
  q[++tt] = u;
  dist[u] = 0;
  st[u] = true;

  while (hh <= tt) {
    int t = q[hh++];

    for (int i = h[t]; ~i; i = ne[i]) {
      int j = e[i];
      if (!st[j]) {
        q[++tt] = j;
        st[j] = true;
        dist[j] = dist[t] + w[i];
      } else if (dist[j] != dist[t] + w[i])
        return false;
    }
  }
  return true;
}

int main() {
  memset(h, -1, sizeof h);
  scanf("%d%d", &n, &m);

  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    add(a, b, c), add(b, a, -c);
  }

  for (int i = 1; i <= n; i++)
    if (!st[i] && !bfs(i)) {
      puts("No");
      return 0;
    }
  puts("Yes");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef priority_queue<int> HEAP;
typedef priority_queue<int, vector<int>, greater<int>> RHEAP;

const int N = 100010, M = 400010;

int n, m;
int h[N], e[M], w[M], ne[M], idx;
int q[M];
int dist[N];
bool st[N];

void add(int a, int b, int c) {
  e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

bool bfs(int u) {
  int hh = 0, tt = -1;
  q[++tt] = u;
  dist[u] = 0;
  st[u] = true;

  while (hh <= tt) {
    int t = q[hh++];

    for (int i = h[t]; ~i; i = ne[i]) {
      int j = e[i];
      if (!st[j]) {
        q[++tt] = j;
        st[j] = true;
        dist[j] = dist[t] + w[i];
      } else if (dist[j] != dist[t] + w[i])
        return false;
    }
  }
  return true;
}

int main() {
  memset(h, -1, sizeof h);
  scanf("%d%d", &n, &m);

  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    add(a, b, c), add(b, a, -c);
  }

  for (int i = 1; i <= n; i++)
    if (!st[i] && !bfs(i)) {
      puts("No");
      return 0;
    }
  puts("Yes");
  return 0;
}
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using lint = long long;
using real = long double;
int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout.setf(std::ios_base::fixed);
  std::cout.precision(15);
  lint n, m;
  std::cin >> n >> m;
  std::vector<std::vector<std::pair<lint, lint>>> g(n);
  while (m--) {
    lint l, r, d;
    std::cin >> l >> r >> d;
    l--, r--;
    g.at(l).emplace_back(r, d);
    g.at(r).emplace_back(l, -d);
  }
  lint inf = std::numeric_limits<lint>::max();
  std::vector<lint> dist(n, inf), que;
  auto dfs = [&](auto &&f, lint x) -> void {
    for (auto &&e : g.at(x)) {
      lint y, w;
      std::tie(y, w) = e;
      if (dist.at(y) != inf) {
        if (dist.at(y) != dist.at(x) + w) {
          std::cout << "No" << '\n';
          exit(0);
        }
        continue;
      }
      dist.at(y) = dist.at(x) + w;
      f(f, y);
    }
  };
  for (lint s = 0; s < n; s++) {
    if (dist.at(s) != inf)
      continue;
    dfs(dfs, s);
  }
  std::cout << "Yes" << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using lint = long long;
using real = long double;
int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout.setf(std::ios_base::fixed);
  std::cout.precision(15);
  lint n, m;
  std::cin >> n >> m;
  std::vector<std::vector<std::pair<lint, lint>>> g(n);
  while (m--) {
    lint l, r, d;
    std::cin >> l >> r >> d;
    l--, r--;
    g.at(l).emplace_back(r, d);
    g.at(r).emplace_back(l, -d);
  }
  lint inf = std::numeric_limits<lint>::max();
  std::vector<lint> dist(n, inf), que;
  auto dfs = [&](auto &&f, lint x) -> void {
    for (auto &&e : g.at(x)) {
      lint y, w;
      std::tie(y, w) = e;
      if (dist.at(y) != inf) {
        if (dist.at(y) != dist.at(x) + w) {
          std::cout << "No" << '\n';
          exit(0);
        }
        continue;
      }
      dist.at(y) = dist.at(x) + w;
      f(f, y);
    }
  };
  for (lint s = 0; s < n; s++) {
    if (dist.at(s) != inf)
      continue;
    dist.at(s) = 0;
    dfs(dfs, s);
  }
  std::cout << "Yes" << '\n';
}
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;

int used[100100];
vector<pair<ll, ll>> g[100100];
ll d[100100];

void dfs(int x) {
  used[x] = 1;
  for (auto p : g[x]) {
    if (d[p.first] == 0) {
      d[p.first] = d[x] + p.second;
      dfs(p.first);
    } else {
      if (d[p.first] != d[x] + p.second) {
        cout << "No" << endl;
        exit(0);
      }
    }
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    ll l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    g[l].push_back(make_pair(r, c));
    g[r].push_back(make_pair(l, -c));
  }
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      d[i] = 0;
      dfs(i);
    }
  }
  cout << "Yes" << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;

int used[100100];
vector<pair<ll, ll>> g[100100];
ll d[100100];

void dfs(int x) {
  used[x]++;
  if (used[x] > 2)
    return;
  for (auto p : g[x]) {
    if (d[p.first] == 0) {
      d[p.first] = d[x] + p.second;
      dfs(p.first);
    } else {
      if (d[p.first] != d[x] + p.second) {
        cout << "No" << endl;
        exit(0);
      }
    }
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    ll l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    g[l].push_back(make_pair(r, c));
    g[r].push_back(make_pair(l, -c));
  }
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      d[i] = 0;
      dfs(i);
    }
  }
  cout << "Yes" << endl;
}
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define P pair<int, int>

struct edge {
  int to, cost;
};

int main() {
  int const INF = 1000000100;
  int n, m;
  cin >> n >> m;
  int l[n], r[n], d[n];
  vector<edge> G[n];
  rep(i, m) {
    cin >> l[i] >> r[i] >> d[i];
    l[i]--;
    r[i]--;
    edge g;
    g.to = r[i];
    g.cost = d[i];
    G[l[i]].push_back(g);
    g.to = l[i];
    g.cost = -d[i];
    G[r[i]].push_back(g);
  }
  int x[n];
  rep(i, n) x[i] = INF;
  rep(i, n) {
    if (x[i] != INF) {
      continue;
    }
    x[i] = 0;
    queue<P> q;
    q.push(P(i, 0));
    while (q.size() > 0) {
      P p = q.front();
      q.pop();
      int dis = p.second;
      int e = p.first;
      rep(j, G[e].size()) {
        if (x[G[e][j].to] == INF) {
          x[G[e][j].to] = dis + G[e][j].cost;
          q.push(P(G[e][j].to, x[G[e][j].to]));
        }
      }
    }
  }
  // rep(i, n) cout << x[i] << " ";
  // cout << endl;
  rep(i, m) {
    if (x[r[i]] - x[l[i]] != d[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define P pair<int, int>

struct edge {
  int to, cost;
};

int main() {
  int const INF = 1000000100;
  int n, m;
  cin >> n >> m;
  int l[m], r[m], d[m];
  vector<edge> G[n];
  rep(i, m) {
    cin >> l[i] >> r[i] >> d[i];
    l[i]--;
    r[i]--;
    edge g;
    g.to = r[i];
    g.cost = d[i];
    G[l[i]].push_back(g);
    g.to = l[i];
    g.cost = -d[i];
    G[r[i]].push_back(g);
  }
  int x[n];
  rep(i, n) x[i] = INF;
  rep(i, n) {
    if (x[i] != INF) {
      continue;
    }
    x[i] = 0;
    queue<P> q;
    q.push(P(i, 0));
    while (q.size() > 0) {
      P p = q.front();
      q.pop();
      int dis = p.second;
      int e = p.first;
      rep(j, G[e].size()) {
        if (x[G[e][j].to] == INF) {
          x[G[e][j].to] = dis + G[e][j].cost;
          q.push(P(G[e][j].to, x[G[e][j].to]));
        }
      }
    }
  }
  // rep(i, n) cout << x[i] << " ";
  // cout << endl;
  rep(i, m) {
    if (x[r[i]] - x[l[i]] != d[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
}