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

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

using namespace std;

// #define FILE_IO

int N;
int sum[2][1005];

int main() {
#ifdef FILE_IO
  freopen("1.in", "r", stdin);
  freopen("1.out", "w", stdout);
#endif

  scanf("%d", &N);
  for (int j = 1; j <= 2; j++)
    for (int i = 1; i <= N; i++) {
      int x;
      scanf("%d", &x);
      sum[j][i] = sum[j][i - 1] + x;
    }

  int ans = 0;
  for (int i = 1; i <= N; i++)
    ans = max(ans, sum[1][i] + sum[2][N] - sum[2][i - 1]);
  printf("%d\n", ans);

  return 0;
}


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

using namespace std;

// #define FILE_IO

int N;
int sum[5][1005];

int main() {
#ifdef FILE_IO
  freopen("1.in", "r", stdin);
  freopen("1.out", "w", stdout);
#endif

  scanf("%d", &N);
  for (int j = 1; j <= 2; j++)
    for (int i = 1; i <= N; i++) {
      int x;
      scanf("%d", &x);
      sum[j][i] = sum[j][i - 1] + x;
    }

  int ans = 0;
  for (int i = 1; i <= N; i++)
    ans = max(ans, sum[1][i] + sum[2][N] - sum[2][i - 1]);
  printf("%d\n", ans);

  return 0;
}
Problem p03451 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for (int i = l; i < r; i++)
#define vi vector<int>
#define all(v) v.begin(), v.end()
#define pb(c) push_back(c)
#define vii vector<pair<int, int>>
#define ii pair<int, int>
#define max(a, b) (((a) < (b)) ? b : a)
#define min(a, b) ((a > b) ? b : a)
#define mp(i, j) make_pair(i, j)
#define ull unsigned long long int
#define ll long long int
#define pi 3.141592653589793238
#define mod ((int)1e9 + 7)
#define inf ((int)1e5)
#define eps 1e-14,
#define maxlg 18
#define left(i) ((i) << 1)
#define right(i) (((i) << 1) + 1)
#define maxn 1051002
int can[3][103];
int dp[3][103];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#ifndef ONLINE_JUDGE
  freopen("input", "r", stdin); // flush(cout);
#endif
  int n;
  cin >> n;
  rep(i, 0, 2) {
    rep(j, 0, n) {
      int x;
      cin >> x;
      can[i][j] = x;
    }
  }

  for (int j = n - 1; j >= 0; j--) {
    for (int i = 1; i >= 0; i--) {
      dp[i][j] = can[i][j] + max(dp[i + 1][j], dp[i][j + 1]);
    }
  }
  cout << dp[0][0];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for (int i = l; i < r; i++)
#define vi vector<int>
#define all(v) v.begin(), v.end()
#define pb(c) push_back(c)
#define vii vector<pair<int, int>>
#define ii pair<int, int>
#define max(a, b) (((a) < (b)) ? b : a)
#define min(a, b) ((a > b) ? b : a)
#define mp(i, j) make_pair(i, j)
#define ull unsigned long long int
#define ll long long int
#define pi 3.141592653589793238
#define mod ((int)1e9 + 7)
#define inf ((int)1e5)
#define eps 1e-14,
#define maxlg 18
#define left(i) ((i) << 1)
#define right(i) (((i) << 1) + 1)
#define maxn 1051002
int can[3][103];
int dp[3][103];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#ifndef ONLINE_JUDGE
  // freopen("input", "r", stdin);//flush(cout);
#endif
  int n;
  cin >> n;
  rep(i, 0, 2) {
    rep(j, 0, n) {
      int x;
      cin >> x;
      can[i][j] = x;
    }
  }

  for (int j = n - 1; j >= 0; j--) {
    for (int i = 1; i >= 0; i--) {
      dp[i][j] = can[i][j] + max(dp[i + 1][j], dp[i][j + 1]);
    }
  }
  cout << dp[0][0];
  return 0;
}
Problem p03451 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>

using namespace std;

int a[3][110];
int dp[3][110];
int dx[] = {1, 0};
int dy[] = {0, 1};
int res;
int n;
bool go(int x, int y) { return x >= 0 && x < 3 && y >= 0 && y < n; }
void dfs(int x, int y, int ret) {
  if (x == 2 && y == n - 1) {
    res = max(res, ret);
    return;
  }
  for (int i = 0; i < 2; ++i) {
    int nx = x + dx[i], ny = y + dy[i];
    if (go(nx, ny))
      dfs(nx, ny, ret + a[nx][ny]);
  }
}
int main() {
  ios::sync_with_stdio(false);
  freopen("input.txt", "r", stdin);
  cin >> n;
  memset(a, 0, sizeof(a));
  memset(dp, 0, sizeof(dp));
  for (int k = 0; k < 2; ++k) {
    for (int i = 0; i < n; ++i) {
      cin >> a[k][i];
      dp[k + 1][i + 1] = a[k][i];
    }
  }
  res = 0;
  dfs(0, 0, a[0][0]);
  cout << res << endl;
  return 0;
}

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int a[3][110];
int dp[3][110];
int dx[] = {1, 0};
int dy[] = {0, 1};
int res;
int n;
bool go(int x, int y) { return x >= 0 && x < 3 && y >= 0 && y < n; }
void dfs(int x, int y, int ret) {
  if (x == 2 && y == n - 1) {
    res = max(res, ret);
    return;
  }
  for (int i = 0; i < 2; ++i) {
    int nx = x + dx[i], ny = y + dy[i];
    if (go(nx, ny))
      dfs(nx, ny, ret + a[nx][ny]);
  }
}
int main() {
  ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);
  cin >> n;
  memset(a, 0, sizeof(a));
  memset(dp, 0, sizeof(dp));
  for (int k = 0; k < 2; ++k) {
    for (int i = 0; i < n; ++i) {
      cin >> a[k][i];
      dp[k + 1][i + 1] = a[k][i];
    }
  }
  res = 0;
  dfs(0, 0, a[0][0]);
  cout << res << endl;
  return 0;
}
Problem p03452 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
#define N 100005
using namespace std;

int n, m, l[N * 2], r, d, a[N];
bool v[N];
vector<int> ve[N], vd[N];

void f(int p) {
  int i;
  for (i = 0; i < ve[p].size(); i++) {
    if (!v[ve[p][i]])
      v[ve[p][i]] = 1, a[ve[p][i]] = a[p] + vd[p][i], f(ve[p][i]);
    else if (a[p] + vd[p][i] != a[ve[p][i]]) {
      cout << "No";
      exit(0);
    }
  }
}

int main() {
  int i;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    scanf("%d %d %d", &l[i], &r, &d);
    ve[l[i]].push_back(r);
    vd[l[i]].push_back(d);
    ve[r].push_back(l[i]);
    vd[r].push_back(-d);
  }
  for (i = 0; i < m; i++)
    if (!v[i])
      v[i] = 1;
  f(i);
  cout << "Yes" << endl;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
#define N 100005
using namespace std;

int n, m, l[N * 2], r, d, a[N];
bool v[N];
vector<int> ve[N], vd[N];

void f(int p) {
  int i;
  for (i = 0; i < ve[p].size(); i++) {
    if (!v[ve[p][i]])
      v[ve[p][i]] = 1, a[ve[p][i]] = a[p] + vd[p][i], f(ve[p][i]);
    else if (a[p] + vd[p][i] != a[ve[p][i]]) {
      cout << "No";
      exit(0);
    }
  }
}

int main() {
  int i;
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    scanf("%d %d %d", &l[i], &r, &d);
    ve[l[i]].push_back(r);
    vd[l[i]].push_back(d);
    ve[r].push_back(l[i]);
    vd[r].push_back(-d);
  }
  for (i = 0; i < m; i++)
    if (!v[l[i]])
      v[l[i]] = 1, f(l[i]);
  cout << "Yes" << endl;
  return 0;
}
Problem p03452 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

struct edge {
  int to, cost;
  edge(int to, int cost) {
    this->to = to;
    this->cost = cost;
  }
};
const int MAX_V = 100000;
vector<edge> G[MAX_V];
int N, M;

bool visited[MAX_V];
int position[MAX_V];

bool dfs(int n, int x) {
  visited[n] = true;
  position[n] = x;

  bool isValid = true;
  for (edge e : G[n]) {
    if (visited[e.to] && position[e.to] != x + e.cost) {
      isValid = false;
    } else if (not(visited[e.to])) {
      isValid = dfs(e.to, x + e.cost);
    }
    if (not(isValid)) {
      break;
    }
  }
  return isValid;
}

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

  for (int i = 0; i < M; i++) {
    int left, right, dist;
    cin >> left;
    cin >> right;
    cin >> dist;
    left--;
    right--;
    G[left].push_back(edge(right, dist));
    G[right].push_back(edge(left, -dist));
  }

  bool isValid = true;
  for (int n = 0; n < N; n++) {
    if (not(visited[n])) {
      isValid = dfs(n, 0);
      if (not(isValid)) {
        break;
      }
    }
  }

  if (isValid) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}


Fixed submission:
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

struct edge {
  int to, cost;
  edge(int to, int cost) {
    this->to = to;
    this->cost = cost;
  }
};
const int MAX_V = 100000;
vector<edge> G[MAX_V];
int N, M;

bool visited[MAX_V];
int position[MAX_V];

bool dfs(int n, int x) {
  visited[n] = true;
  position[n] = x;

  bool isValid = true;
  for (edge e : G[n]) {
    if (visited[e.to] && position[e.to] != x + e.cost) {
      isValid = false;
    } else if (not(visited[e.to])) {
      isValid = dfs(e.to, x + e.cost);
    }
    if (not(isValid)) {
      break;
    }
  }
  return isValid;
}

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

  for (int i = 0; i < M; i++) {
    int left, right, dist;
    cin >> left;
    cin >> right;
    cin >> dist;
    left--;
    right--;
    G[left].push_back(edge(right, dist));
    G[right].push_back(edge(left, -dist));
  }

  bool isValid = true;
  for (int n = 0; n < N; n++) {
    if (not(visited[n])) {
      isValid = dfs(n, 0);
      if (not(isValid)) {
        break;
      }
    }
  }

  if (isValid) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}
Problem p03452 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
// #include <chrono> //1e+9で割る auto end= chrono::system_clock::now()-st;
// cout<<end.count()%1e+9<<endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef long double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)

int N, M;
int L[200000], R[200000], D[200000];
vector<P> el[100000], er[100000];
ll X[100000];

bool dfs(int id, ll a) {
  if (X[id] == -1) {
    X[id] = a;
    rep(i, el[id].size()) {
      if (!dfs(el[id][i].fr, a - el[id][i].sc))
        return 0;
    }
    rep(i, er[id].size()) {
      if (!dfs(er[id][i].fr, a + er[id][i].sc))
        return 0;
    }
    return 1;
  } else {
    if (X[id] != a)
      return 0;
    if (X[id] == a)
      return 1;
  }
}

int main() {
  cin >> N >> M;
  memset(X, -1, sizeof(X));
  rep(i, M) {
    cin >> L[i] >> R[i] >> D[i];
    el[R[i]].pb(P(L[i], D[i]));
    er[L[i]].pb(P(R[i], D[i]));
  }
  rep(i, N) {
    if (X[i] == -1 && !dfs(i, 1e+10)) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
// #include <chrono> //1e+9で割る auto end= chrono::system_clock::now()-st;
// cout<<end.count()%1e+9<<endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef long double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)

int N, M;
int L[200000], R[200000], D[200000];
vector<P> el[100000], er[100000];
ll X[100000];

bool dfs(int id, ll a) {
  if (X[id] == -1) {
    X[id] = a;
    rep(i, el[id].size()) {
      if (!dfs(el[id][i].fr, a - el[id][i].sc))
        return 0;
    }
    rep(i, er[id].size()) {
      if (!dfs(er[id][i].fr, a + er[id][i].sc))
        return 0;
    }
    return 1;
  } else {
    if (X[id] != a)
      return 0;
    if (X[id] == a)
      return 1;
  }
}

int main() {
  cin >> N >> M;
  memset(X, -1, sizeof(X));
  rep(i, M) {
    cin >> L[i] >> R[i] >> D[i];
    L[i]--;
    R[i]--;
    el[R[i]].pb(P(L[i], D[i]));
    er[L[i]].pb(P(R[i], D[i]));
  }
  rep(i, N) {
    if (X[i] == -1 && !dfs(i, 1e+10)) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
}
Problem p03452 (C++)

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

#define MAX_N 100001

vector<pair<int, int64_t>> adj[MAX_N];
int64_t dis[MAX_N];

void dfs(int v, int64_t d) {
  if (dis[v]) {
    if (dis[v] != d) {
      printf("No\n");
      exit(0);
    }
    return;
  }
  dis[v] = d;
  for (int i = 0; i < adj[v].size(); i++) {
    int u = adj[v][i].first;
    int64_t w = adj[v][i].second;
    dfs(u, d + w);
  }
}

int main(void) {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int l, r, d;
    scanf("%d%d%d", &l, &r, &d);
    adj[l].push_back(make_pair(r, d));
    adj[r].push_back(make_pair(l, -d));
  }
  for (int i = 1; i <= n; i++)
    if (!dis[i])
      dfs(i, 0);
  printf("Yes\n");
  return 0;
}


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

#define MAX_N 100001

vector<pair<int, int64_t>> adj[MAX_N];
int64_t dis[MAX_N];

void dfs(int v, int64_t d) {
  if (dis[v]) {
    if (dis[v] != d) {
      printf("No\n");
      exit(0);
    }
    return;
  }
  dis[v] = d;
  for (int i = 0; i < adj[v].size(); i++) {
    int u = adj[v][i].first;
    int64_t w = adj[v][i].second;
    dfs(u, d + w);
  }
}

int main(void) {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int l, r, d;
    scanf("%d%d%d", &l, &r, &d);
    adj[l].push_back(make_pair(r, d));
    adj[r].push_back(make_pair(l, -d));
  }
  for (int i = 1; i <= n; i++)
    if (!dis[i])
      dfs(i, 0xffff);
  printf("Yes\n");
  return 0;
}
Problem p03452 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = x; i <= y; ++i)
#define repd(i, x, y) for (register int i = x; i >= y; --i)
using namespace std;
typedef long long ll;
template <typename T> inline bool chkmin(T &x, T y) {
  return x > y ? x = y, 1 : 0;
}
template <typename T> inline bool chkmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}
template <typename T> inline void read(T &x) {
  x = 0;
  char c;
  int sign = 1;
  do {
    c = getchar();
    if (c == '-')
      sign = -1;
  } while (!isdigit(c));
  do {
    x = x * 10 + c - '0';
    c = getchar();
  } while (isdigit(c));
  x *= sign;
}

const int N = 1e5 + 500;
int tot, head[N], nxt[N << 1], to[N << 1], val[N << 1];
int n, m, d[N];
bool vis[N];

inline void add(int x, int y, int w) {
  val[tot] = w;
  to[tot] = y;
  nxt[tot] = head[x];
  head[x] = tot++;
}

bool dfs(int x) {
  vis[x] = 1;
  for (register int i = head[x]; ~i; i = nxt[i])
    if (chkmin(d[to[i]], d[x] + val[i])) {
      if (vis[to[i]])
        return false;
      if (!dfs(to[i]))
        return false;
    }
  vis[x] = 0;
  return true;
}

int main() {
  memset(d, 0x3f, sizeof d);
  memset(head, -1, sizeof head);
  read(n);
  read(m);
  rep(i, 1, m) {
    int a, b, c;
    read(a);
    read(b);
    read(c);
    add(a, b, c), add(b, a, -c);
  }

  rep(i, 1, n) add(0, i, 1);

  d[0] = 0;
  puts(dfs(0) ? "Yes" : "No");

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = x; i <= y; ++i)
#define repd(i, x, y) for (register int i = x; i >= y; --i)
using namespace std;
typedef long long ll;
template <typename T> inline bool chkmin(T &x, T y) {
  return x > y ? x = y, 1 : 0;
}
template <typename T> inline bool chkmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}
template <typename T> inline void read(T &x) {
  x = 0;
  char c;
  int sign = 1;
  do {
    c = getchar();
    if (c == '-')
      sign = -1;
  } while (!isdigit(c));
  do {
    x = x * 10 + c - '0';
    c = getchar();
  } while (isdigit(c));
  x *= sign;
}

const int N = 6e5 + 500;
int tot, head[N], nxt[N], to[N], val[N];
int n, m, d[N];
bool vis[N];

inline void add(int x, int y, int w) {
  val[tot] = w;
  to[tot] = y;
  nxt[tot] = head[x];
  head[x] = tot++;
}

bool dfs(int x) {
  vis[x] = 1;
  for (register int i = head[x]; ~i; i = nxt[i])
    if (chkmin(d[to[i]], d[x] + val[i])) {
      if (vis[to[i]])
        return false;
      if (!dfs(to[i]))
        return false;
    }
  vis[x] = 0;
  return true;
}

int main() {
  memset(d, 0x3f, sizeof d);
  memset(head, -1, sizeof head);
  read(n);
  read(m);
  rep(i, 1, m) {
    int a, b, c;
    read(a);
    read(b);
    read(c);
    add(a, b, c), add(b, a, -c);
  }

  rep(i, 1, n) add(0, i, 1);

  d[0] = 0;
  puts(dfs(0) ? "Yes" : "No");

  return 0;
}
Problem p03452 (C++)

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

using namespace std;

void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, int>>> g(n);
  for (int _ = 0; _ < m; _++) {
    int x, y, c;
    cin >> x >> y >> c;
    g[x].emplace_back(y, c);
    g[y].emplace_back(x, -c);
  }
  vector<long long> d(n);
  function<void(int)> dfs = [&](int u) {
    for (auto &p : g[u]) {
      int v, c;
      tie(v, c) = p;
      if (d[v]) {
        if (d[u] + c != d[v]) {
          cout << "No";
          exit(0);
        }
      } else {
        d[v] = d[u] + c;
        dfs(v);
      }
    }
  };
  for (int i = 0; i < n; i++) {
    if (!d[i]) {
      d[i] = 1ll << 40;
      dfs(i);
    }
  }
  cout << "Yes";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  cout << endl;
}


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

using namespace std;

void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, int>>> g(n);
  for (int _ = 0; _ < m; _++) {
    int x, y, c;
    cin >> x >> y >> c;
    x--;
    y--;
    g[x].emplace_back(y, c);
    g[y].emplace_back(x, -c);
  }
  vector<long long> d(n);
  function<void(int)> dfs = [&](int u) {
    for (auto &p : g[u]) {
      int v, c;
      tie(v, c) = p;
      if (d[v]) {
        if (d[u] + c != d[v]) {
          cout << "No";
          exit(0);
        }
      } else {
        d[v] = d[u] + c;
        dfs(v);
      }
    }
  };
  for (int i = 0; i < n; i++) {
    if (!d[i]) {
      d[i] = 1ll << 40;
      dfs(i);
    }
  }
  cout << "Yes";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  cout << endl;
}
Problem p03452 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
typedef long long ll;
struct Graph {
  vector<vector<pair<pair<int, ll>, bool>>> edge;
  int n;
  vector<ll> d;
  vector<int> flag;

  Graph(int _n) {
    n = _n;
    edge.resize(n);
    d.assign(n, -1);
    flag.assign(n, 0);
  }

  void add(int from, int to, ll cost) {
    edge[from].push_back(make_pair(make_pair(to, cost), 0));
  }

  bool dfs(int v) {
    bool f = true;
    flag[v] = true;
    rep(i, edge[v].size()) {
      if (edge[v][i].second)
        continue;
      edge[v][i].second = true;
      int w = edge[v][i].first.first;
      ll co = edge[v][i].first.second;
      if (d[w] == -1) {
        d[w] = d[v] + co;
        f = f && dfs(w);
      } else {
        if (d[w] - d[v] != co)
          return false;
        f = f && dfs(w);
      }
    }
    return f;
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(m), r(m);
  vector<ll> d(m);
  rep(i, m) {
    cin >> l[i] >> r[i] >> d[i];
    l[i]--;
    r[i]--;
  }

  Graph gp(n);
  rep(i, m) {
    gp.add(l[i], r[i], d[i]);
    gp.add(r[i], l[i], -d[i]);
  }

  bool f = true;
  rep(i, n) {
    if (gp.flag[i])
      continue;
    gp.d[i] = 0;
    f = f && gp.dfs(i);
  }

  cout << (f ? "Yes" : "No") << "\n";

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
typedef long long ll;
struct Graph {
  vector<vector<pair<pair<int, ll>, bool>>> edge;
  int n;
  vector<ll> d;
  vector<int> flag;

  Graph(int _n) {
    n = _n;
    edge.resize(n);
    d.assign(n, -1);
    flag.assign(n, 0);
  }

  void add(int from, int to, ll cost) {
    edge[from].push_back(make_pair(make_pair(to, cost), 0));
  }

  bool dfs(int v) {
    bool f = true;
    flag[v] = true;
    rep(i, edge[v].size()) {
      if (edge[v][i].second)
        continue;
      edge[v][i].second = true;
      int w = edge[v][i].first.first;
      ll co = edge[v][i].first.second;
      if (d[w] == -1) {
        d[w] = d[v] + co;
        f = f && dfs(w);
      } else {
        if (d[w] - d[v] != co)
          return false;
      }
    }
    return f;
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> l(m), r(m);
  vector<ll> d(m);
  rep(i, m) {
    cin >> l[i] >> r[i] >> d[i];
    l[i]--;
    r[i]--;
  }

  Graph gp(n);
  rep(i, m) {
    gp.add(l[i], r[i], d[i]);
    gp.add(r[i], l[i], -d[i]);
  }

  bool f = true;
  rep(i, n) {
    if (gp.flag[i])
      continue;
    gp.d[i] = 0;
    f = f && gp.dfs(i);
  }

  cout << (f ? "Yes" : "No") << "\n";

  return 0;
}
Problem p03452 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define inf 0x7f7f7f7f
using namespace std;
const int N = 100005, M = 200005;
int l, r, d, n, m, x[N];
int counter = 1;
int head[N], nxt[M], to[M], val[M], cnt, dis[N], vis[N];
void add(int u, int v, int w) {
  to[cnt] = v;
  val[cnt] = w;
  nxt[cnt] = head[u];
  head[u] = cnt++;
}
bool spfa(int u) {
  queue<int> que;
  que.push(u);
  dis[u] = 0;
  vis[u] = 1;
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    for (int i = head[now]; ~i; i = nxt[i]) {
      int v = to[i];
      if (!vis[v]) {
        dis[v] = dis[now] + val[i];
        que.push(v);
        vis[v] = 1;
      } else if (dis[v] != dis[now] + val[i]) {
        return false;
      }
    }
  }
  return true;
}
int main() {
  memset(head, -1, sizeof(head));
  memset(dis, 127, sizeof(dis));
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d", &l, &r, &d);
    add(l, r, d);
    add(r, l, -d);
  }
  int flag = 0;
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      if (!spfa(i))
        flag = 1;
    }
  }
  if (flag)
    printf("No\n");
  else
    printf("Yes\n");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define inf 0x7f7f7f7f
using namespace std;
const int N = 100005, M = 200005;
int l, r, d, n, m, x[N];
int counter = 1;
int head[N], nxt[M * 2], to[M * 2], val[M * 2], cnt, dis[N], vis[N];
void add(int u, int v, int w) {
  to[cnt] = v;
  val[cnt] = w;
  nxt[cnt] = head[u];
  head[u] = cnt++;
}
bool spfa(int u) {
  queue<int> que;
  que.push(u);
  dis[u] = 0;
  vis[u] = 1;
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    for (int i = head[now]; ~i; i = nxt[i]) {
      int v = to[i];
      if (!vis[v]) {
        dis[v] = dis[now] + val[i];
        que.push(v);
        vis[v] = 1;
      } else if (dis[v] != dis[now] + val[i]) {
        return false;
      }
    }
  }
  return true;
}
int main() {
  memset(head, -1, sizeof(head));
  memset(dis, 127, sizeof(dis));
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d", &l, &r, &d);
    add(l, r, d);
    add(r, l, -d);
  }
  int flag = 0;
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      if (!spfa(i))
        flag = 1;
    }
  }
  if (flag)
    printf("No\n");
  else
    printf("Yes\n");
  return 0;
}
Problem p03452 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
const int INF = 1000000009;

int n, m;
typedef std::pair<int, int> P;
std::vector<P> know[100000];
int pos[100000];
bool used[100000];

bool fill(int a, int p) {
  if (used[a] == true) {
    if (pos[a] == p) {
      return true;
    } else {
      return false;
    }
  } else {
    used[a] = true;
    pos[a] = p;
    for (int i = 0; i < know[a].size(); i++) {
      if (fill(know[a][i].first, p + know[a][i].second) == false) {
        return false;
      }
    }
    return true;
  }
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    know[a].push_back(P(b, c));
    know[b].push_back(P(a, -c));
  }
  for (int i = 0; i < n; i++) {
    pos[i] = INF;
  }
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      if (!fill(i, 0)) {
        printf("No\n");
        return 0;
      }
    }
  }
  printf("Yes\n");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
const int INF = 1000000009;

int n, m;
typedef std::pair<int, int> P;
std::vector<P> know[100000];
int pos[100000];
bool used[100000];

bool fill(int a, int p) {
  if (used[a] == true) {
    if (pos[a] == p) {
      return true;
    } else {
      return false;
    }
  } else {
    used[a] = true;
    pos[a] = p;
    for (int i = 0; i < know[a].size(); i++) {
      if (fill(know[a][i].first, p + know[a][i].second) == false) {
        return false;
      }
    }
    return true;
  }
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--;
    b--;
    know[a].push_back(P(b, c));
    know[b].push_back(P(a, -c));
  }
  for (int i = 0; i < n; i++) {
    pos[i] = INF;
  }
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      if (!fill(i, 0)) {
        printf("No\n");
        return 0;
      }
    }
  }
  printf("Yes\n");
  return 0;
}
Problem p03452 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, s, e) for (int i = s; i < (e); i++)
#define All(v) (v).begin(), (v).end()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
using namespace std;
typedef long long llint;
typedef pair<int, int> P;
const int MOD = (int)1e9 + 7;
const int INF = numeric_limits<int>::max();

struct edge {
  int to;
  int cost;
  edge(int to, int cost) : to(to), cost(cost) {}
};
typedef vector<vector<edge>> Graph;

void dfs(Graph &G, int st, vector<int> &x) {
  x[st] = 0;

  stack<int> S;
  int N = G.size();
  static vector<vector<edge>::iterator> itr(N);
  if (st == 0)
    REP(i, N) { itr[i] = G[i].begin(); }
  vector<int> done(N, 0);
  S.push(st);
  while (!S.empty()) {
    int u = S.top();
    if (itr[u] == G[u].end()) {
      S.pop();
      done[u] = 2;
    } else {
      edge v = *itr[u]++;
      if (!done[v.to]) {
        done[v.to] = 1;
        S.push(v.to);
        x[v.to] = x[u] + v.cost;
      }
    }
  }
}

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

  int N, M;
  cin >> N >> M;

  Graph G(N);
  REP(i, M) {
    int l, r, d;
    cin >> l >> r >> d;
    l--;
    r--;
    G[l].pb(edge(r, d));
    G[r].pb(edge(l, -d));
  }
  vector<int> x(N, INF);
  REP(i, N) {
    if (x[i] == INF)
      dfs(G, i, x);
  }
  REP(l, N) {
    // cout << x[l] << "\n";
    for (auto e : G[l]) {
      if (x[e.to] - x[l] != e.cost) {
        puts("No");
        return 0;
      }
    }
  }
  puts("Yes");

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, s, e) for (int i = s; i < (e); i++)
#define All(v) (v).begin(), (v).end()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
using namespace std;
typedef long long llint;
typedef pair<int, int> P;
const int MOD = (int)1e9 + 7;
const int INF = numeric_limits<int>::max();

struct edge {
  int to;
  int cost;
  edge(int to, int cost) : to(to), cost(cost) {}
};
typedef vector<vector<edge>> Graph;

void dfs(Graph &G, int st, vector<int> &x) {
  x[st] = 0;

  stack<int> S;
  int N = G.size();
  static vector<vector<edge>::iterator> itr(N);
  if (st == 0)
    REP(i, N) { itr[i] = G[i].begin(); }
  static vector<int> done(N, 0);
  S.push(st);
  while (!S.empty()) {
    int u = S.top();
    if (itr[u] == G[u].end()) {
      S.pop();
      done[u] = 2;
    } else {
      edge v = *itr[u]++;
      if (!done[v.to]) {
        done[v.to] = 1;
        S.push(v.to);
        x[v.to] = x[u] + v.cost;
      }
    }
  }
}

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

  int N, M;
  cin >> N >> M;

  Graph G(N);
  REP(i, M) {
    int l, r, d;
    cin >> l >> r >> d;
    l--;
    r--;
    G[l].pb(edge(r, d));
    G[r].pb(edge(l, -d));
  }
  vector<int> x(N, INF);
  REP(i, N) {
    if (x[i] == INF)
      dfs(G, i, x);
  }
  REP(l, N) {
    // cout << x[l] << "\n";
    for (auto e : G[l]) {
      if (x[e.to] - x[l] != e.cost) {
        puts("No");
        return 0;
      }
    }
  }
  puts("Yes");

  return 0;
}
Problem p03452 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define M 100010
#define LL long long
#define inf 2147483647
#define rep(i, x, y) for (int i = (x); i <= (y); ++i)
inline int read() {
  char ch = getchar();
  int x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
struct Edge {
  int u, v, w, Next;
} G[M * 2];
int head[M], tot;
inline void add(int u, int v, int w) {
  G[++tot] = (Edge){u, v, w, head[u]};
  head[u] = tot;
}
int d[M];
bool vis[M];
inline void dfs(int x) {
  vis[x] = 1;
  for (int i = head[x]; i != -1; i = G[i].Next) {
    if (d[G[i].v] != inf && d[x] + G[i].w != d[G[i].v]) {
      puts("No");
      exit(0);
    }
    if (!vis[G[i].v]) {
      d[G[i].v] = d[x] + G[i].w;
      dfs(G[i].v);
    }
  }
}
int main() {
  // freopen("in.txt", "r", stdin);
  int n = read(), m = read();
  memset(head, -1, sizeof(head));
  rep(i, 1, n) d[i] = inf;
  rep(i, 1, m) {
    int u = read(), v = read(), D = read();
    add(u, v, D);
    add(v, u, -D);
  }
  rep(i, 1, n) if (!vis[i]) { dfs(i); }
  puts("Yes");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define M 200010
#define LL long long
#define inf 2147483647
#define rep(i, x, y) for (int i = (x); i <= (y); ++i)
inline int read() {
  char ch = getchar();
  int x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
struct Edge {
  int u, v, w, Next;
} G[M * 2];
int head[M], tot;
inline void add(int u, int v, int w) {
  G[++tot] = (Edge){u, v, w, head[u]};
  head[u] = tot;
}
int d[M];
bool vis[M];
inline void dfs(int x) {
  vis[x] = 1;
  for (int i = head[x]; i != -1; i = G[i].Next) {
    if (d[G[i].v] != inf && d[x] + G[i].w != d[G[i].v]) {
      puts("No");
      exit(0);
    }
    if (!vis[G[i].v]) {
      d[G[i].v] = d[x] + G[i].w;
      dfs(G[i].v);
    }
  }
}
int main() {
  // freopen("in.txt", "r", stdin);
  int n = read(), m = read();
  memset(head, -1, sizeof(head));
  rep(i, 1, n) d[i] = inf;
  rep(i, 1, m) {
    int u = read(), v = read(), D = read();
    add(u, v, D);
    add(v, u, -D);
  }
  rep(i, 1, n) if (!vis[i]) { dfs(i); }
  puts("Yes");
  return 0;
}
Problem p03452 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define int long long
int MOD = 1000000007;
vector<vector<int>> ed;
vector<vector<int>> dd;
vector<int> v;
vector<int> X;
int dfs(int a, int p) {
  v[a] = 1;
  for (int i = 0; i < ed[a].size(); i++) {
    if (v[ed[a][i]] == 0) {
      X[ed[a][i]] = X[a] + dd[a][i];
      dfs(ed[a][i], a);
    }
  }
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<int> L(N);
  vector<int> R(N);
  vector<int> D(N);
  ed.resize(N);
  dd.resize(N);
  v.resize(N, 0);
  X.resize(N, 0);
  int res = 0;
  for (int i = 0; i < M; i++) {
    cin >> L[i] >> R[i] >> D[i];
    L[i]--;
    R[i]--;
    ed[L[i]].push_back(R[i]);
    dd[L[i]].push_back(D[i]);
    ed[R[i]].push_back(L[i]);
    dd[R[i]].push_back(-D[i]);
  }
  for (int i = 0; i < N; i++) {
    if (v[i] == 0) {
      X[i] = 0;
      dfs(i, -1);
    }
  }
  bool f = true;
  for (int i = 0; i < M; i++) {
    if (X[L[i]] + D[i] != X[R[i]]) {
      f = false;
    }
  }
  if (f) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define int long long
int MOD = 1000000007;
vector<vector<int>> ed;
vector<vector<int>> dd;
vector<int> v;
vector<int> X;
int dfs(int a, int p) {
  v[a] = 1;
  for (int i = 0; i < ed[a].size(); i++) {
    if (v[ed[a][i]] == 0) {
      X[ed[a][i]] = X[a] + dd[a][i];
      dfs(ed[a][i], a);
    }
  }
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<int> L(M);
  vector<int> R(M);
  vector<int> D(M);
  ed.resize(N);
  dd.resize(N);
  v.resize(N, 0);
  X.resize(N, 0);
  int res = 0;
  for (int i = 0; i < M; i++) {
    cin >> L[i] >> R[i] >> D[i];
    L[i]--;
    R[i]--;
    ed[L[i]].push_back(R[i]);
    dd[L[i]].push_back(D[i]);
    ed[R[i]].push_back(L[i]);
    dd[R[i]].push_back(-D[i]);
  }
  for (int i = 0; i < N; i++) {
    if (v[i] == 0) {
      X[i] = 0;
      dfs(i, -1);
    }
  }
  bool f = true;
  for (int i = 0; i < M; i++) {
    if (X[L[i]] + D[i] != X[R[i]]) {
      f = false;
    }
  }
  if (f) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}
Problem p03452 (C++)

Buggy submission (status: Runtime Error):
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
struct edge {
  int to, cost;
};
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<edge>> G(N);
  for (int i = 0; i < N; ++i) {
    int L, R, D;
    cin >> L >> R >> D;
    --L, --R;
    G[L].push_back(edge{R, D});
    G[R].push_back(edge{L, -D});
  }
  vector<bool> vis(N);
  vector<long long> pot(N);
  function<bool(int)> dfs = [&](int pos) {
    vis[pos] = true;
    for (edge e : G[pos]) {
      if (vis[e.to] && e.cost + pot[pos] != pot[e.to]) {
        return false;
      }
      if (!vis[e.to]) {
        pot[e.to] = pot[pos] + e.cost;
        bool res = dfs(e.to);
        if (!res)
          return false;
      }
    }
    return true;
  };
  bool ans = true;
  for (int i = 0; i < N; ++i) {
    if (!vis[i]) {
      ans = ans && dfs(i);
    }
  }
  cout << (ans ? "Yes" : "No") << endl;
  return 0;
}

Fixed submission:
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
struct edge {
  int to, cost;
};
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<edge>> G(N);
  for (int i = 0; i < M; ++i) {
    int L, R, D;
    cin >> L >> R >> D;
    --L, --R;
    G[L].push_back(edge{R, D});
    G[R].push_back(edge{L, -D});
  }
  vector<bool> vis(N);
  vector<long long> pot(N);
  function<bool(int)> dfs = [&](int pos) {
    vis[pos] = true;
    for (edge e : G[pos]) {
      if (vis[e.to] && e.cost + pot[pos] != pot[e.to]) {
        return false;
      }
      if (!vis[e.to]) {
        pot[e.to] = pot[pos] + e.cost;
        bool res = dfs(e.to);
        if (!res)
          return false;
      }
    }
    return true;
  };
  bool ans = true;
  for (int i = 0; i < N; ++i) {
    if (!vis[i]) {
      ans = ans && dfs(i);
    }
  }
  cout << (ans ? "Yes" : "No") << endl;
  return 0;
}
Problem p03452 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define ll long long
#define dbg(v) cerr << #v << " = " << v << '\n'
#define vi vector<int>
#define vl vector<ll>
#define pii pair<int, int>
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(), s.end()
template <class T> T smin(T &a, T b) {
  if (a > b)
    a = b;
  return a;
}
template <class T> T smax(T &a, T b) {
  if (a < b)
    a = b;
  return a;
}
const int N = (int)(1e6) + 5;
vector<pii> g[N];
const int oo = 1e9 + 69;
int D[N];
int main(void) {
  int n, m;
  ios_base ::sync_with_stdio(0);
  cin >> n >> m;
  vector<vi> v(m, vi(3, 0));
  for (auto &it : v)
    for (int i = 0; i < 3; ++i)
      cin >> it[i];
  for (auto it : v)
    g[it[0]].pb(mp(it[1], it[2])), g[it[1]].pb(mp(it[0], -it[2]));
  for (int i = 1; i <= n; ++i)
    D[i] = -oo;
  for (int i = 1; i <= n; ++i)
    if (D[i] == -oo) {
      D[i] = 0;
      queue<int> Q;
      Q.push(i);
      while (!Q.empty()) {
        int node = Q.front();
        Q.pop();
        for (auto it : g[node])
          if (D[it.fi] == -oo)
            D[it.fi] = D[node] + it.se, Q.push(it.fi);
      }
    }
  for (auto it : v)
    if (D[it[1]] - D[it[0]] != it[2])
      return puts("No");
  puts("Yes");
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define ll long long
#define dbg(v) cerr << #v << " = " << v << '\n'
#define vi vector<int>
#define vl vector<ll>
#define pii pair<int, int>
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(), s.end()
template <class T> T smin(T &a, T b) {
  if (a > b)
    a = b;
  return a;
}
template <class T> T smax(T &a, T b) {
  if (a < b)
    a = b;
  return a;
}
const int N = (int)(1e6) + 5;
vector<pii> g[N];
const int oo = 1e9 + 69;
int D[N];
int main(void) {
  int n, m;
  ios_base ::sync_with_stdio(0);
  cin >> n >> m;
  vector<vi> v(m, vi(3, 0));
  for (auto &it : v)
    for (int i = 0; i < 3; ++i)
      cin >> it[i];
  for (auto it : v)
    g[it[0]].pb(mp(it[1], it[2])), g[it[1]].pb(mp(it[0], -it[2]));
  for (int i = 1; i <= n; ++i)
    D[i] = -oo;
  for (int i = 1; i <= n; ++i)
    if (D[i] == -oo) {
      D[i] = 0;
      queue<int> Q;
      Q.push(i);
      while (!Q.empty()) {
        int node = Q.front();
        Q.pop();
        for (auto it : g[node])
          if (D[it.fi] == -oo)
            D[it.fi] = D[node] + it.se, Q.push(it.fi);
      }
    }
  for (auto it : v)
    if (D[it[1]] - D[it[0]] != it[2])
      return puts("No") * 0;
  puts("Yes");
  return 0;
}
Problem p03452 (C++)

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

using namespace std;

#define X first
#define Y second
#define ll long long
#define inf (ll)1e18
#define all(x) x.begin(), x.end()

const int MX = 200020;
const int N = 1000060;
const int mood = ((int)1e9 + 7);

int bst[MX];
bool in[MX];
vector<pair<int, int>> g[MX];

void dfs(int x, int cost) {
  for (auto y : g[x]) {
    if (bst[y.X] == -1) {
      bst[y.X] = cost + y.Y;
      dfs(y.X, cost + y.Y);
    } else if (bst[y.X] != cost + y.Y) {
      cout << "No" << endl;
      exit(0);
    }
  }
}

void remove(int x) {

  for (auto y : g[x]) {
    bst[y.X] = -1;
    remove(y.X);
  }
}

int main() {
  /*
      Hossam ^_^
       ___     ___
      (^_^)   (^_^)
     /( | )\ /( | )\
       | |     | |
  */
  cin.tie(0);
  cin.sync_with_stdio(0);

#ifdef Cyborg101
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

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

  if (m == 0) {
    cout << "Yes" << endl;
    return 0;
  }

  while (m--) {
    int a, b, d;
    cin >> a >> b >> d;
    --a, --b;
    g[a].push_back({b, d});
    in[b] = true;
  }

  memset(bst, 0xff, sizeof bst);
  bool found = false;
  for (int i = 0; i < n; ++i) {
    if ((int)g[i].size() && !in[i]) {
      dfs(i, 0);
      found = true;
      remove(i);
    }
  }

  if (found)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}

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

using namespace std;

#define X first
#define Y second
#define ll long long
#define inf (ll)1e18
#define all(x) x.begin(), x.end()

const int MX = 200020;
const int N = 1000060;
const int mood = ((int)1e9 + 7);

int bst[MX];
bool in[MX];
vector<pair<int, int>> g[MX];

void dfs(int x, int cost) {
  for (auto y : g[x]) {
    if (bst[y.X] == -1) {
      bst[y.X] = cost + y.Y;
      dfs(y.X, cost + y.Y);
    } else if (bst[y.X] != cost + y.Y) {
      cout << "No" << endl;
      exit(0);
    }
  }
}

void remove(int x) {

  for (auto y : g[x]) {
    if (bst[y.X] != -1) {
      bst[y.X] = -1;
      remove(y.X);
    }
  }
}

int main() {
  /*
      Hossam ^_^
       ___     ___
      (^_^)   (^_^)
     /( | )\ /( | )\
       | |     | |
  */
  cin.tie(0);
  cin.sync_with_stdio(0);

#ifdef Cyborg101
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

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

  if (m == 0) {
    cout << "Yes" << endl;
    return 0;
  }

  while (m--) {
    int a, b, d;
    cin >> a >> b >> d;
    --a, --b;
    g[a].push_back({b, d});
    in[b] = true;
  }

  memset(bst, 0xff, sizeof bst);
  bool found = false;
  for (int i = 0; i < n; ++i) {
    if ((int)g[i].size() && !in[i]) {
      dfs(i, 0);
      found = true;
      remove(i);
    }
  }

  if (found)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}
Problem p03452 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define Set(a, v) memset(a, v, sizeof(a))
#define For(i, l, r) for (int i = (l); i <= (int)(r); ++i)
#define Fordown(i, r, l) for (int i = (r); i >= (int)(l); --i)
#define mid ((l + r) >> 1)
#define lson h << 1, l, mid
#define rson h << 1 | 1, mid + 1, r
using namespace std;

inline int read() {
  int x = 0, fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fh = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ '0');
  return x * fh;
}

const int M = 100100 << 1, N = 100100;
int to[M], Head[N], Next[M], val[M], e;
void add_edge(int u, int v, int w) {
  to[++e] = v;
  val[e] = w;
  Next[e] = Head[u];
  Head[u] = e;
}

bool vis[N], flag;
int dis[N];
void bellman_ford(int u) {
  vis[u] = true;
  for (int i = Head[u]; i; i = Next[i]) {
    int v = to[i];
    if (dis[v] > dis[u] + val[i]) {
      if (vis[v]) {
        flag = true;
        return;
      }
      dis[v] = dis[u] + val[i];
      bellman_ford(v);
    }
  }
  vis[u] = false;
}

void init() {
  e = 0;
  Set(Head, 0);
  Set(vis, false);
  Set(dis, 0x3f);
  flag = false;
}

int main() {
  init();
  int n = read(), m = read();
  while (m--) {
    int l = read(), r = read(), s = read();
    add_edge(l, r, s);
    add_edge(r, l, -s);
  }
  For(i, 1, n) {
    if (!vis[i])
      bellman_ford(i);
    if (flag)
      break;
  }
  printf("%s\n", !flag ? "Yes" : "No");
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define Set(a, v) memset(a, v, sizeof(a))
#define For(i, l, r) for (int i = (l); i <= (int)(r); ++i)
#define Fordown(i, r, l) for (int i = (r); i >= (int)(l); --i)
#define mid ((l + r) >> 1)
#define lson h << 1, l, mid
#define rson h << 1 | 1, mid + 1, r
using namespace std;

inline int read() {
  int x = 0, fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fh = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ '0');
  return x * fh;
}

const int M = 200100 << 1, N = 200100;
int to[M], Head[N], Next[M], val[M], e;
void add_edge(int u, int v, int w) {
  to[++e] = v;
  val[e] = w;
  Next[e] = Head[u];
  Head[u] = e;
}

bool vis[N], flag;
int dis[N];
void bellman_ford(int u) {
  vis[u] = true;
  for (int i = Head[u]; i; i = Next[i]) {
    int v = to[i];
    if (dis[v] > dis[u] + val[i]) {
      if (vis[v]) {
        flag = true;
        return;
      }
      dis[v] = dis[u] + val[i];
      bellman_ford(v);
    }
  }
  vis[u] = false;
}

void init() {
  e = 0;
  Set(Head, 0);
  Set(vis, false);
  Set(dis, 0x3f);
  flag = false;
}

int main() {
  init();
  int n = read(), m = read();
  while (m--) {
    int l = read(), r = read(), s = read();
    add_edge(l, r, s);
    add_edge(r, l, -s);
  }
  For(i, 1, n) {
    if (!vis[i])
      bellman_ford(i);
    if (flag)
      break;
  }
  printf("%s\n", !flag ? "Yes" : "No");
}
Problem p03452 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define pb push_back
using namespace std;
typedef pair<int, int> pii;

int n, m;
vector<vector<pii>> g;
bool hako[111111];
int kyori[111111];

bool dfs(int x, int d) {
  if (kyori[x] == -1)
    kyori[x] = d;
  else if (kyori[x] != d)
    return false;

  for (int i = 0; i < g[x].size(); i++) {
    if (!dfs(g[x][i].first, d + g[x][i].second))
      return false;
  }
  return true;
}

int main(void) {
  cin >> n >> m;
  int l, r, d;
  g.resize(n + 1, vector<pii>());
  memset(kyori, -1, sizeof kyori);

  for (int i = 0; i < m; i++) {
    cin >> l >> r >> d;
    g[l].pb(pii(r, d));
    hako[r] = true;
  }

  bool f = true;
  for (int i = 1; i <= n; i++) {
    if (hako[i])
      continue;
    if (!dfs(i, 0)) {
      f = false;
      break;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (kyori[i] == -1)
      f = false;
  }
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define pb push_back
using namespace std;
typedef pair<int, int> pii;

int n, m;
vector<vector<pii>> g;
bool hako[111111];
int kyori[111111];

bool dfs(int x, int d) {
  if (kyori[x] == -1)
    kyori[x] = d;
  else if (kyori[x] != d)
    return false;
  else
    return true;

  for (int i = 0; i < g[x].size(); i++) {
    if (!dfs(g[x][i].first, d + g[x][i].second))
      return false;
  }
  return true;
}

int main(void) {
  cin >> n >> m;
  int l, r, d;
  g.resize(n + 1, vector<pii>());
  memset(kyori, -1, sizeof kyori);

  for (int i = 0; i < m; i++) {
    cin >> l >> r >> d;
    g[l].pb(pii(r, d));
    hako[r] = true;
  }

  bool f = true;
  for (int i = 1; i <= n; i++) {
    if (hako[i])
      continue;
    if (!dfs(i, 0)) {
      f = false;
      break;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (kyori[i] == -1)
      f = false;
  }
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}