Problem p03306 (C++)

Buggy submission (status: Runtime Error):
// header
#ifdef LOCAL
#include "cxx-prettyprint-master/prettyprint.hpp"
#define print(x) cout << x << endl
#else
#define print(...) 42
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// types
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
template <typename T> using mat = vector<vector<T>>;
template <std::uint_fast64_t Modulus> class modint {
  using u64 = std::uint_fast64_t;

public:
  u64 a;

  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    u64 exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};
// abbreviations
#define all(x) (x).begin(), (x).end()
#define reverse(x) (x).rbegin(), (x).rend()
#define rep2(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define SZ(x) ((ll)(x).size())
#define pb(x) push_back(x)
#define ff first
#define ss second
#define mp make_pair
// functions
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> T mypow(T x, ll n) {
  T ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x);
    (x *= x);
    n >>= 1;
  }
  return ret;
}
uint64_t my_rand(void) {
  static uint64_t x = 88172645463325252ULL;
  x = x ^ (x << 13);
  x = x ^ (x >> 7);
  return x = x ^ (x << 17);
}
// graph template
template <typename T> struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};

template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;

// constant
#define INF 1'010'000'000'000'000'017LL
#define mod 1000003LL
typedef modint<mod> mint;
#define eps 0.000001
// library

// main
int main() {
  int n, m;
  cin >> n >> m;
  WeightedGraph<ll> G(n);
  rep(i, m) {
    ll u, v, s;
    cin >> u >> v >> s;
    G[u - 1].pb(edge<ll>(v - 1, s));
    G[v - 1].pb(edge<ll>(u - 1, s));
  }
  vl color(n, -1), value(n, -1);
  color[0] = 0;
  value[0] = 0;
  deque<ll> que;
  que.pb(0);
  ll diff, conf_c;
  bool flag = true;
  while (SZ(que)) {
    ll v = que.front();
    que.pop_front();
    ll c = color[v];
    ll val = value[v];
    for (auto e : G[v]) {
      if (color[e.to] == c) {
        diff = value[e.to] - (e.cost - val);
        flag = false;
        break;
      }
      if (color[e.to] == -1) {
        value[e.to] = e.cost - val;
        color[e.to] = 1 - c;
        que.pb(e.to);
      }
    }
  }
  rep(i, n) {
    for (auto e : G[i]) {
      if (color[i] == color[e.to])
        flag = false;
      if (color[i] != 0 && color[i] != 1)
        flag = false;
    }
  }
  if (not flag) {
    diff = abs(diff);
    if (diff % 2 != 0 || diff == 0) {
      cout << 0 << endl;
      return 0;
    }
    rep(i, n) value[i] = -1;
    value[0] = abs(diff) / 2;
    deque<ll> que;
    que.pb(0);
    while (SZ(que)) {
      ll v = que.front();
      que.pop_front();
      ll val = value[v];
      for (auto e : G[v]) {
        if (value[e.to] != -1) {
          if (value[e.to] + val == e.cost)
            continue;
          cout << 0 << endl;
          return 0;
        }
        value[e.to] = e.cost - val;
        que.pb(e.to);
        if (value[e.to] <= 0) {
          cout << 0 << endl;
          return 0;
        }
      }
    }
    flag = true;
    rep(i, n) {
      if (value[i] <= 0)
        flag = false;
      for (auto e : G[i]) {
        if (value[i] + value[e.to] != e.cost)
          flag = false;
      }
    }
    if (flag)
      cout << 1 << endl;
    else
      cout << 0 << endl;
    return 0;
  }
  ll a = INF, b = INF;
  rep(i, n) {
    int c = color[i];
    if (c == 0)
      chmin(a, value[i]);
    else
      chmin(b, value[i]);
  }
  if (b > 0)
    throw "abv";
  cout << max(0, int(a + b - 1)) << endl;
  return 0;
}

Fixed submission:
// header
#ifdef LOCAL
#include "cxx-prettyprint-master/prettyprint.hpp"
#define print(x) cout << x << endl
#else
#define print(...) 42
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// types
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
template <typename T> using mat = vector<vector<T>>;
template <std::uint_fast64_t Modulus> class modint {
  using u64 = std::uint_fast64_t;

public:
  u64 a;

  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    u64 exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};
// abbreviations
#define all(x) (x).begin(), (x).end()
#define reverse(x) (x).rbegin(), (x).rend()
#define rep2(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define SZ(x) ((ll)(x).size())
#define pb(x) push_back(x)
#define ff first
#define ss second
#define mp make_pair
// functions
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> T mypow(T x, ll n) {
  T ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x);
    (x *= x);
    n >>= 1;
  }
  return ret;
}
uint64_t my_rand(void) {
  static uint64_t x = 88172645463325252ULL;
  x = x ^ (x << 13);
  x = x ^ (x >> 7);
  return x = x ^ (x << 17);
}
// graph template
template <typename T> struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};

template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;

// constant
#define INF 1'010'000'000'000'000'017LL
#define mod 1000003LL
typedef modint<mod> mint;
#define eps 0.000001
// library

// main
int main() {
  int n, m;
  cin >> n >> m;
  WeightedGraph<ll> G(n);
  rep(i, m) {
    ll u, v, s;
    cin >> u >> v >> s;
    G[u - 1].pb(edge<ll>(v - 1, s));
    G[v - 1].pb(edge<ll>(u - 1, s));
  }
  vl color(n, -1), value(n, -1);
  color[0] = 0;
  value[0] = 0;
  deque<ll> que;
  que.pb(0);
  ll diff, conf_c;
  bool flag = true;
  while (SZ(que)) {
    ll v = que.front();
    que.pop_front();
    ll c = color[v];
    ll val = value[v];
    for (auto e : G[v]) {
      if (color[e.to] == c) {
        diff = value[e.to] - (e.cost - val);
        flag = false;
        break;
      }
      if (color[e.to] == -1) {
        value[e.to] = e.cost - val;
        color[e.to] = 1 - c;
        que.pb(e.to);
      }
    }
  }
  rep(i, n) {
    for (auto e : G[i]) {
      if (color[i] == color[e.to])
        flag = false;
      if (color[i] != 0 && color[i] != 1)
        flag = false;
    }
  }
  if (not flag) {
    diff = abs(diff);
    if (diff % 2 != 0 || diff == 0) {
      cout << 0 << endl;
      return 0;
    }
    rep(i, n) value[i] = -1;
    value[0] = abs(diff) / 2;
    deque<ll> que;
    que.pb(0);
    while (SZ(que)) {
      ll v = que.front();
      que.pop_front();
      ll val = value[v];
      for (auto e : G[v]) {
        if (value[e.to] != -1) {
          if (value[e.to] + val == e.cost)
            continue;
          cout << 0 << endl;
          return 0;
        }
        value[e.to] = e.cost - val;
        que.pb(e.to);
        if (value[e.to] <= 0) {
          cout << 0 << endl;
          return 0;
        }
      }
    }
    flag = true;
    rep(i, n) {
      if (value[i] <= 0)
        flag = false;
      for (auto e : G[i]) {
        if (value[i] + value[e.to] != e.cost)
          flag = false;
      }
    }
    if (flag)
      cout << 1 << endl;
    else
      cout << 0 << endl;
    return 0;
  }
  ll a = INF, b = INF;
  rep(i, n) {
    int c = color[i];
    if (c == 0)
      chmin(a, value[i]);
    else
      chmin(b, value[i]);
  }
  rep(i, n) value[i] = -1;
  value[0] = abs(a) + 1;
  que.pb(0);
  if (a <= 0) {
    while (SZ(que)) {
      ll v = que.front();
      que.pop_front();
      ll val = value[v];
      for (auto e : G[v]) {
        if (value[e.to] != -1) {
          if (value[e.to] + val == e.cost)
            continue;
          cout << 0 << endl;
          return 0;
        }
        value[e.to] = e.cost - val;
        que.pb(e.to);
        if (value[e.to] <= 0) {
          cout << 0 << endl;
          return 0;
        }
      }
    }
  }
  cout << max(0, int(a + b - 1)) << endl;
  return 0;
}