Language: C++

#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <iostream>
#define REP(i,a,b)for (int i=a;i<=b;++i)
#define PER(i,a,b)for (int i=a;i>=b;--i)
#define RVC(i,S)for (int i=0;i<S.size();++i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define debug(...) fprintf(stderr,__VA_ARGS__)
using namespace std;

typedef long long LL;
typedef pair<int,int> pii;
typedef vector<int> VI;

const int N=100005;
int n,m,fa[N][2],mx[N],dir[N],tok[N],dep[N];
int sz,eu[N],ev[N],es[N],onc[N];
VI ed[N],si[N];

void addedge(int u,int v,int s){
	ed[u].pb(v);
	si[u].pb(s);
	mx[u]=min(mx[u],s-1);
}

void dfs(int x,int f){
	RVC(i,ed[x])if(ed[x][i]!=f){
		tok[ed[x][i]]=si[x][i]-tok[x];
		dir[ed[x][i]]=-dir[x];
		dfs(ed[x][i],x);
	}
}

void invalid(){
	puts("0");
	exit(0);
}

void tree_case(){
	dir[1]=1;
	tok[1]=1;
	dfs(1,0);
	//REP(i,1,n)debug("%d, %d\n",tok[i],dir[i]);
	int not_positive=0;
	REP(i,1,n)if(tok[i]<=0){
		not_positive=1;
		break;
	}
	if(not_positive){
		int d=0,mn=0;
		REP(i,1,n)if(tok[i]<=0){
			mn=min(mn,tok[i]);
			if(d==0)d=dir[i];
			if(d!=dir[i])invalid();
		}
		REP(i,1,n){
			tok[i]+=d*mn;
			if(tok[i]<=0)invalid();
		}
		int ans=0x3f3f3f3f;
		REP(i,1,n)if(dir[i]==d)
			ans=min(ans,mx[i]-tok[i]);
		printf("%d\n",ans);
	} else{
		int dt=0x3f3f3f3f;
		int ans=dt;
		REP(i,1,n)if(dir[i]==1)
			dt=min(dt,mx[i]-tok[i]);
		REP(i,1,n)tok[i]+=dt;
		REP(i,1,n)if(dir[i]==1)
			ans=min(ans,tok[i]);
		printf("%d\n",ans);
	}
}

void addequ(int x){
	++sz;
	eu[sz]=x; ev[sz]=fa[x][0]; es[sz]=ed[fa[x][0]][fa[x][1]];
	onc[fa[x][0]]=1;
}

void gao(int u,int v,int ke){
	int x=u,y=v;
	++sz;
	eu[sz]=u; ev[sz]=v; es[sz]=s;
	onc[u]=onc[v]=1;
	while(dep[x]>dep[y]){
		addequ(x);
		x=fa[x];
	}
	swap(x,y);
	while(dep[x]>dep[y]){
		addequ(x);
		x=fa[x];
	}
	while(x!=y){
		addequ(x);
		addequ(y);
		x=fa[x][0];
		y=fa[y][0];
	}

}

void find_cycle(int x,int f){
	tok[x]=1;
	dep[x]=dep[f]+1;
	RVC(i,ed[x]){
		int y=ed[x][i];
		fa[y][0]=x;
		fa[y][1]=i;
		if(y!=f&&tok[y]){
			gao(x,y,si[x][i]);
		}else find_cycle(y,x);
	}
}

int main(){
	scanf("%d%d",&n,&m);
	memset(mx,0x3f,sizeof mx);
	REP(i,1,m){
		int u,v,s;
		scanf("%d%d%d",&u,&v,&s);
		addedge(u,v,s);
		addedge(v,u,s);
	}
	if(m==n-1)tree_case();
	else find_cycle(1,0);
	return 0;
}

Compiler output:
a.cc: In function 'void gao(int, int, int)':
a.cc:96:36: error: 's' was not declared in this scope
   96 |         eu[sz]=u; ev[sz]=v; es[sz]=s;
      |                                    ^
a.cc:100:23: error: invalid conversion from 'int*' to 'int' [-fpermissive]
  100 |                 x=fa[x];
      |                   ~~~~^
      |                       |
      |                       int*
a.cc:105:23: error: invalid conversion from 'int*' to 'int' [-fpermissive]
  105 |                 x=fa[x];
      |                   ~~~~^
      |                       |
      |                       int*
a.cc: In function 'int main()':
a.cc:131:9: error: 'memset' was not declared in this scope
  131 |         memset(mx,0x3f,sizeof mx);
      |         ^~~~~~
a.cc:12:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
   11 | #include <iostream>
  +++ |+#include <cstring>
   12 | #define REP(i,a,b)for (int i=a;i<=b;++i)