Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#define LOCAL
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, s, n) for (int i = s; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
static const int INTINF = (INT_MAX >> 1); // 10^9 + 10^7
static const ll LLINF = (LLONG_MAX >> 1);
static const int MAX = 1e5 + 1;
static const ll MOD = 1e9 + 7;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
string to_string(string s) { return '"' + s + '"'; }
string to_string(bool b) { return b ? "true" : "false"; }
template <size_t N> string to_string(bitset<N> bs) {
  string res;
  for (size_t i = 0; i < N; ++i)
    res += '0' + bs[i];
  return res;
}
string to_string(vector<bool> v) {
  string res = "{";
  for (bool e : v)
    res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p);
template <class C> string to_string(C c) {
  string res = "{";
  for (auto e : c)
    res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void debug() { cerr << '\n'; }
template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  cerr << '\t' << to_string(head), debug(tail...);
}
#ifdef LOCAL
#define DEBUG(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

struct edge {
  int to, cost;
};
vector<edge> g[MAX];
int N, M;

bool used[MAX];
int d[MAX];
bool ans;

void bfs(int v, int dist) {
  if (used[v] && d[v] != dist) {
    // DEBUG(v);
    // DEBUG(dist);
    ans = false;
  }
  if (used[v])
    return;
  d[v] = dist;
  used[v] = true;
  for (edge u : g[v]) {
    bfs(u.to, dist + u.cost);
  }
}

int main(int argc, const char *argv[]) {
  std::cout << std::fixed << std::setprecision(15);

  cin >> N >> M;
  ans = true;
  rep(i, M) {
    int l, r, d;
    cin >> l >> r >> d;
    l--;
    r--;
    g[l].push_back({r, d});
    g[r].push_back({l, -d});
  }

  memset(d, -1, sizeof(d));
  memset(used, 0, sizeof(d));
  rep(i, N) {
    if (used[i])
      continue;
    bfs(i, 0);
  }

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


Fixed submission:
#define LOCAL
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, s, n) for (int i = s; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
static const int INTINF = (INT_MAX >> 1); // 10^9 + 10^7
static const ll LLINF = (LLONG_MAX >> 1);
static const int MAX = 1e5 + 1;
static const ll MOD = 1e9 + 7;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
string to_string(string s) { return '"' + s + '"'; }
string to_string(bool b) { return b ? "true" : "false"; }
template <size_t N> string to_string(bitset<N> bs) {
  string res;
  for (size_t i = 0; i < N; ++i)
    res += '0' + bs[i];
  return res;
}
string to_string(vector<bool> v) {
  string res = "{";
  for (bool e : v)
    res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p);
template <class C> string to_string(C c) {
  string res = "{";
  for (auto e : c)
    res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void debug() { cerr << '\n'; }
template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  cerr << '\t' << to_string(head), debug(tail...);
}
#ifdef LOCAL
#define DEBUG(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

struct edge {
  int to, cost;
};
vector<edge> g[MAX];
int N, M;

bool used[MAX];
int d[MAX];
bool ans;

void bfs(int v, int dist) {
  if (used[v] && d[v] != dist) {
    // DEBUG(v);
    // DEBUG(dist);
    ans = false;
  }
  if (used[v])
    return;
  d[v] = dist;
  used[v] = true;
  for (edge u : g[v]) {
    bfs(u.to, dist + u.cost);
  }
}

int main(int argc, const char *argv[]) {
  std::cout << std::fixed << std::setprecision(15);

  cin >> N >> M;
  ans = true;
  rep(i, M) {
    int l, r, d;
    cin >> l >> r >> d;
    l--;
    r--;
    g[l].push_back({r, d});
    g[r].push_back({l, -d});
  }

  memset(d, -1, sizeof(d));
  memset(used, 0, sizeof(used));
  rep(i, N) {
    if (used[i])
      continue;
    bfs(i, 0);
  }

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