Language: C++

#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>

#define int long long
using namespace std;
#define rep(i, n) for(int i=0;i<(n);++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound
/*
int N, M;
vector<pii> G[100100];
vector<pair<pii, int>> extraedges;
int S[100100];
bool tree[100100];
int Rank[100100];

void maketree(int par, int v, int s, int rank) {
    tree[v] = true;
    S[v] = s;
    Rank[v] = rank;
    rep(i, G[v].size()) {
        int to = G[v][i].first;
        int s2 = G[v][i].second;
        if (to == par)continue;
        if (tree[to])extraedges.push_back(make_pair(pii(v, to), s2));
        else maketree(v, to, s2, rank + 1);
    }
}

signed main() {
    cin >> N >> M;
    rep(i, M) {
        int u, v, s;
        cin >> u >> v >> s;
        --u;
        --v;
        G[u].push_back(pii(v, s));
        G[v].push_back(pii(u, s));
    }
    maketree(-1, 0, 0, 0);



    return 0;
}
 */

int N, M;
int val1[100100];
int val2[100100];
int *val;
vector<pii> G[100100];

bool make(int par, int v) {
    rep(i, G[v].size()) {
        int to = G[v][i].first;
        int s2 = G[v][i].second;
        if (to == par)continue;
        else {
            if (val[to] == -1)val[to] = s2 - val[v];
            else if (val[to] + val[v] != s2)return false;
            else continue;
            bool success = make(v, to);
            if (!success)return false;
        }
    }
    return true;
}

signed main() {
    cin >> N >> M;
    rep(i, M) {
        int u, v, s;
        cin >> u >> v >> s;
        --u;
        --v;
        G[u].push_back(pii(v, s));
        G[v].push_back(pii(u, s));
    }
    memset(val1, -1, sizeof(val1));
    val1[0] = 1;
    val = val1;
    bool bool1 = make(-1, 0);

    memset(val2, -1, sizeof(val2));
    val2[0] = 2;
    val = val2;
    bool bool2 = make(-1, 0);

    if (bool1 && bool2) {
        vector<int> inc, dec;
        rep(i, N) {
            if (val1[i] < val2[i])inc.push_back(i);
            else dec.push_back(i);
        }
        int mininc = INF;
        int mindec = INF;
        rep(j, inc.size()) {
            mininc = min(mininc, val1[inc[j]]);
        }
        rep(j, dec.size()) {
            mindec = min(mindec, val1[dec[j]]);
        }
        if (mininc <= 0) {
            mindec -= (1 - mininc);
            mininc += (1 - mininc);
        }
        if (mindec > 0) {
            //mininc=1
            cout << mindec << endl;
            return 0;
        } else if (mindec <= 0) {
            //mininc=1
            cout << 0 << endl;
            return 0;
        }
    } else if (bool1 || bool2) {
        if (bool1) {
            rep(i, N) {
                if (val1[i] <= 0) {
                    cout << 0 << endl;
                    return 0;
                }
            }
        } else {
            rep(i, N) {
                if (val2[i] <= 0) {
                    cout << 0 << endl;
                    return 0;
                }
            }
        }
        cout << 1 << endl;
        return 0;
    } else {
        assert(0);
    }
}
//E


Compiler output:
a.cc: In function 'int main()':
a.cc:142:9: error: 'assert' was not declared in this scope
  142 |         assert(0);
      |         ^~~~~~
a.cc:7:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
    6 | #include<cstring>
  +++ |+#include <cassert>
    7 |