Problem p03306 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long INF = 10000000000000000;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, int>>> E(n);
  for (int i = 0; i < m; i++) {
    int u, v, s;
    cin >> u >> v >> s;
    u--;
    v--;
    E[u].push_back(make_pair(s, v));
    E[v].push_back(make_pair(s, u));
  }
  vector<pair<int, long long>> f(n);
  f[0] = make_pair(1, 0);
  vector<bool> used(n, false);
  used[0] = true;
  queue<int> Q;
  Q.push(0);
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    for (auto P : E[v]) {
      int s = P.first;
      int w = P.second;
      if (!used[w]) {
        used[w] = true;
        f[w] = make_pair(-f[v].first, s - f[v].second);
        Q.push(w);
      }
    }
  }
  long long x = -1;
  bool ok = true;
  for (int v = 0; v < n; v++) {
    for (auto P : E[v]) {
      int s = P.first;
      int w = P.second;
      int sum1 = f[v].first + f[w].first;
      long long sum2 = f[v].second + f[w].second;
      if (sum1 == 0) {
        if (sum2 != s) {
          ok = false;
        }
      }
      if (sum1 == 2) {
        if ((s - sum2) % 2 != 0) {
          ok = false;
        } else {
          long long x_tmp = (s - sum2) / 2;
          if (x_tmp <= 0) {
            ok = false;
          } else {
            if (x == -1 || x == x_tmp) {
              x = x_tmp;
            } else {
              ok = false;
            }
          }
        }
      }
      if (sum1 == -2) {
        if ((sum2 - s) % 2 != 0) {
          ok = false;
        } else {
          long long x_tmp = (sum2 - s) / 2;
          if (x_tmp <= 0) {
            ok = false;
          } else {
            if (x == -1 || x == x_tmp) {
              x = x_tmp;
            } else {
              ok = false;
            }
          }
        }
      }
    }
  }
  if (!ok) {
    cout << 0 << endl;
  } else if (x != -1) {
    assert(false);
    for (int i = 0; i < n; i++) {
      if (f[i].first * x + f[i].second <= 0) {
        ok = false;
      }
    }
    if (!ok) {
      cout << 0 << endl;
    } else {
      cout << 1 << endl;
    }
  } else {
    long long ub = INF;
    long long lb = 1;
    for (int i = 0; i < n; i++) {
      if (f[i].first == 1) {
        lb = max(lb, -f[i].second + 1);
      }
      if (f[i].first == -1) {
        ub = min(ub, f[i].second - 1);
      }
    }
    if (lb <= ub) {
      cout << ub - lb + 1 << endl;
    } else {
      cout << 0 << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long INF = 10000000000000000;
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, int>>> E(n);
  for (int i = 0; i < m; i++) {
    int u, v, s;
    cin >> u >> v >> s;
    u--;
    v--;
    E[u].push_back(make_pair(s, v));
    E[v].push_back(make_pair(s, u));
  }
  vector<pair<int, long long>> f(n);
  f[0] = make_pair(1, 0);
  vector<bool> used(n, false);
  used[0] = true;
  queue<int> Q;
  Q.push(0);
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    for (auto P : E[v]) {
      int s = P.first;
      int w = P.second;
      if (!used[w]) {
        used[w] = true;
        f[w] = make_pair(-f[v].first, s - f[v].second);
        Q.push(w);
      }
    }
  }
  long long x = -1;
  bool ok = true;
  for (int v = 0; v < n; v++) {
    for (auto P : E[v]) {
      int s = P.first;
      int w = P.second;
      int sum1 = f[v].first + f[w].first;
      long long sum2 = f[v].second + f[w].second;
      if (sum1 == 0) {
        if (sum2 != s) {
          ok = false;
        }
      }
      if (sum1 == 2) {
        if ((s - sum2) % 2 != 0) {
          ok = false;
        } else {
          long long x_tmp = (s - sum2) / 2;
          if (x_tmp <= 0) {
            ok = false;
          } else {
            if (x == -1 || x == x_tmp) {
              x = x_tmp;
            } else {
              ok = false;
            }
          }
        }
      }
      if (sum1 == -2) {
        if ((sum2 - s) % 2 != 0) {
          ok = false;
        } else {
          long long x_tmp = (sum2 - s) / 2;
          if (x_tmp <= 0) {
            ok = false;
          } else {
            if (x == -1 || x == x_tmp) {
              x = x_tmp;
            } else {
              ok = false;
            }
          }
        }
      }
    }
  }
  if (!ok) {
    cout << 0 << endl;
  } else if (x != -1) {
    for (int i = 0; i < n; i++) {
      if (f[i].first * x + f[i].second <= 0) {
        ok = false;
      }
    }
    if (!ok) {
      cout << 0 << endl;
    } else {
      cout << 1 << endl;
    }
  } else {
    long long ub = INF;
    long long lb = 1;
    for (int i = 0; i < n; i++) {
      if (f[i].first == 1) {
        lb = max(lb, -f[i].second + 1);
      }
      if (f[i].first == -1) {
        ub = min(ub, f[i].second - 1);
      }
    }
    if (lb <= ub) {
      cout << ub - lb + 1 << endl;
    } else {
      cout << 0 << endl;
    }
  }
}