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: Accepted)

using CompLib.Util;
using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    int N, M;
    List<Edge>[] Edges;
    long[] S;

    int[] Dist;
    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        M = sc.NextInt();
        Edges = new List<Edge>[N];
        for (int i = 0; i < N; i++)
        {
            Edges[i] = new List<Edge>();
        }
        S = new long[M];
        for (int i = 0; i < M; i++)
        {
            int u = sc.NextInt() - 1;
            int v = sc.NextInt() - 1;
            long s = sc.NextLong();
            Edges[u].Add(new Edge(v, i));
            Edges[v].Add(new Edge(u, i));
            S[i] = s;
        }


        // 2部グラフか?
        bool isBipartiteGraph = Check();

        // 2部グラフじゃない
        // 構成可能 1 else 0

        // 2部グラフ

        if (isBipartiteGraph)
        {
            Type1();
        }
        else
        {
            Type2();
        }
    }

    void Type1()
    {
        // 2部グラフ
        // 頂点集合 r,bに分ける
        // rの値を増やす bの値が減る
        // 適当に構成 rの最小値が0以下なら1になるようにする
        // max(0,min(b))

        // 頂点0を1として構成
        var num = new long[N];
        for (int i = 0; i < N; i++)
        {
            num[i] = long.MaxValue;
        }

        var q = new Queue<int>();
        num[0] = 1;
        q.Enqueue(0);

        while (q.Count > 0)
        {
            var d = q.Dequeue();
            foreach (var e in Edges[d])
            {
                if (num[e.To] == long.MaxValue)
                {
                    // num[e.To] + num[d] = s[e.Index]
                    num[e.To] = S[e.Index] - num[d];
                    q.Enqueue(e.To);
                }
                else
                {
                    // 構成不可能なら0
                    if (num[e.To] + num[d] != S[e.Index])
                    {
                        Console.WriteLine(0);
                        return;
                    }
                }
            }
        }

        // rMinは1未満のみ考える
        long rMin = 1;

        long bMin = int.MaxValue;

        for (int i = 0; i < N; i++)
        {
            if (Dist[i] % 2 == 0)
            {
                rMin = Math.Min(rMin, num[i]);
            }
            else
            {
                bMin = Math.Min(bMin, num[i]);
            }
        }
        long ans = bMin - (1 - rMin);

        Console.WriteLine(Math.Max(0, ans));
    }
    void Type2()
    {
        // 2部グラフじゃない

        // 奇数長の閉路からすべての値が決まる
        // すべて正整数か? 1:0

        var num = new long[N];
        for (int i = 0; i < N; i++)
        {
            num[i] = long.MaxValue;
        }

        // num[0] = 0として適当に構成
        // 奇数回、偶数回両方で行ける頂点を見つける
        // num[0]を増やす 距離が偶数 増える　奇数 減る
        // 
        var dist = new int[N];
        for (int i = 0; i < N; i++)
        {
            dist[i] = -1;
        }

        var q = new Queue<int>();
        num[0] = 0;
        dist[0] = 0;
        q.Enqueue(0);

        long zero = long.MinValue;

        while (q.Count > 0)
        {
            var d = q.Dequeue();

            foreach (var e in Edges[d])
            {

                if (num[e.To] == long.MaxValue)
                {
                    num[e.To] = S[e.Index] - num[d];
                    dist[e.To] = dist[d] + 1;
                    q.Enqueue(e.To);
                }
                else
                {
                    if (dist[e.To] % 2 == (dist[d] + 1) % 2)
                    {
                        if (num[e.To] + num[d] != S[e.Index])
                        {
                            Console.WriteLine(0);
                            return;
                        }
                    }
                    else
                    {
                        if (dist[e.To] % 2 == 0)
                        {
                            // nun[0] を増やすと num[e.to]が増える S[e.index] - num[d]が減る
                            zero = ((S[e.Index] - num[d]) - num[e.To]) / 2;
                        }
                        else
                        {
                            zero = (num[e.To] - (S[e.Index] - num[d])) / 2;
                        }
                        break;
                    }
                }
            }
            if (zero != long.MinValue)
            {
                break;
            }
        }
        var ans = new long[N];

        var q2 = new Queue<int>();
        for (int i = 0; i < N; i++)
        {
            ans[i] = long.MaxValue;
        }
        ans[0] = zero;
        q2.Enqueue(0);

        while (q2.Count > 0)
        {
            var d = q2.Dequeue();
            foreach (var e in Edges[d])
            {
                if (ans[e.To] == long.MaxValue)
                {
                    ans[e.To] = S[e.Index] - ans[d];
                    q2.Enqueue(e.To);
                }
                else
                {
                    if (ans[e.To] + ans[d] != S[e.Index])
                    {
                        Console.WriteLine(0);
                        return;
                    }
                }
            }
        }

        Console.WriteLine(ans.Any(l => l <= 0) ? 0 : 1);
    }
    bool Check()
    {
        Dist = new int[N];
        for (int i = 0; i < N; i++)
        {
            Dist[i] = -1;
        }

        var q = new Queue<int>();
        Dist[0] = 0;
        q.Enqueue(0);

        while (q.Count > 0)
        {
            var d = q.Dequeue();
            foreach (var edge in Edges[d])
            {
                if (Dist[edge.To] == -1)
                {
                    Dist[edge.To] = Dist[d] + 1;
                    q.Enqueue(edge.To);
                }
                else
                {
                    if (Dist[edge.To] % 2 != (Dist[d] + 1) % 2)
                    {
                        return false;
                    }
                }
            }
        }
        return true;
    }



    public static void Main(string[] args) => new Program().Solve();
}

struct Edge
{
    public int To, Index;
    public Edge(int t, int i)
    {
        To = t;
        Index = i;
    }
}

namespace CompLib.Util
{
    using System;
    using System.Linq;
    class Scanner
    {
        private int _index;
        private string[] _line;
        const char separator = ' ';

        public Scanner()
        {
            _index = 0;
            _line = new string[0];
        }

        public string Next()
        {
            while (_index >= _line.Length)
            {
                _line = Console.ReadLine().Split(separator);
                _index = 0;
            }
            return _line[_index++];
        }
        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            _line = Console.ReadLine().Split(separator);
            _index = _line.Length;
            return _line;
        }
        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
    }
}