Language: C++

#include <bits/stdc++.h>
using namespace std;

#define NDEBUG
#ifdef DEBUG
#include "../cout11.h"
#undef NDEBUG
#endif
#include <cassert>

typedef long long ll;
typedef long double Double;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<ll,ll> llll;
typedef pair<double,double> dd;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<long double> vD;

#define sz(a)  int((a).size())
#define pb  push_back
#define FOR(var,from,to) for(int var=(from);var<=(to);++var)
#define rep(var,n)  for(int var=0;var<(n);++var)
#define rep1(var,n)  for(int var=1;var<=(n);++var)
#define repC2(vari,varj,n)  for(int vari=0;vari<(n)-1;++vari)for(int varj=vari+1;varj<(n);++varj)
#define ALL(c)  (c).begin(),(c).end()
#define RALL(c)  (c).rbegin(),(c).rend()
#define tr(i,c)  for(auto i=(c).begin(); i!=(c).end(); ++i)
#define found(s,e)  ((s).find(e)!=(s).end())
#define mset(arr,val)  memset(arr,val,sizeof(arr))
#define mid(x,y) ((x)+((y)-(x))/2)
#define IN(x,a,b) ((a)<=(x)&&(x)<=(b))
#define cons make_pair

typedef pair<int,ll> iL;


bool check_consistency(int n,int m,vi& u,vi& v,vi& s,
                      vector<vector<ii>>& nxt, ll x0) {
#ifdef DEBUG
    fprintf(stderr, "x0=%lld\n", x0);
#endif
    if (x0 <= 0) return false;

    vector<ll> x(n, -1);
    x[0] = x0;
    queue<pair<int,ll>> q;
    q.push(cons(0, x0));
    while (!q.empty()) {
        auto t=q.front(); q.pop();
        int u=t.first; ll xu=t.second;
        // xu >= 0
        if (x[u] == -1) {
            x[u] = xu;
        } else {
            if (x[u] == xu) continue;
            else return false;
        }
        for (ii v_s: nxt[u]) {
            int v=v_s.first, s=v_s.second;
            if (s-xu <= 0) return false;
            q.push(cons(v,s-xu));
        }
    }
    rep(i,n) if (x[i] <= 0) return false;
    return true;
}

ll solve(int n,int m,vi& u,vi& v,vi& s) {
    vector<vector<ii>> nxt(n);
    rep(i,m){
        int u_i=u[i], v_i=v[i], s_i=s[i];
        nxt[u_i].pb(ii(v_i, s_i));
        nxt[v_i].pb(ii(u_i, s_i));
    }

    vector<bool> visited(n, false);
    vector<iL> ab(n);

    queue<pair<int,iL>> q;
    q.push(cons(0,iL(1,0LL)));
    while (!q.empty()) {
        auto t=q.front(); q.pop();

        int ui = t.first; iL c = t.second;

        if (visited[ui]) {
            if (ab[ui].first == c.first) {
                // + +, - -
                if (ab[ui].second == c.second) {
                    // consistent
                    continue;
                } else {
#ifdef DEBUG
                    cerr << "*inconsistent*" << endl;
#endif
                    return 0;
                }
            } else {
                //ax+b = -cx+d
                int denom = ab[ui].first - c.first; // +-2
                ll numer = c.second - ab[ui].second;
                if (numer % denom == 0) {
                    ll x0 = numer / denom;
                    if (check_consistency(n,m, u,v,s, nxt, x0)) {
                        return 1;
                    } else {
                        return 0;
                    }
                } else {
#ifdef DEBUG
                    cerr << "*impossible*" << endl;
#endif
                    return 0;
                }
            }
            continue;
        }
        visited[ui] = true; ab[ui] = c;

        int a = c.first; ll b = c.second;
        for (ii v_s : nxt[ui]) {
            int uj = v_s.first;
            ll s = (ll)v_s.second;
            // u: ax+b
            // u+v = s; v = s-u = s-(ax+b) = -ax + s-b
            q.push(cons(uj,iL(-a,s-b)));
        }
    }
    if (!visited[ui]) return 0;

#ifdef DEBUG
    cerr << ab << endl;
#endif
    ll xmin = 1, xmax = LLONG_MAX;
    rep(i,n){
        // fprintf(stderr, "[%lld .. %lld]\n", xmin, xmax);
        int a = ab[i].first; ll b = ab[i].second;
        // fprintf(stderr, "%dx + %lld >= 0\n", a, b);
        if (a == 1) {
            // x + b >= 1
            // x >= 1-b
            // fprintf(stderr, "x >= %lld\n", 1-b);
            xmin = max(xmin, 1-b);
        } else if (a == -1) {
            // -x + b >= 1
            // x <= b-1
            // fprintf(stderr, "x <= %lld\n", b-1);
            xmax = min(xmax, b-1);
        }
    }
    // fprintf(stderr, "[%lld .. %lld]\n", xmin, xmax);
    if (xmax < xmin) return 0;
    return xmax - xmin + 1;
}

int main() {
    int n,m;cin>>n>>m;
    vi u(m),v(m),s(m);
    rep(i,m){
        cin>>u[i]>>v[i]>>s[i];
        --u[i]; --v[i];
    }
    cout << solve(n,m, u,v,s) << endl;
    return 0;
}


Compiler output:
a.cc: In function 'll solve(int, int, vi&, vi&, vi&)':
a.cc:137:18: error: 'ui' was not declared in this scope; did you mean 'u'?
  137 |     if (!visited[ui]) return 0;
      |                  ^~
      |                  u