Problem p03306 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define P pair<int, int>
#define int long long
#define mod (int)(1e9 + 7)
#define inf (int)(3e18)
#define all(v) v.begin(), v.end()
#define prique(T) priority_queue<T, vector<T>, greater<T>>
using namespace std;

template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); }
template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); }

int mod_pow(int x, int y, int m = mod) {
  int res = 1;
  while (y) {
    if (y % 2) {
      res *= x;
      res %= m;
    }
    x = x * x % mod;
    y /= 2;
  }
  return res;
}

int n, m;
struct info {
  int to, sum;
};
vector<info> G[100005];
bool used[100005];
int num[100005];
int dis[100005];
void dfs2(int x) {
  used[x] = true;
  for (info e : G[x]) {
    if (!used[e.to]) {
      num[e.to] = e.sum - num[x];
      dfs2(e.to);
    }
  }
}
void dfs(int x, int pre) {
  used[x] = true;
  for (info e : G[x]) {
    if (e.to == pre)
      continue;
    if (used[e.to]) {
      if ((dis[x] + 1 - dis[e.to]) % 2) {
        num[e.to] += (e.sum - num[x] - num[e.to]) / 2;
        memset(used, false, sizeof(used));
        dfs2(e.to);
        rep(i, n) {
          if (num[i] <= 0) {
            puts("0");
            exit(0);
          }
          for (info p : G[i]) {
            if (num[i] + num[p.to] != p.sum) {
              puts("0");
              exit(0);
            }
          }
        }
        puts("1");
        exit(0);
      }
    }
    num[e.to] = e.sum - num[x];
    dis[e.to] = dis[x] + 1;
    dfs(e.to, x);
  }
}
signed main() {
  cin >> n >> m;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back({b, c});
    G[b].push_back({a, c});
  }
  dfs(0, -1);
  int ma = inf;
  int least = 0;
  rep(i, n) {
    if (dis[i] % 2) {
      chmin(ma, num[i] - 1);
    } else {
      chmax(least, 1 - num[i]);
    }
  }
  cout << max(0ll, ma - least + 1) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define P pair<int, int>
#define int long long
#define mod (int)(1e9 + 7)
#define inf (int)(3e18)
#define all(v) v.begin(), v.end()
#define prique(T) priority_queue<T, vector<T>, greater<T>>
using namespace std;

template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); }
template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); }

int mod_pow(int x, int y, int m = mod) {
  int res = 1;
  while (y) {
    if (y % 2) {
      res *= x;
      res %= m;
    }
    x = x * x % mod;
    y /= 2;
  }
  return res;
}

int n, m;
struct info {
  int to, sum;
};
vector<info> G[100005];
bool used[100005];
int num[100005];
int dis[100005];
void dfs2(int x) {
  used[x] = true;
  for (info e : G[x]) {
    if (!used[e.to]) {
      num[e.to] = e.sum - num[x];
      dfs2(e.to);
    }
  }
}
void dfs(int x, int pre) {
  used[x] = true;
  for (info e : G[x]) {
    if (e.to == pre)
      continue;
    if (used[e.to]) {
      if ((dis[x] + 1 - dis[e.to]) % 2) {
        num[e.to] += (e.sum - num[x] - num[e.to]) / 2;
        memset(used, false, sizeof(used));
        dfs2(e.to);
        rep(i, n) {
          if (num[i] <= 0) {
            puts("0");
            exit(0);
          }
          for (info p : G[i]) {
            if (num[i] + num[p.to] != p.sum) {
              puts("0");
              exit(0);
            }
          }
        }
        puts("1");
        exit(0);
      } else {
        if (num[e.to] != e.sum - num[x]) {
          puts("0");
          exit(0);
        } else {
          continue;
        }
      }
    }
    num[e.to] = e.sum - num[x];
    dis[e.to] = dis[x] + 1;
    dfs(e.to, x);
  }
}
signed main() {
  cin >> n >> m;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back({b, c});
    G[b].push_back({a, c});
  }
  dfs(0, -1);
  int ma = inf;
  int least = 0;
  rep(i, n) {
    if (dis[i] % 2) {
      chmin(ma, num[i] - 1);
    } else {
      chmax(least, 1 - num[i]);
    }
  }
  cout << max(0ll, ma - least + 1) << endl;
}