Problem:
Score : 600 points

Problem Statement

Kenkoooo found a simple connected graph.
The vertices are numbered 1 through n.
The i-th edge connects Vertex u_i and v_i, and has a fixed integer s_i.

Kenkoooo is trying to write a positive integer in each vertex so that the following condition is satisfied:

For every edge i, the sum of the positive integers written in Vertex u_i and v_i is equal to s_i.

Find the number of such ways to write positive integers in the vertices.

Constraints

2 \leq n \leq 10^5

1 \leq m \leq 10^5

1 \leq u_i < v_i \leq n

2 \leq s_i \leq 10^9

If i\neq j, then u_i \neq u_j or v_i \neq v_j.

The graph is connected.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

n m
u_1 v_1 s_1
:
u_m v_m s_m

Output

Print the number of ways to write positive integers in the vertices so that the condition is satisfied.

Sample Input 1

3 3
1 2 3
2 3 5
1 3 4

Sample Output 1

1

The condition will be satisfied if we write 1,2 and 3 in vertices 1,2 and 3, respectively.
There is no other way to satisfy the condition, so the answer is 1.

Sample Input 2

4 3
1 2 6
2 3 7
3 4 5

Sample Output 2

3

Let a,b,c and d be the numbers to write in vertices 1,2,3 and 4, respectively.
There are three quadruples (a,b,c,d) that satisfy the condition:

(a,b,c,d)=(1,5,2,3)

(a,b,c,d)=(2,4,3,2)

(a,b,c,d)=(3,3,4,1)

Sample Input 3

8 7
1 2 1000000000
2 3 2
3 4 1000000000
4 5 2
5 6 1000000000
6 7 2
7 8 1000000000

Sample Output 3

0

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;

class Main implements Runnable {

	public static void main(String[] args) {
		new Thread(null, new Main(), "", Runtime.getRuntime().maxMemory()).start();
	}

	class Edge {
		int src, dst;
		long s;

		public Edge(int src_, int dst_, long s_) {
			src = src_;
			dst = dst_;
			s = s_;
		}
	}

	public void run() {
		Scanner sc = new Scanner();
		int n = sc.nextInt();// 1e5
		int m = sc.nextInt();// 1e5
		int[] u = new int[m];
		int[] v = new int[m];
		long[] s = new long[m];// 1e9
		ArrayList<Edge>[] g = new ArrayList[n];
		for (int i = 0; i < n; ++i) {
			g[i] = new ArrayList<>();
		}
		for (int i = 0; i < m; ++i) {
			u[i] = sc.nextInt();
			v[i] = sc.nextInt();
			s[i] = sc.nextLong();
			--u[i];
			--v[i];
			g[u[i]].add(new Edge(u[i], v[i], s[i]));
			g[v[i]].add(new Edge(v[i], u[i], s[i]));
		}
		int[] col = new int[n];
		long[] cnt = new long[n];
		Arrays.fill(col, -1);
		col[0] = 1;
		dfs(0, -1, col, g, cnt);
		if (max % 1 == 0.5)
			max = (int) max + 1;
		if (min % 1 == 0.5)
			min = (int) min;
		System.out.println((int) Math.max(0, max - min - 1));
	}

	double min = 0;// min < v < max
	double max = Double.MAX_VALUE / 3;

	void dfs(int cur, int par, int[] col, ArrayList<Edge>[] g, long[] cnt) {
		if (col[cur] == 1) {
			min = Math.max(min, -cnt[cur]);
		} else if (col[cur] == 0) {
			max = Math.min(max, cnt[cur]);
		}
		for (Edge e : g[cur]) {
			if (e.dst == par)
				continue;
			if (col[e.dst] != -1) {
				if (col[e.src] != col[e.dst] && cnt[e.dst] + cnt[e.src] != e.s) {
					min = Double.MAX_VALUE / 3;
					max = -Double.MAX_VALUE / 3;
				} else if (col[e.src] == col[e.dst] && col[e.src] == 1) {
					// cnt[src]+cnt[dst]+2v=e.s
					min = Math.max(min, (e.s - cnt[e.src] - cnt[e.dst]) / 2 - 1);
					max = Math.min(max, (e.s - cnt[e.src] - cnt[e.dst]) / 2) + 1;
				} else if (col[e.src] == col[e.dst] && col[e.src] == -1) {
					// cnt[src]+cnt[dst]-2v>0
					min = Math.max(min, (-e.s + cnt[e.src] + cnt[e.dst]) / 2 - 1);
					max = Math.min(max, (-e.s + cnt[e.src] + cnt[e.dst]) / 2) + 1;
				}
			} else {
				col[e.dst] = col[cur] ^ 1;
				cnt[e.dst] = e.s - cnt[e.src];
				dfs(e.dst, e.src, col, g, cnt);
			}
		}
	}

	class Scanner {
		private final InputStream in = System.in;
		private final byte[] buffer = new byte[1024];
		private int ptr = 0;
		private int buflen = 0;

		private boolean hasNextByte() {
			if (ptr < buflen) {
				return true;
			} else {
				ptr = 0;
				try {
					buflen = in.read(buffer);
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (buflen <= 0) {
					return false;
				}
			}
			return true;
		}

		private int readByte() {
			if (hasNextByte())
				return buffer[ptr++];
			else
				return -1;
		}

		private boolean isPrintableChar(int c) {
			return 33 <= c && c <= 126;
		}

		private void skipUnprintable() {
			while (hasNextByte() && !isPrintableChar(buffer[ptr]))
				ptr++;
		}

		public boolean hasNext() {
			skipUnprintable();
			return hasNextByte();
		}

		public String next() {
			if (!hasNext())
				throw new NoSuchElementException();
			StringBuilder sb = new StringBuilder();
			int b = readByte();
			while (isPrintableChar(b)) {
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		public long nextLong() {
			if (!hasNext())
				throw new NoSuchElementException();
			long n = 0;
			boolean minus = false;
			int b = readByte();
			if (b == '-') {
				minus = true;
				b = readByte();
			}
			if (b < '0' || '9' < b) {
				throw new NumberFormatException();
			}
			while (true) {
				if ('0' <= b && b <= '9') {
					n *= 10;
					n += b - '0';
				} else if (b == -1 || !isPrintableChar(b)) {
					return minus ? -n : n;
				} else {
					throw new NumberFormatException();
				}
				b = readByte();
			}
		}

		int nextInt() {
			return (int) nextLong();
		}
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}