Problem p03306 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1000000007;

struct edge {
  int to;
  ll cost;
  edge(int to, ll cost) : to(to), cost(cost) {}
};

int n, m;
vector<vector<edge>> G;
vector<bool> used;
map<int, P> memo;

P intersection(P a, P b) {
  return make_pair(max(a.first, b.first), min(a.second, b.second));
}

P dfs(int v, P a_s) {
  used[v] = true;
  memo[v] = a_s;
  P ret(1, 1000000000);
  for (edge &e : G[v]) {
    P b = make_pair(-a_s.first, e.cost - a_s.second);

    if (!used[e.to]) {
      ret = intersection(ret, dfs(e.to, b));
    } else {
      P c = memo[e.to];
      if (c.first != b.first) {
        if (abs(b.second - c.second) % abs(c.first - b.first) == 0) {
          ll a = (b.second - c.second) / (c.first - b.first);
          ret = intersection(ret, P(a, a));
        } else {
          ret = P(0, -1);
        }
      } else {
        assert(c.second - b.second == 0);
      }
    }

    if (b.first == 0) {
      ll a = b.second - e.cost;
      ret = intersection(ret, P(a, a));
    } else if (b.first > 0) {
      ret.first = max(ret.first, (1 - b.second + b.first - 1) / b.first);
    } else {
      ret.second = min(ret.second, (b.second - 1) / abs(b.first));
    }
  }
  return ret;
}

int main() {
  cin >> n >> m;

  G.assign(n, {});
  for (int i = 0; i < m; i++) {
    int u, v;
    ll s;
    cin >> u >> v >> s;
    u--;
    v--;
    G[u].emplace_back(v, s);
    G[v].emplace_back(u, s);
  }

  used.assign(n, false);
  auto a_range = dfs(0, make_pair(1, 0));
  // cout << a_range.first << " " << a_range.second << endl;
  cout << max(0LL, a_range.second - a_range.first + 1) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1000000007;

struct edge {
  int to;
  ll cost;
  edge(int to, ll cost) : to(to), cost(cost) {}
};

int n, m;
vector<vector<edge>> G;
vector<bool> used;
map<int, P> memo;

P intersection(P a, P b) {
  return make_pair(max(a.first, b.first), min(a.second, b.second));
}

P dfs(int v, P a_s) {
  used[v] = true;
  memo[v] = a_s;
  P ret(1, 1000000000);
  for (edge &e : G[v]) {
    P b = make_pair(-a_s.first, e.cost - a_s.second);

    if (!used[e.to]) {
      ret = intersection(ret, dfs(e.to, b));
    } else {
      P c = memo[e.to];
      if (c.first != b.first) {
        if (abs(b.second - c.second) % abs(c.first - b.first) == 0) {
          ll a = (b.second - c.second) / (c.first - b.first);
          ret = intersection(ret, P(a, a));
        } else {
          ret = P(0, -1);
        }
      } else {
        if (c.second - b.second != 0) {
          ret = P(0, -1);
        }
      }
    }

    if (b.first == 0) {
      ll a = b.second - e.cost;
      ret = intersection(ret, P(a, a));
    } else if (b.first > 0) {
      ret.first = max(ret.first, (1 - b.second + b.first - 1) / b.first);
    } else {
      ret.second = min(ret.second, (b.second - 1) / abs(b.first));
    }
  }
  return ret;
}

int main() {
  cin >> n >> m;

  G.assign(n, {});
  for (int i = 0; i < m; i++) {
    int u, v;
    ll s;
    cin >> u >> v >> s;
    u--;
    v--;
    G[u].emplace_back(v, s);
    G[v].emplace_back(u, s);
  }

  used.assign(n, false);
  auto a_range = dfs(0, make_pair(1, 0));
  // cout << a_range.first << " " << a_range.second << endl;
  cout << max(0LL, a_range.second - a_range.first + 1) << endl;

  return 0;
}