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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

long long sabun[102000][2];
bool visited[102000][2];
bool impossible;
vector<pair<int, long long> > path[102000];


void visit(int from, int to, long long cost, int checker)
{
    //cout << from << endl;
    long long delta = cost - sabun[from][checker];
    checker = 1 - checker;
    if(visited[to][checker] == true){
        if(delta != sabun[to][checker]){
            impossible = true;
        }
        return;
    }
    sabun[to][checker] = delta;
    visited[to][checker] = true;
    for(int i = 0; i < path[to].size(); i++){
        visit(to, path[to][i].first, path[to][i].second, checker);
    }
}

int main() {
    impossible = false;
    //cout.precision(10);
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        sabun[i][0] = 0;//a + hoge
        sabun[i][1] = 0;//-a + hoge
        visited[i][0] = false;
        visited[i][1] = false;
    }
    for(int i = 1; i <= m; i++){
        int u, v;
        long long s;
        cin >> u >> v >> s;
        pair<int, long long> in;
        in.first = u;
        in.second = s;
        path[v].push_back(in);
        in.first = v;
        path[u].push_back(in);
    }
    visit(1, 1, 0, 0);
    if(impossible){
        cout << -1 << endl;
        return 0;
    }
    for(int i = 1; i <= n; i++){
        for(int j = 0; j <= 1; j++){
            if(visited[i][j]){
                //cout << j << endl;
            }
        }
        //cout << sabun[i][0] << " " << sabun[i][1] << endl;
    }
    bool check = true;
    for(int i = 1; i <= n; i++){
        if(visited[i][0] && visited[i][1]){
            check = false;
        }
    }
    if(check){
        long long maxi = INF * INF;
        long long mini = 0;
        for(int i = 1; i <= n; i++){
            if(visited[i][0]){
                maxi = min(sabun[i][0], maxi);
            }
            mini = max(mini, -sabun[i][1]);
        }
        long long ans = maxi - mini - 1;
        if(ans < 0){
            ans = 0;
        }
        cout << ans << endl;
        return 0;
    }
    vector<long long> answer;
    for(int i = 1; i <= n; i++){
        if(visited[i][0] && visited[i][1]){
            if((sabun[i][0] - sabun[i][1]) % 2 != 0){
                cout << 0 << endl;
                return 0;
            }
            long long ans = (sabun[i][0] - sabun[i][1]) / 2;
            if(ans <= 0){
                cout << 0 << endl;
                return 0;
            }
            answer.push_back(ans);
        }
    }
    for(int i = 0; i < answer.size(); i++){
        if(answer[i] != answer[0]){
            cout << 0 << endl;
            return 0;
        }
    }
    cout << 1 << endl;
    return 0;
}