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 minS = 1000000001;
            var minV = -1;
            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;
                if (s < minS)
                {
                    minS = s;
                    minV = u;
                }
            }

            var minNum = new int[n];
            var maxNum = new int[n];
            var checkNumA = 0;
            var checkSignA = 0;
            var checkVA = -1;
            var checkNumB = 0;
            var checkSignB = 0;
            var checkVB = -1;
            {
                var doneList = new bool[n];
                var queue = new Queue<int>();
                var vertexNumbers = new int[n];
                var vertexSigns = new int[n];
                queue.Enqueue(minV);
                doneList[minV] = true;
                vertexNumbers[minV] = 1;
                vertexSigns[minV] = 1;
                minNum[minV] = vertexNumbers[minV];
                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;
                            vertexNumbers[vertex] = cost - vertexNumbers[v];
                            vertexSigns[vertex] = vertexSigns[v] == 1 ? -1 : 1;
                            if (vertexSigns[vertex] == 1)
                            {
                                minNum[vertex] = vertexNumbers[vertex];
                            }
                            else
                            {
                                maxNum[vertex] = vertexNumbers[vertex];
                            }
                            queue.Enqueue(vertex);
                        }
                        else
                        {
                            if ((vertexNumbers[vertex] != cost - vertexNumbers[v]) && checkVA == -1)
                            {
                                if (vertexSigns[vertex] != vertexSigns[v] || (Math.Abs(vertexNumbers[vertex] - cost + vertexNumbers[v]) % 2 == 1))
                                {
                                    Console.WriteLine(0);
                                    return;
                                }
                                else
                                {
                                    if (vertexSigns[vertex] == 1)
                                    {
                                        checkVA = vertex;
                                        checkSignA = 1;
                                        checkNumA = vertexNumbers[vertex] - (vertexNumbers[vertex] - cost + vertexNumbers[v]) / 2;
                                    }
                                    else
                                    {
                                        checkVA = vertex;
                                        checkSignA = -1;
                                        checkNumA = vertexNumbers[vertex] + (vertexNumbers[vertex] - cost + vertexNumbers[v]) / 2;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (checkVA != -1)
            {
                var doneList = new bool[n];
                var queue = new Queue<int>();
                var vertexNumbers = new int[n];
                var vertexSigns = new int[n];
                queue.Enqueue(checkVA);
                doneList[checkVA] = true;
                vertexNumbers[checkVA] = checkNumA;
                vertexSigns[checkVA] = checkSignA;
                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;
                            vertexNumbers[vertex] = cost - vertexNumbers[v];
                            vertexSigns[vertex] = vertexSigns[v] == 1 ? -1 : 1;
                            if (vertexSigns[vertex] == 1)
                            {
                                minNum[vertex] = vertexNumbers[vertex];
                            }
                            else
                            {
                                maxNum[vertex] = vertexNumbers[vertex];
                            }
                            queue.Enqueue(vertex);
                        }
                        else
                        {
                            if (vertexNumbers[vertex] != cost - vertexNumbers[v])
                            {
                                Console.WriteLine(0);
                                return;
                            }
                        }
                    }
                }
            }
            {
                var doneList = new bool[n];
                var queue = new Queue<int>();
                var vertexNumbers = new int[n];
                var vertexSigns = new int[n];
                queue.Enqueue(minV);
                doneList[minV] = true;
                vertexNumbers[minV] = minS - 1;
                vertexSigns[minV] = -1;
                maxNum[minV] = vertexNumbers[minV];
                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;
                            vertexNumbers[vertex] = cost - vertexNumbers[v];
                            vertexSigns[vertex] = vertexSigns[v] == 1 ? -1 : 1;
                            if (vertexSigns[vertex] == 1)
                            {
                                minNum[vertex] = vertexNumbers[vertex];
                            }
                            else
                            {
                                maxNum[vertex] = vertexNumbers[vertex];
                            }
                            queue.Enqueue(vertex);
                        }
                        else
                        {
                            if ((vertexNumbers[vertex] != cost - vertexNumbers[v]) && checkVB == -1)
                            {
                                if (vertexSigns[vertex] != vertexSigns[v] || (Math.Abs(vertexNumbers[vertex] - cost + vertexNumbers[v]) % 2 == 1))
                                {
                                    Console.WriteLine(0);
                                    return;
                                }
                                else
                                {
                                    if (vertexSigns[vertex] == 1)
                                    {
                                        checkVB = vertex;
                                        checkSignB = 1;
                                        checkNumB = vertexNumbers[vertex] - (vertexNumbers[vertex] - cost + vertexNumbers[v]) / 2;
                                    }
                                    else
                                    {
                                        checkVB = vertex;
                                        checkSignB = -1;
                                        checkNumB = vertexNumbers[vertex] + (vertexNumbers[vertex] - cost + vertexNumbers[v]) / 2;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (checkVB != -1)
            {
                var doneList = new bool[n];
                var queue = new Queue<int>();
                var vertexNumbers = new int[n];
                var vertexSigns = new int[n];
                queue.Enqueue(checkVB);
                doneList[checkVB] = true;
                vertexNumbers[checkVB] = checkNumB;
                vertexSigns[checkVB] = checkSignB;
                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;
                            vertexNumbers[vertex] = cost - vertexNumbers[v];
                            vertexSigns[vertex] = vertexSigns[v] == 1 ? -1 : 1;
                            if (vertexSigns[vertex] == 1)
                            {
                                minNum[vertex] = vertexNumbers[vertex];
                            }
                            else
                            {
                                maxNum[vertex] = vertexNumbers[vertex];
                            }
                            queue.Enqueue(vertex);
                        }
                        else
                        {
                            if (vertexNumbers[vertex] != cost - vertexNumbers[v])
                            {
                                Console.WriteLine(0);
                                return;
                            }
                        }
                    }
                }
            }

            var ans = 1000000001;
            if (checkVA != -1)
            {
                if (Math.Min(maxNum[checkVA], vertexMaxNum[checkVA]) < checkNumA || checkNumA < Math.Max(minNum[checkVA], 1))
                {
                    Console.WriteLine(0);
                    return;
                }
            }
            if (checkVB != -1)
            {
                if (Math.Min(maxNum[checkVB], vertexMaxNum[checkVB]) < checkNumB || checkNumB < Math.Max(minNum[checkVB], 1))
                {
                    Console.WriteLine(0);
                    return;
                }
            }
            if (checkVA != -1 || checkVB != -1)
            {
                Console.WriteLine(1);
                return;
            }
            for (int i = 0; i < n; ++i)
            {
                ans = Math.Min(ans, Math.Min(maxNum[i], vertexMaxNum[i]) - Math.Max(minNum[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;
        }
    }
}