Language: C++

#include<iostream>
#include<cstdio>
#include<cstring>
#include<limits>
#include<map>
#include<cassert>
#include<vector>
using namespace std;
typedef long long ll;

ll N,M;
struct edge {
  ll to,c;
  edge(){}
  edge(ll to,ll c):to(to),c(c){}
};

vector<edge> G[100001];
bool F[100001][2];
ll S[100001][2];

bool dfs(ll v, ll f){
  F[v][f] = true;
  for(edge e : G[v]) {
    if( F[e.to][1-f] ) {
      if (S[e.to][1-f] != e.c - S[v][f]) return false;
    } else {
      S[e.to][1-f] = e.c - S[v][f];
      if( !dfs(e.to, 1-f) ) return false;
    }
  }
  return true;
}


bool dfs2(ll v){

}

int main() {
  cin >> N >> M;
  for(ll i=0;i<M;i++){
    ll a,b,c;
     cin >> a >> b >> c; --a; --b;
    G[a].emplace_back( b,c);
    G[b].emplace_back( a,c);
  }

  if( !dfs(0,0) ) {
    cout << 0 << endl;
    return 0;
  }

  ll mini1 = LLONG_MAX, mini2 = LLONG_MAX;
  ll cnt = 0;
  for(ll i=0;i<N;i++) {
    if( F[i][0] ) {
      mini1 = min( mini1, S[i][0] );
      cnt++;
    }
    if( F[i][1] ) {
      mini2 = min( mini2, S[i][1] );
      cnt++;
    }
  }


  if( cnt == 2 * N ) {
    bool f = [](mini1,mini2) {
      for (int i = 0; i < N; i++) {
        for (edge e : G[i]) {
          if (S[i][0] + S[e.to][1] != e.c) {
            int sa = e.c - (S[i][0] + S[e.to][1]);
            if (mini1 + sa / 2 < 1 || mini2 - sa / 2 < 1) return false;
          }
        }
      }
      return true;
    }();
    if( !f ) {
      cout << 0 << endl;
    } else {
      cout << 1 << endl;
    }
  } else {
    if( mini1 > mini2 )
      swap(mini1,mini2);
    mini1--;
    mini2 += mini1;
    cout << max(0LL,mini2) << endl;
  }
}

Compiler output:
a.cc: In function 'bool dfs2(ll)':
a.cc:38:1: warning: no return statement in function returning non-void [-Wreturn-type]
   38 | }
      | ^
a.cc: In function 'int main()':
a.cc:54:14: error: 'LLONG_MAX' was not declared in this scope
   54 |   ll mini1 = LLONG_MAX, mini2 = LLONG_MAX;
      |              ^~~~~~~~~
a.cc:8:1: note: 'LLONG_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
    7 | #include<vector>
  +++ |+#include <climits>
    8 | using namespace std;
a.cc:62:7: error: 'mini2' was not declared in this scope; did you mean 'mini1'?
   62 |       mini2 = min( mini2, S[i][1] );
      |       ^~~~~
      |       mini1
a.cc:69:17: error: 'mini1' is not a type
   69 |     bool f = [](mini1,mini2) {
      |                 ^~~~~
a.cc:69:23: error: 'mini2' has not been declared
   69 |     bool f = [](mini1,mini2) {
      |                       ^~~~~
a.cc: In lambda function:
a.cc:74:17: error: 'mini1' is not captured
   74 |             if (mini1 + sa / 2 < 1 || mini2 - sa / 2 < 1) return false;
      |                 ^~~~~
a.cc:69:15: note: the lambda has no capture-default
   69 |     bool f = [](mini1,mini2) {
      |               ^
a.cc:54:6: note: 'll mini1' declared here
   54 |   ll mini1 = LLONG_MAX, mini2 = LLONG_MAX;
      |      ^~~~~
a.cc:74:39: error: 'mini2' was not declared in this scope; did you mean 'mini1'?
   74 |             if (mini1 + sa / 2 < 1 || mini2 - sa / 2 < 1) return false;
      |                                       ^~~~~
      |                                       mini1
a.cc: In function 'int main()':
a.cc:79:6: error: no match for call to '(main()::<lambda(int, int)>) ()'
   69 |     bool f = [](mini1,mini2) {
      |              ~~~~~~~~~~~~~~~~~
   70 |       for (int i = 0; i < N; i++) {
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   71 |         for (edge e : G[i]) {
      |         ~~~~~~~~~~~~~~~~~~~~~
   72 |           if (S[i][0] + S[e.to][1] != e.c) {
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |             int sa = e.c - (S[i][0] + S[e.to][1]);
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   74 |             if (mini1 + sa / 2 < 1 || mini2 - sa / 2 < 1) return false;
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   75 |           }
      |           ~
   76 |         }
      |         ~
   77 |       }
      |       ~
   78 |       return true;
      |       ~~~~~~~~~~~~
   79 |     }();
      |     ~^~
a.cc:79:6: note: candidate: 'bool (*)(int, int)' (conversion)
a.cc:79:6: note:   candidate expects 3 arguments, 1 provided
a.cc:69:14: note: candidate: 'main()::<lambda(int, int)>'
   69 |     bool f = [](mini1,mini2) {
      |              ^
a.cc:69:14: note:   candidate expects 2 arguments, 0 provided
a.cc:86:17: error: 'mini2' was not declared in this scope; did you mean 'mini1'?
   86 |     if( mini1 > mini2 )
      |                 ^~~~~
      |                 mini1
a.cc:89:5: error: 'mini2' was not declared in this scope; did you mean 'mini1'?
   89 |     mini2 += mini1;
      |     ^~~~~
      |     mini1