Problem p03306 (C++)

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

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif

using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;

class Solver {
public:
  int n, m;
  LL diff;
  vector<vector<pair<int, LL>>> g;
  vector<LL> val;
  vector<LL> visit;
  vector<LL> mini;
  set<pair<int, int>> used;
  int oddloop;
  Solver(LL n) : n(n), g(n), val(n), visit(n, -1), mini(2, LINF), oddloop(0){};
  void no() {
    cout << 0 << endl;
    exit(0);
  }
  void dfs(int v, int p) {
    mini[visit[v]] = min(mini[visit[v]], val[v]);
    for (int i = 0; i < g[v].size(); ++i) {
      auto x = g[v][i];
      if (used.count(make_pair(v, x.first)))
        continue;
      if (x.first != p) {
        if (visit[x.first] >= 0) {
          if (visit[x.first] == visit[v]) {
            used.insert({x.first, v});
            oddloop++;
            if ((val[x.first] + val[v]) % 2 != x.second % 2) {
              assert(false);
              no();
            } else if (val[x.first] + val[v] != x.second) {
              diff = (-val[x.first] - val[v] + x.second) / 2;
              auto tmp = val[v] + diff;
              fill(visit.begin(), visit.end(), -1);
              val[v] = tmp;
              visit[v] = 0;
              mini[0] = mini[1] = LINF;
              used = set<pair<int, int>>();
              dfs2(v, -1);
              if (min(mini[0], mini[1]) <= 0)
                cout << 0 << endl;
              else
                cout << 1 << endl;
              exit(0);
            }
          } else {
            if (val[x.first] + val[v] != x.second) {
              no();
            }
          }
        } else {
          visit[x.first] = !visit[v];
          val[x.first] = x.second - val[v];
          dfs(x.first, v);
        }
      }
    }
  }
  void dfs2(int v, int p) {
    mini[visit[v]] = min(mini[visit[v]], val[v]);
    for (int i = 0; i < g[v].size(); ++i) {
      auto x = g[v][i];
      if (used.count(make_pair(v, x.first)))
        continue;
      if (x.first != p) {
        if (visit[x.first] >= 0) {
          if (val[x.first] + val[v] != x.second) {
            no();
          }
        } else {
          visit[x.first] = !visit[v];
          val[x.first] = x.second - val[v];
          dfs2(x.first, v);
        }
      }
    }
  }
  void solve() {
    cin >> m;
    for (int i = 0; i < m; ++i) {
      int u, v, s;
      cin >> u >> v >> s;
      --u;
      --v;
      g[u].push_back({v, s});
      g[v].push_back({u, s});
    }
    val[0] = 1;
    visit[0] = 0;
    dfs(0, -1);
    if (oddloop >= 1) {
      if (min(mini[0], mini[1]) <= 0)
        cout << 0 << endl;
      else
        cout << 1 << endl;
    } else {
      cout << max<LL>(0, mini[0] + mini[1] - 1) << endl;
    }
  }
};
} // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  long long n = 0;
  std::cin >> n;

  Problem::Solver sol(n);
  sol.solve();
  return 0;
}


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

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif

using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;

class Solver {
public:
  int n, m;
  LL diff;
  vector<vector<pair<int, LL>>> g;
  vector<LL> val;
  vector<LL> visit;
  vector<LL> mini;
  set<pair<int, int>> used;
  int oddloop;
  Solver(LL n) : n(n), g(n), val(n), visit(n, -1), mini(2, LINF), oddloop(0){};
  void no() {
    cout << 0 << endl;
    exit(0);
  }
  void dfs(int v, int p) {
    mini[visit[v]] = min(mini[visit[v]], val[v]);
    for (int i = 0; i < g[v].size(); ++i) {
      auto x = g[v][i];
      if (used.count(make_pair(v, x.first)))
        continue;
      if (x.first != p) {
        if (visit[x.first] >= 0) {
          if (visit[x.first] == visit[v]) {
            used.insert({x.first, v});
            oddloop++;
            if ((val[x.first] + val[v]) % 2 != x.second % 2) {
              cout << 1 << endl;
              exit(0);
            } else if (val[x.first] + val[v] != x.second) {
              diff = (-val[x.first] - val[v] + x.second) / 2;
              auto tmp = val[v] + diff;
              fill(visit.begin(), visit.end(), -1);
              val[v] = tmp;
              visit[v] = 0;
              mini[0] = mini[1] = LINF;
              used = set<pair<int, int>>();
              dfs2(v, -1);
              if (min(mini[0], mini[1]) <= 0)
                cout << 0 << endl;
              else
                cout << 1 << endl;
              exit(0);
            }
          } else {
            if (val[x.first] + val[v] != x.second) {
              no();
            }
          }
        } else {
          visit[x.first] = !visit[v];
          val[x.first] = x.second - val[v];
          dfs(x.first, v);
        }
      }
    }
  }
  void dfs2(int v, int p) {
    mini[visit[v]] = min(mini[visit[v]], val[v]);
    for (int i = 0; i < g[v].size(); ++i) {
      auto x = g[v][i];
      if (used.count(make_pair(v, x.first)))
        continue;
      if (x.first != p) {
        if (visit[x.first] >= 0) {
          if (val[x.first] + val[v] != x.second) {
            no();
          }
        } else {
          visit[x.first] = !visit[v];
          val[x.first] = x.second - val[v];
          dfs2(x.first, v);
        }
      }
    }
  }
  void solve() {
    cin >> m;
    for (int i = 0; i < m; ++i) {
      int u, v, s;
      cin >> u >> v >> s;
      --u;
      --v;
      g[u].push_back({v, s});
      g[v].push_back({u, s});
    }
    val[0] = 1;
    visit[0] = 0;
    dfs(0, -1);
    if (oddloop >= 1) {
      if (min(mini[0], mini[1]) <= 0)
        cout << 0 << endl;
      else
        cout << 1 << endl;
    } else {
      cout << max<LL>(0, mini[0] + mini[1] - 1) << endl;
    }
  }
};
} // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  long long n = 0;
  std::cin >> n;

  Problem::Solver sol(n);
  sol.solve();
  return 0;
}