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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.*;
import java.util.*;
import java.math.*;
// import java.awt.Point;
 
public class Main {
    InputStream is;
    PrintWriter out;
    String INPUT = "";
 
    int mod = 1_000_000_007;
    long inf = Long.MAX_VALUE;

    void solve(){
       int n = ni();
       int m = ni();
       ArrayList<int[]>[] edge = new ArrayList[n];
       for(int i = 0; i < n; i++){
            edge[i] = new ArrayList<int[]>();
       }
       for(int i = 0; i < m; i++){
            int from = ni()-1;
            int to = ni()-1;
            int s = ni();
            edge[from].add(new int[]{to, s});
            edge[to].add(new int[]{from, s});
       }
       Deque<long[]> q = new ArrayDeque<long[]>();
       q.push(new long[]{0,0,1,-1});
       long zeromin = Long.MAX_VALUE;
       long onemin = Long.MAX_VALUE;
       long[] memo = new long[n];
       long zerobias = 0;
       long onebias = 0;
       int[] type = new int[n];
       memo[0] = 1;
       boolean flag = false;
       while(!q.isEmpty()){
            long[] res = q.poll();
            // out.println(res[0]+" "+res[1]+" "+res[2]+" "+res[3]);
            type[(int)res[0]] = (int)res[1];
            if(res[1]==0) zeromin = Math.min(zeromin, res[2]);
            else onemin = Math.min(onemin, res[2]);

            for(int[] c : edge[(int)res[0]]){
                if(c[0]==res[3]) continue;
                long cost = c[1]-res[2];
                boolean tmpflag = false;
                if(cost<=0){
                    tmpflag = true;
                    long plus = 1-cost;
                    if(res[1]==0){
                        zeromin+=plus;
                        onemin-=plus;
                        zerobias += plus;
                        onebias -= plus;
                    }
                    else{
                        zeromin-=plus;
                        onemin+=plus;
                        zerobias -= plus;
                        onebias += plus;
                    }
                    if(zeromin<=0||onemin<=0){
                        out.println(0);
                        return;
                    }
                    cost = 1;
                }
                if(memo[c[0]]>0){
                    flag = true;
                    long tmp = memo[c[0]];
                    if(type[c[0]]==0) tmp += zerobias;
                    else tmp += onebias;
                    if(tmp!=cost){
                        if(type[c[0]]!=type[(int)res[0]]){
                            out.println(0);
                            return;
                        }
                        else{
                            long dif = cost-memo[c[0]];
                            if(dif%2!=0){
                                out.println(0);
                                return;
                            }
                            long plus = dif/2;
                            cost += plus;
                            if(res[1]==0){
                                zeromin+=plus;
                                onemin-=plus;
                                zerobias += plus;
                                onebias -= plus;
                            }
                            else{
                                zeromin-=plus;
                                onemin+=plus;
                                zerobias -= plus;
                                onebias += plus;
                            }
                            if(zeromin<=0||onemin<=0){
                                out.println(0);
                                return;
                            }
                        }
                        out.println(0);
                        return;
                    }
                    else continue;
                }
                q.push(new long[]{c[0], 1-res[1], cost, res[0]});
                memo[c[0]] = cost;
            }
       }
       if(flag){
        out.println(1);
        return;
       }
       out.println(Math.max(onemin, zeromin));

    }

    static class Dijkstra {
        int n;
        ArrayList<Pair>[] G;
        long INF = Long.MAX_VALUE / 3;

        public Dijkstra(int n) {
            this.n = n;
            G = new ArrayList[n];
            for (int i = 0; i < n; i++) {
                G[i] = new ArrayList<>();
            }
        }

        public void addDirectedEdge(int from, int to, int cost) {
            G[from].add(new Pair(to, cost));
        }

        public long[] getDist(int s) {
            PriorityQueue<Pair> Q = new PriorityQueue<>();
            Q.add(new Pair(s, 0));
            long[] dist = new long[n];
            Arrays.fill(dist, INF);
            boolean[] used = new boolean[n];
            while (!Q.isEmpty()) {
                Pair p = Q.poll();
                if (used[p.x]) continue;
                used[p.x] = true;
                dist[p.x] = p.y;

                for (Pair edge : G[p.x]) {
                    Q.add(new Pair(edge.x, p.y + edge.y));
                }
            }
            return dist;
        }

        class Pair implements Comparable<Pair> {
            int x;
            long y;

            Pair(int x, long y) {
                this.x = x;
                this.y = y;
            }

            public int compareTo(Pair p) {
                return Long.compare(y, p.y);
            }

        }

    }

    void run() throws Exception
    {
        is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        out = new PrintWriter(System.out);
        long s = System.currentTimeMillis();
        solve();
        out.flush();
        if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
    }
    
    public static void main(String[] args) throws Exception { new Main().run(); }
    
    private byte[] inbuf = new byte[1024];
    private int lenbuf = 0, ptrbuf = 0;
    
    private int readByte()
    {
        if(lenbuf == -1)throw new InputMismatchException();
        if(ptrbuf >= lenbuf){
            ptrbuf = 0;
            try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
            if(lenbuf <= 0)return -1;
        }
        return inbuf[ptrbuf++];
    }
    
    private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
    private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
    
    private double nd() { return Double.parseDouble(ns()); }
    private char nc() { return (char)skip(); }
    
    private String ns()
    {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!(isSpaceChar(b) && b != ' ')){
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    
    private char[] ns(int n)
    {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while(p < n && !(isSpaceChar(b))){
            buf[p++] = (char)b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }
    
    private char[][] nm(int n, int m)
    {
        char[][] map = new char[n][];
        for(int i = 0;i < n;i++)map[i] = ns(m);
        return map;
    }
    
    private int[] na(int n)
    {
        int[] a = new int[n];
        for(int i = 0;i < n;i++)a[i] = ni();
        return a;
    }
    
    private int ni()
    {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
    
    private long nl()
    {
        long num = 0;
        int b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
    
    private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
 
}