Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using lli = long long int;
template <class T, class U> void init_n(vector<T> &v, size_t n, U x) {
  v = vector<T>(n, x);
}
template <class T> void init_n(vector<T> &v, size_t n) { init_n(v, n, T()); }
template <class T> void read_n(vector<T> &v, size_t n) {
  v = vector<T>(n);
  for (lli i = 0; i < n; ++i)
    cin >> v[i];
}
template <class T, class U> void read_n(vector<pair<T, U>> &v, size_t n) {
  v = vector<pair<T, U>>(n);
  for (lli i = 0; i < n; ++i)
    cin >> v[i].first >> v[i].second;
}
template <class T> T gabs(const T &x) { return max(x, -x); }
#define abs gabs

struct E {
  lli to, d;
  E(lli to, lli d) : to(to), d(d) {}
};
lli n, m;
vector<vector<E>> v;
vector<bool> vi;
vector<lli> x;

bool dfs(lli from, lli d) {
  if (vi[from])
    return d == x[from];
  vi[from] = true;
  x[from] = d;
  for (auto &&e : v[from]) {
    if (!dfs(e.to, d + e.d))
      return false;
  }
  return true;
}

int main() {
  cin >> n >> m;
  v = decltype(v)(n + 1);

  for (lli i = 0; i < m; ++i) {
    lli l, r, d;
    cin >> l >> r >> d;
    v[l].emplace_back(r, d);
    v[r].emplace_back(l, -d);
  }
  init_n(vi, n, false);
  init_n(x, n, 0);
  lli res = true;
  for (lli i = 1; i <= n; ++i) {
    res = res && dfs(i, x[i]);
  }
  cout << (res ? "Yes" : "No") << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using lli = long long int;
template <class T, class U> void init_n(vector<T> &v, size_t n, U x) {
  v = vector<T>(n, x);
}
template <class T> void init_n(vector<T> &v, size_t n) { init_n(v, n, T()); }
template <class T> void read_n(vector<T> &v, size_t n) {
  v = vector<T>(n);
  for (lli i = 0; i < n; ++i)
    cin >> v[i];
}
template <class T, class U> void read_n(vector<pair<T, U>> &v, size_t n) {
  v = vector<pair<T, U>>(n);
  for (lli i = 0; i < n; ++i)
    cin >> v[i].first >> v[i].second;
}
template <class T> T gabs(const T &x) { return max(x, -x); }
#define abs gabs

struct E {
  lli to, d;
  E(lli to, lli d) : to(to), d(d) {}
};
lli n, m;
vector<vector<E>> v;
vector<bool> vi;
vector<lli> x;

bool dfs(lli from, lli d) {
  if (vi[from])
    return d == x[from];
  vi[from] = true;
  x[from] = d;
  for (auto &&e : v[from]) {
    if (!dfs(e.to, d + e.d))
      return false;
  }
  return true;
}

int main() {
  cin >> n >> m;
  v = decltype(v)(n + 1);

  for (lli i = 0; i < m; ++i) {
    lli l, r, d;
    cin >> l >> r >> d;
    v[l].emplace_back(r, d);
    v[r].emplace_back(l, -d);
  }
  init_n(vi, n + 1, false);
  init_n(x, n + 1, 0);
  lli res = true;
  for (lli i = 1; i <= n; ++i) {
    res = res && dfs(i, x[i]);
  }
  cout << (res ? "Yes" : "No") << endl;
  return 0;
}