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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace Program
{
    public class SOUNDHOUND2018E
    {
        static public void Main(string[] args)
        {
            var n = NextInt;
            var m = NextInt;
            var edge = new List<Tuple<int, int>>[n];
            for (int i = 0; i < n; ++i)
            {
                edge[i] = new List<Tuple<int, int>>();
            }
            var vertexMaxNum = new int[n];
            for (int i = 0; i < m; ++i)
            {
                var u = NextInt - 1;
                var v = NextInt - 1;
                var s = NextInt;
                edge[u].Add(Tuple.Create(v, s));
                edge[v].Add(Tuple.Create(u, s));
                if (vertexMaxNum[u] == 0) vertexMaxNum[u] = s - 1;
                if (vertexMaxNum[v] == 0) vertexMaxNum[v] = s - 1;
                if (vertexMaxNum[u] > s - 1) vertexMaxNum[u] = s - 1;
                if (vertexMaxNum[v] > s - 1) vertexMaxNum[v] = s - 1;
            }

            var checkNum = 0;
            var checkV = -1;
            var vertexNumbers = new int[n];
            var vertexSigns = new int[n];
            {
                var doneList = new bool[n];
                var queue = new Queue<int>();
                queue.Enqueue(0);
                doneList[0] = true;
                vertexNumbers[0] = 1;
                vertexSigns[0] = 1;
                while (queue.Any() && checkV == -1)
                {
                    var v = queue.Dequeue();
                    foreach (var item in edge[v])
                    {
                        var vertex = item.Item1;
                        var cost = item.Item2;
                        if (!doneList[vertex])
                        {
                            doneList[vertex] = true;
                            vertexNumbers[vertex] = cost - vertexNumbers[v];
                            vertexSigns[vertex] = vertexSigns[v] == 1 ? -1 : 1;
                            queue.Enqueue(vertex);
                        }
                        else
                        {
                            if (vertexSigns[vertex] != vertexSigns[v])
                            {
                                if (vertexNumbers[vertex] != cost - vertexNumbers[v])
                                {
                                    Console.WriteLine(0);
                                    return;
                                }
                            }
                            else
                            {
                                if ((vertexNumbers[vertex] + cost - vertexNumbers[v]) % 2 == 1)
                                {
                                    Console.WriteLine(0);
                                    return;
                                }
                                checkV = vertex;
                                checkNum = (vertexNumbers[vertex] + cost - vertexNumbers[v]) / 2;
                                break;
                            }
                        }
                    }
                }
            }
            if (checkV != -1)
            {
                var doneList = new bool[n];
                var queue = new Queue<int>();
                var vertexNumbersChk = new int[n];
                queue.Enqueue(checkV);
                doneList[checkV] = true;
                vertexNumbersChk[checkV] = checkNum;
                while (queue.Any())
                {
                    var v = queue.Dequeue();
                    foreach (var item in edge[v])
                    {
                        var vertex = item.Item1;
                        var cost = item.Item2;
                        if (!doneList[vertex])
                        {
                            doneList[vertex] = true;
                            vertexNumbersChk[vertex] = cost - vertexNumbersChk[v];
                            queue.Enqueue(vertex);
                        }
                        else
                        {
                            if (vertexNumbersChk[vertex] != cost - vertexNumbersChk[v])
                            {
                                Console.WriteLine(0);
                                return;
                            }
                        }
                    }
                }
                Console.WriteLine(1);
                return;
            }

            var ans = 1000000001;
            for (int i = 0; i < n; ++i)
            {
                if (vertexSigns[i] == 1)
                {
                    ans = Math.Min(ans, vertexMaxNum[i] - Math.Max(vertexNumbers[i], 1));

                }
                else
                {
                    ans = Math.Min(ans, Math.Min(vertexNumbers[i], vertexMaxNum[i]) - 1);
                }
            }


            Console.WriteLine(Math.Max(ans + 1, 0));
        }

        static class Console_
        {
            private static Queue<string> param = new Queue<string>();
            public static string NextString()
            {
                if (param.Count == 0) foreach (var item in Console.ReadLine().Split(' ')) param.Enqueue(item);
                return param.Dequeue();
            }
        }
        static int NextInt => int.Parse(Console_.NextString());
        static long NextLong => long.Parse(Console_.NextString());
        static string NextString => Console_.NextString();

        static IEnumerable<long> Prime(long x)
        {
            if (x < 2) yield break;
            yield return 2;
            var halfx = x / 2;
            var table = new bool[halfx + 1];
            var max = (long)(Math.Sqrt(x) / 2);
            for (long i = 1; i <= max; ++i)
            {
                if (table[i]) continue;
                var add = 2 * i + 1;
                yield return add;
                for (long j = 2 * i * (i + 1); j <= halfx; j += add) table[j] = true;
            }
            for (long i = max + 1; i <= halfx; ++i) if (!table[i]) yield return 2 * i + 1;
        }
        static IEnumerable<long> Divisor(long x)
        {
            if (x < 1) yield break;
            var max = (long)Math.Sqrt(x);
            for (long i = 1; i < max; ++i)
            {
                if (x % i != 0) continue;
                yield return i;
                if (i != x / i) yield return x / i;
            }
        }
        static long GCD(long a, long b)
        {
            long tmpa = a, tmpb = b;
            while (tmpb > 0) { var tmp = tmpb; tmpb = tmpa % tmpb; tmpa = tmp; }
            return tmpa;
        }
    }
}