Language: C++

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cmath>
#include <algorithm>

using namespace std;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

const int MAX_N = 100000;
const int MAX_M = 100000;
const ll INF = 1e18;

struct edge {
  ll to, sum;
};

int n, m;
ll t = -1;
ll ans = INF;
vector<edge> G[MAX_N];
vi candis[MAX_N][2];
vector<bool> visited(MAX_N, false);

bool dfs(int now, int pre, ll val, int sign) {
  if (visited[now]) return true;
  visited[now] = true;

  int sign_d = (sign == -1) ? 0 : 1;
  for (int i = 0; i < G[now].size(); i++) {
    int next = G[now][i].to;
    if (next == pre) continue;
    if (candis[next][sign_d].size() == 1) {
      ll a = val + sign*G[now][i].sum;
      ll b = candis[next][sign_d][0];
      // cout << a << ", " << b << endl;
      if (((a + b)&1) == 0) {
        t = (a + b) / 2;
        if (t <= 0) {
          ans = 0;
        }
        // cout << "t: " << t << endl;
      }
      return false;
    }
    if (candis[next][1-sign_d].size() == 1) {
      if (candis[next][1-sign_d][0] != val + sign*G[now][i].sum) {
        // cout << "now, next" << endl;
        // cout << now << ", " << next << endl;
        // cout << candis[next][1-sign_d][0] << ", " << val + sign*G[now][i].sum << endl;
        return false;
      }
    } else {
      // cout << next << " << " << val + sign*G[now][i].sum << endl;
      candis[next][1-sign_d].pb(val + sign*G[now][i].sum);
    }
    if (!dfs(next, now, val + sign*G[now][i].sum, -sign)) {
      return false;
    }
  }
  return true;
}

vi check_values(MAX_N, -1);
vector<bool> check_visited(MAX_N, false);

bool check(int now, int pre, ll value) {
  check_visited[now] = true;

  if (check_values[now] != -1 && check_values[now] != value) {
    return false;
  }
  check_values[now] = value;

  for (int i = 0; i < G[now].size(); i++) {
    int next = G[now][i].to;
    if (next == pre) continue;
    if (check_visited[next]) {
      if (check_values[now] + check_values[next] != G[now][i].sum) {
        return false;
      }
      continue;
    }
    if (!check(next, now, G[now][i].sum - value)) {
      return false;
    }
  }

  return true;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v; cin >> u >> v; u--; v--;
    ll s; cin >> s;
    edge tmp1 = {v, s};
    edge tmp2 = {u, s};
    G[u].pb(tmp1);
    G[v].pb(tmp2);
  }
  ll t_min = 1, t_max = INF;
  candis[0][1].pb(0);
  if (!dfs(0, -1, 0, 1)) {
    // cout << "!" << endl;
    ans = 0;
    if (t != -1) {
      if (t <= 0) break;
      if (check(0, -1, t)) {
        // cout << "OK" << endl;
        ans = 1;
        for (int i = 0; i < n; i++) {
          // cout << i+1 << ": " << check_values[i] << endl;
        }
      }
    }
  } else {
    for (int i = 1; i < n; i++) {
      // cout << i+1 << endl;
      if (candis[i][0].size() == 1) {
        ll a = candis[i][0][0];
        // cout << "a: " << a << endl;
        t_max = min(t_max, a - 1);
      } else {
        ll b = -candis[i][1][0];
        // cout << "b: " << b << endl;
        t_min = max(t_min, -b + 1);
      }
      // cout << "t_min: " << t_min << endl;
      // cout << "t_max: " << t_max << endl;
    }
  }
  ans = min(ans, max(0LL, t_max - t_min + 1));
  cout << ans << endl;
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:120:19: error: break statement not within loop or switch
  120 |       if (t <= 0) break;
      |                   ^~~~~