Language: C++

#include<bits/stdc++.h>

using namespace std;

#define LL long long
#define N 300000
#define INF 0x3f3f3f3f3f3f3f3fLL

LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
struct edge{LL v,l,nxt;}e[N];

void add(LL x,LL y,LL z){
	e[++tot].v=y; e[tot].l=z; e[tot].nxt=head[x]; head[x]=tot;
}

void dfs(LL u){
	for (LL i=head[u],v;i;i=e[i].nxt){
		v=e[i].v;
		if (f[u][0]){
			if (!g[v][0]){g[v][0]=1; g[v][1]=e[i].l-f[u][1]; dfs(v);}
			else if (g[v][1]!=e[i].l-f[u][1]){puts("0"); exit(0);}
		}
		if (g[u][0]){
			if (!f[v][0]){f[v][0]=1; f[v][1]=e[i].l-g[u][1]; dfs(v);}
			else if (f[v][1]!=e[i].l-g[u][1]){puts("0"); exit(0);}
		}
	}
}

int main(){
	scanf("%lld%lld",&n,&m);
	for (LL i=1;i<=m;++i){
		scanf("%lld%lld%lld",&x[i],&y[i],&z[i]);
		add(x[i],y[i],z[i]); add(y[i],x[i],z[i]);
	}
	f[1][0]=1; f[1][1]=0;
	dfs(1);
	if (f[1][0]&&g[1][0]){
		LL t=g[1][1]-f[1][1];
		if (t&1){puts("0"); return 0;}
		for (LL i=1;i<=n;++i)
			if (f[i][1]+t/2<=0||g[i][1]-t/2<=0||f[i][1]+t/2!=g[i][1]-t/2){puts("0"); return 0;}
		for (LL i=1;i<=m;++i)
			if (f[x[i]][1]+f[y[i]][1]+t!=z[i]){puts("0"); return 0;}
		puts("1"); return 0;
	}
	mn=1; mx=INF;
	for (LL i=1;i<=n;++i)
		if (f[i][0]) mn=max(mn,1-f[i][1]);
		else mx=min(mx,g[i][1]-1);
	printf("%lld\n",mx>=mn?mx-mn+1:0);
	
	return 0;
}
			#include<bits/stdc++.h>

using namespace std;

#define LL long long
#define N 300000
#define INF 0x3f3f3f3f3f3f3f3fLL

LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
struct edge{LL v,l,nxt;}e[N];

void add(LL x,LL y,LL z){
	e[++tot].v=y; e[tot].l=z; e[tot].nxt=head[x]; head[x]=tot;
}

void dfs(LL u){
	for (LL i=head[u],v;i;i=e[i].nxt){
		v=e[i].v;
		if (f[u][0]){
			if (!g[v][0]){g[v][0]=1; g[v][1]=e[i].l-f[u][1]; dfs(v);}
			else if (g[v][1]!=e[i].l-f[u][1]){puts("0"); exit(0);}
		}
		if (g[u][0]){
			if (!f[v][0]){f[v][0]=1; f[v][1]=e[i].l-g[u][1]; dfs(v);}
			else if (f[v][1]!=e[i].l-g[u][1]){puts("0"); exit(0);}
		}
	}
}

int main(){
	scanf("%lld%lld",&n,&m);
	for (LL i=1;i<=m;++i){
		scanf("%lld%lld%lld",&x[i],&y[i],&z[i]);
		add(x[i],y[i],z[i]); add(y[i],x[i],z[i]);
	}
	f[1][0]=1; f[1][1]=0;
	dfs(1);
	if (f[1][0]&&g[1][0]){
		LL t=g[1][1]-f[1][1];
		if (t&1){puts("0"); return 0;}
		for (LL i=1;i<=n;++i)
			if (f[i][1]+t/2<=0||g[i][1]-t/2<=0||f[i][1]+t/2!=g[i][1]-t/2){puts("0"); return 0;}
		for (LL i=1;i<=m;++i)
			if (f[x[i]][1]+f[y[i]][1]+t!=z[i]){puts("0"); return 0;}
		puts("1"); return 0;
	}
	mn=1; mx=INF;
	for (LL i=1;i<=n;++i)
		if (f[i][0]) mn=max(mn,1-f[i][1]);
		else mx=min(mx,g[i][1]-1);
	printf("%lld\n",mx>=mn?mx-mn+1:0);
	
	return 0;
}
			

Compiler output:
a.cc:63:4: error: redefinition of 'long long int n'
   63 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |    ^
a.cc:9:4: note: 'long long int n' previously declared here
    9 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |    ^
a.cc:63:6: error: redefinition of 'long long int m'
   63 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |      ^
a.cc:9:6: note: 'long long int m' previously declared here
    9 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |      ^
a.cc:63:8: error: redefinition of 'long long int tot'
   63 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |        ^~~
a.cc:9:8: note: 'long long int tot' previously declared here
    9 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |        ^~~
a.cc:63:12: error: redefinition of 'long long int x [300000]'
   63 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |            ^
a.cc:9:12: note: 'long long int x [300000]' previously declared here
    9 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |            ^
a.cc:63:17: error: redefinition of 'long long int y [300000]'
   63 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |                 ^
a.cc:9:17: note: 'long long int y [300000]' previously declared here
    9 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |                 ^
a.cc:63:22: error: redefinition of 'long long int z [300000]'
   63 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |                      ^
a.cc:9:22: note: 'long long int z [300000]' previously declared here
    9 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |                      ^
a.cc:63:27: error: redefinition of 'long long int f [300000][2]'
   63 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |                           ^
a.cc:9:27: note: 'long long int f [300000][2]' previously declared here
    9 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |                           ^
a.cc:63:35: error: redefinition of 'long long int g [300000][2]'
   63 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |                                   ^
a.cc:9:35: note: 'long long int g [300000][2]' previously declared here
    9 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |                                   ^
a.cc:63:43: error: redefinition of 'long long int mn'
   63 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |                                           ^~
a.cc:9:43: note: 'long long int mn' previously declared here
    9 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |                                           ^~
a.cc:63:46: error: redefinition of 'long long int mx'
   63 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |                                              ^~
a.cc:9:46: note: 'long long int mx' previously declared here
    9 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |                                              ^~
a.cc:63:49: error: redefinition of 'long long int head [300000]'
   63 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |                                                 ^~~~
a.cc:9:49: note: 'long long int head [300000]' previously declared here
    9 | LL n,m,tot,x[N],y[N],z[N],f[N][2],g[N][2],mn,mx,head[N];
      |                                                 ^~~~
a.cc:64:8: error: redefinition of 'struct edge'
   64 | struct edge{LL v,l,nxt;}e[N];
      |        ^~~~
a.cc:10:8: note: previous definition of 'struct edge'
   10 | struct edge{LL v,l,nxt;}e[N];
      |        ^~~~
a.cc:64:25: error: conflicting declaration 'int e [300000]'
   64 | struct edge{LL v,l,nxt;}e[N];
      |                         ^
a.cc:10:25: note: previous declaration as 'edge e [300000]'
   10 | struct edge{LL v,l,nxt;}e[N];
      |                         ^
a.cc:66:6: error: redefinition of 'void add(long long int, long long int, long long int)'
   66 | void add(LL x,LL y,LL z){
      |      ^~~
a.cc:12:6: note: 'void add(long long int, long long int, long long int)' previously defined here
   12 | void add(LL x,LL y,LL z){
      |      ^~~
a.cc:70:6: error: redefinition of 'void dfs(long long int)'
   70 | void dfs(LL u){
      |      ^~~
a.cc:16:6: note: 'void dfs(long long int)' previously defined here
   16 | void dfs(LL u){
      |      ^~~
a.cc:84:5: error: redefinition of 'int main()'
   84 | int main(){
      |     ^~~~
a.cc:30:5: note: 'int main()' previously defined here
   30 | int main(){
      |     ^~~~