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 (check(0, -1, t)) {
        // cout << "OK" << endl;
        ans = min(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:122:18: error: no matching function for call to 'min(ll&, int)'
  122 |         ans = min(ans, 1);
      |               ~~~^~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:122:18: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
  122 |         ans = min(ans, 1);
      |               ~~~^~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:7:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:122:18: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  122 |         ans = min(ans, 1);
      |               ~~~^~~~~~~~