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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <climits>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

struct e
{
    int to;
    ll c;
};

vector<e> g[112345];

int dou[112345];

ll v[112345];

bool used[112345];

bool check2Graph(int p)
{
    bool res = false;
    for (auto i : g[p])
    {
        if (dou[i.to] == dou[p])
        {
            res |= true;
        }
        else
        {
            if (dou[i.to] == -1)
            {
                dou[i.to] = 1 - dou[p];
                res |= check2Graph(i.to);
            }
        }
    }

    return res;
}

pair<ll, int> dfs_nd(int p)
{
    used[p] = true;
    P res = {-1, -1};
    for (auto i : g[p])
    {
        if (used[i.to])
        {
            if (v[i.to] + v[p] != i.c && dou[i.to] == dou[p])
            {
                res = {i.c - (v[i.to] + v[p]), p};
                break;
            }
        }
        else
        {
            v[i.to] = i.c - v[p];
            dfs_nd(i.to);
        }
    }

    return res;
}

bool dfs(int p)
{
    used[p] = true;
    bool res = true;
    for (auto i : g[p])
    {
        if (!used[i.to])
        {
            v[i.to] = i.c - v[p];
            if (!dfs(i.to))
                res = false;
        }
        else
        {
            if (v[i.to] + v[p] != i.c)
                res = false;
        }
    }
    return res;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, m;

    cin >> n >> m;
    REP(i, m)
    {
        int u, v, s;
        cin >> u >> v >> s;

        g[u].push_back({v, s});
        g[v].push_back({u, s});
    }

    memset(dou, -1, sizeof(dou));
    dou[1] = 0;

    ll res = 0;

    if (check2Graph(1))
    {
        //Not 2

        fill(used, used + n + 1, false);
        memset(v, -1, sizeof(v));

        v[1] = 1;
        P d = dfs_nd(1);

        if (d.second == -1)
        {
            bool check = true;
            for (int i = 1; i <= n; i++)
                if (v[i] < 1)
                    check = false;
            if (check)
                res = 1;
            else
                res = 0;
        }
        else if (d.first % 2)
        {
            res = 0;
        }
        else
        {
            ll tmp = v[d.second] + d.first / 2;
            fill(used, used + n + 1, false);
            memset(v, -1, sizeof(v));
            v[d.second] = tmp;

            if (dfs(d.second))
            {
                bool check = true;
                for (int i = 1; i <= n; i++)
                    if (v[i] < 1)
                        check = false;
                if (check)
                    res = 1;
                else
                    res = 0;
            }
            else
            {
                res = 0;
            }
        }
    }
    else
    {
        // 2

        fill(used, used + n + 1, false);
        memset(v, -1, sizeof(v));

        v[1] = 1;

        if (!dfs(1))
        {
            res = 0;
        }
        else
        {
            ll st = 1; //v[1]の開始地点
            ll gl = 0; //v[1]の終了地点

            ll mis = LLONG_MAX;
            ll mig = LLONG_MAX;
            for (int i = 1; i <= n; i++)
                if (dou[i] == 0)
                    mis = min(mis, v[i]);

            if (mis < 1)
                st = 2 - mis;

            for (int i = 1; i <= n; i++)
                if (dou[i] == 1)
                    mig = min(mig, v[i]);

            gl = mig;

            res = max(0ll, gl - st + 1);
        }
    }

    cout << res << endl;

    return 0;
}