Problem p03452 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = a; i > b; i--)
#define repLL(i, a, b) for (LL i = (LL)a; i < (LL)b; i++)
#define perLL(i, a, b) for (LL i = (LL)a; i > (LL)b; i--)
#define debug5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " " << #b << ": " << b << " " << #c << ": " << c  \
       << " " << #d << ": " << d << " " << #e << ": " << e << endl;
#define debug4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " " << #b << ": " << b << " " << #c << ": " << c  \
       << " " << #d << ": " << d << endl;
#define debug3(a, b, c)                                                        \
  cerr << #a << ": " << a << " " << #b << ": " << b << " " << #c << ": " << c  \
       << endl;
#define debug2(a, b) cerr << #a << ": " << a << " " << #b << ": " << b << endl;
#define debug(a) cerr << #a << ": " << a << endl;
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(), x.end()
#define fi first
#define se second
#define _upgrade                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD, LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;

template <typename T> void dbg(T a) {
#ifdef LOCAL
  cerr << a << "\n";
#endif
}

template <typename T, typename... Arg> void dbg(T a, Arg... arg) {
#ifdef LOCAL
  cerr << a << " ";
  dbg(arg...);
#endif
}

const int maxn = (1e6) + 7;
const int inf = (1e9) + 7;
const LL LLinf = (1e18) + 7;
const LD eps = 1e-9;
const LL mod = 1e9 + 7;

// ***************************** CODE ***************************** //

VPII wek[maxn];
bool vis[maxn];
LL odl[maxn];
bool f = 0;

void dfs(int x) {
  vis[x] = 1;
  for (auto s : wek[x]) {
    if (vis[s.fi] == 0) {
      odl[s.fi] = odl[x] + s.se;
      dfs(s.fi);
    } else if (odl[s.fi] != odl[x + s.se])
      f = 1;
  }
}

int main() {
  _upgrade int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b, c;
    cin >> a >> b >> c;
    wek[a].pb({b, c});
    wek[b].pb({a, -c});
  }
  for (int i = 1; i <= n; i++)
    if (vis[i] == 0)
      dfs(i);
  if (f)
    cout << "No";
  else
    cout << "Yes";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = a; i > b; i--)
#define repLL(i, a, b) for (LL i = (LL)a; i < (LL)b; i++)
#define perLL(i, a, b) for (LL i = (LL)a; i > (LL)b; i--)
#define debug5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " " << #b << ": " << b << " " << #c << ": " << c  \
       << " " << #d << ": " << d << " " << #e << ": " << e << endl;
#define debug4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " " << #b << ": " << b << " " << #c << ": " << c  \
       << " " << #d << ": " << d << endl;
#define debug3(a, b, c)                                                        \
  cerr << #a << ": " << a << " " << #b << ": " << b << " " << #c << ": " << c  \
       << endl;
#define debug2(a, b) cerr << #a << ": " << a << " " << #b << ": " << b << endl;
#define debug(a) cerr << #a << ": " << a << endl;
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(), x.end()
#define fi first
#define se second
#define _upgrade                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD, LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;

template <typename T> void dbg(T a) {
#ifdef LOCAL
  cerr << a << "\n";
#endif
}

template <typename T, typename... Arg> void dbg(T a, Arg... arg) {
#ifdef LOCAL
  cerr << a << " ";
  dbg(arg...);
#endif
}

const int maxn = (1e6) + 7;
const int inf = (1e9) + 7;
const LL LLinf = (1e18) + 7;
const LD eps = 1e-9;
const LL mod = 1e9 + 7;

// ***************************** CODE ***************************** //

VPII wek[maxn];
bool vis[maxn];
LL odl[maxn];
bool f = 0;

void dfs(int x) {
  vis[x] = 1;
  for (auto s : wek[x]) {
    if (vis[s.fi] == 0) {
      odl[s.fi] = odl[x] + s.se;
      dfs(s.fi);
    } else if (odl[s.fi] != odl[x] + s.se)
      f = 1;
  }
}

int main() {
  _upgrade int n, m;
  cin >> n >> m;
  while (m--) {
    int a, b, c;
    cin >> a >> b >> c;
    wek[a].pb({b, c});
    wek[b].pb({a, -c});
  }
  for (int i = 1; i <= n; i++)
    if (vis[i] == 0)
      dfs(i);
  if (f)
    cout << "No";
  else
    cout << "Yes";
  return 0;
}