Language: C++

#include <iostream>
#include <vector>
#include <utility>
#include <queue>
#include <stack>
using namespace std;
const int MAX = 100010;
long long n,m,dist[MAX],used[MAX],a[MAX],color[MAX];
struct edge{long long to,cost;};
pair<pair<int,int>,long long> E[MAX];
vector<edge> G[MAX];

void dfs(int s,int x,int y){
    a[s] = y; dist[s] = x;used[s] = true;
    for(int i=0;i<G[s].size();i++){
        if(!used[G[s][i].to]) dfs(G[s][i].to,x+1,G[s][i].cost-y);
    }
}

bool bip(int i, int col) {
    if (color[i] == col) return true;
    if (color[i] != 0) return false;
    bool ok = true;
    color[i] = col;
    for (int u = 0;u<G[i].size();u++)
        ok &= bip(G[i][u].to, 3 - col);
    return ok;
}

bool check(){
    for(int i=0;i<m;i++){
        if(a[E[i].first.first]+a[E[i].first.second]!=E[i].second){
            return false;
        }
    }
    return true;
}

vector<edge> c;
long long dep[MAX],ml=0,l;
stack<pair<int,edge>> st;
void cycle(int s){
	edge t = G[s][0];
	st.push({0,t});
	int dist = 0;
	while(!st.size()){
		bool flag = true;
		pair<int,edge> v = st.top(); st.pop; c.push_back(v.second);
		int x = v.first, y = v.second.to
		dep[y] = x; dist++;
		for(auto&&e : G[v]){
			if(dep[e.to]==-1){
				st.push({dist,e});
				flag = false;
			}
			else if(dep[e.to]<dep[y]-1 && !(ml & 1)){
				ml = dep[y]+1-dep[e.to];
				if(ml & 1){
					l = e.to;
					return ;
				}
			}
		}
		if(flag){
			c.pop_back();
		}
	}
}

int main(){
    int i;
    cin >> n >> m;
    for(i=0;i<m;i++){
        long long u,v,s;
        cin >> u >> v >> s;
        u--; v--;
        edge e;
        e.to = v; e.cost = s;
        G[u].push_back(e);
        e.to = u; e.cost = s;
        G[v].push_back(e);
        pair<int,int> p = make_pair(u,v);
        E[i] = make_pair(p,s);
    }
    long long min1 = 1000000000,min2 = 10000000000;
    dfs(0,0,0);
    for(i=0;i<n;i++){
        if(dist[i]%2==1){
            min1 = min(min1,a[i]);
        }else{
            min2 = min(min2,a[i]);
        }
    }
    if(bip(0,1)){
        if(!check()){
            cout << 0 << endl;
            return 0;
        }
        if(min1<=0){
            cout << 0 << endl;
        }else if(min1>0 && min2>=0){
            cout << min1-1 << endl;
        }else if(min1>=0 && min2<0){
            if(min1+min2<=0){
                cout << 0 << endl;
            }else{
            	cout << min1+min2-1 << endl;
            }
        }
    }else{
 		 /*struct data { long long a0; int sign; };
 		 vector<data> t(n);
 		 for (auto&& i : t) {
   			 i.sign = 0;
 		 }
 		 queue<int> q;
  		t[0].a0 = 0;
  		t[0].sign = 1;
 		q.push(0);
  		bool fixed = false;
  		long long s = 0;
  		while (not q.empty()) {
    		int v = q.front(); q.pop();
   				 for (auto&& e : G[v]) {
      				if (t[e.to].sign == 0) {
        				t[e.to].sign = t[v].sign * -1;
        				t[e.to].a0 = e.cost - t[v].a0;
        				q.push(e.to);
      				} else {
        				if (!fixed) {
          					if (t[v].sign != t[e.to].sign) {
            					if (t[v].a0 + t[e.to].a0 != e.cost) {
              						cout << 0 << '\n';
              						return 0;
           						}
          					} else {
           						 s = t[v].sign * (e.cost - t[v].a0 - t[e.to].a0);
            							if (s & 1) {
              								cout << 0 << '\n';
              								return 0;
            							} else {
              								s /= 2;
            }
            fixed = true;
          }
        } else {
          if (t[v].a0 + t[v].sign * s + t[e.to].a0 + t[e.to].sign * s != e.cost) {
            cout << 0 << '\n';
            return 0;
          }
        }
      }
    }
  }*/

  /*if (fixed == false) {
    lint mi = 1e18, mi2 = 1e18;
    for (int i = 0; i < n; i++) {
      if (a[i].sign == 1) {
        mi2 = min(mi2, a[i].a0);
      } else {
        mi = min(mi, a[i].a0);
      }
    }
    if (mi <= 1 or mi2 + mi <= 0) {
      cout << 0 << '\n';
    } else {
      cout << mi + mi2 - 1 << '\n';
    }
    	} elseif(fixed) {
    for (int i = 0; i < n; i++) {
      if (t[i].a0 + t[i].sign * s <= 0) {
        cout << 0 << '\n';
        break;
      }
      if (i == n - 1) {
        cout << 1 << '\n';
      }
    }
  }
  return 0;*/

   for(i=0;i<n;i++){
        dep[i] = -1;
    }
    cycle(0);
    int x = 1,k = c.size() - ml;
    long long sum = 0;
    for(i=d.size()-1;i>=k;i--){
    	sum += x*d[i].cost;
    	x *= -1;
    }
    if(sum%2==1){
    	cout << 0 << endl;
    	return 0;
    }
    dfs(l,0,sum/2);
    for(i=0;i<n;i++){
    	min1 = min(min1,a[i]);
    }
    if(check() && min1>0){
        	cout << 1 << endl;
        }else{
            cout << 0 << endl;
        }
    }
}


Compiler output:
a.cc: In function 'void cycle(int)':
a.cc:48:49: error: invalid use of non-static member function 'void std::stack<_Tp, _Sequence>::pop() [with _Tp = std::pair<int, edge>; _Sequence = std::deque<std::pair<int, edge>, std::allocator<std::pair<int, edge> > >]'
   48 |                 pair<int,edge> v = st.top(); st.pop; c.push_back(v.second);
      |                                              ~~~^~~
In file included from /usr/include/c++/14/stack:63,
                 from a.cc:5:
/usr/include/c++/14/bits/stl_stack.h:291:7: note: declared here
  291 |       pop()
      |       ^~~
a.cc:50:17: error: expected ',' or ';' before 'dep'
   50 |                 dep[y] = x; dist++;
      |                 ^~~
a.cc:51:32: error: no match for 'operator[]' (operand types are 'std::vector<edge> [100010]' and 'std::pair<int, edge>')
   51 |                 for(auto&&e : G[v]){
      |                                ^
a.cc:53:40: error: no matching function for call to 'std::stack<std::pair<int, edge> >::push(<brace-enclosed initializer list>)'
   53 |                                 st.push({dist,e});
      |                                 ~~~~~~~^~~~~~~~~~
/usr/include/c++/14/bits/stl_stack.h:258:7: note: candidate: 'void std::stack<_Tp, _Sequence>::push(const value_type&) [with _Tp = std::pair<int, edge>; _Sequence = std::deque<std::pair<int, edge>, std::allocator<std::pair<int, edge> > >; value_type = std::pair<int, edge>]'
  258 |       push(const value_type& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_stack.h:258:30: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const std::stack<std::pair<int, edge> >::value_type&' {aka 'const std::pair<int, edge>&'}
  258 |       push(const value_type& __x)
      |            ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_stack.h:263:7: note: candidate: 'void std::stack<_Tp, _Sequence>::push(value_type&&) [with _Tp = std::pair<int, edge>; _Sequence = std::deque<std::pair<int, edge>, std::allocator<std::pair<int, edge> > >; value_type = std::pair<int, edge>]'
  263 |       push(value_type&& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_stack.h:263:25: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::stack<std::pair<int, edge> >::value_type&&' {aka 'std::pair<int, edge>&&'}
  263 |       push(value_type&& __x)
      |            ~~~~~~~~~~~~~^~~
a.cc: In function 'int main()':
a.cc:189:11: error: 'd' was not declared in this scope
  189 |     for(i=d.size()-1;i>=k;i--){
      |           ^