Problem:
Score : 400 points

Problem Statement

There are N people standing on the x-axis.
Let the coordinate of Person i be x_i.
For every i, x_i is an integer between 0 and 10^9 (inclusive).
It is possible that more than one person is standing at the same coordinate.

You will given M pieces of information regarding the positions of these people.
The i-th piece of information has the form (L_i, R_i, D_i).
This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.

It turns out that some of these M pieces of information may be incorrect.
Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.

Constraints

1 \leq N \leq 100 000

0 \leq M \leq 200 000

1 \leq L_i, R_i \leq N (1 \leq i \leq M)

0 \leq D_i \leq 10 000 (1 \leq i \leq M)

L_i \neq R_i (1 \leq i \leq M)

If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).

D_i are integers.

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M

Output

If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print Yes; if it does not exist, print No.

Sample Input 1

3 3
1 2 1
2 3 1
1 3 2

Sample Output 1

Yes

Some possible sets of values (x_1, x_2, x_3) are (0, 1, 2) and (101, 102, 103).

Sample Input 2

3 3
1 2 1
2 3 1
1 3 5

Sample Output 2

No

If the first two pieces of information are correct, x_3 - x_1 = 2 holds, which is contradictory to the last piece of information.

Sample Input 3

4 3
2 1 1
2 3 5
3 4 2

Sample Output 3

Yes

Sample Input 4

10 3
8 7 100
7 9 100
9 8 100

Sample Output 4

No

Sample Input 5

100 0

Sample Output 5

Yes

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

fn visit(
    g: &Vec<Vec<usize>>,
    v: usize,
    scc: &mut Vec<Vec<usize>>,
    stk: &mut Vec<usize>,
    inS: &mut Vec<bool>,
    low: &mut Vec<i32>,
    num: &mut Vec<i32>,
    time: &mut i32,
) {
    *time += 1;
    low[v] = *time;
    num[v] = *time;
    stk.push(v);
    inS[v] = true;
    for &w in &g[v] {
        if num[w] == 0 {
            visit(g, w, scc, stk, inS, low, num, time);
            low[v] = min(low[v], low[w]);
        } else if inS[w] {
            low[v] = min(low[v], num[w]);
        }
    }
    if low[v] == num[v] {
        let mut ls = Vec::new();
        loop {
            let w = stk.pop().unwrap();
            inS[w] = false;
            ls.push(w);
            if v == w {
                break;
            }
        }
        scc.push(ls);
    }
}
fn scc(g: &Vec<Vec<usize>>, mut sc: &mut Vec<Vec<usize>>) {
    let n = g.len();
    let mut num = vec![0i32; n];
    let mut low = vec![0i32; n];
    let mut stk: Vec<usize> = Vec::new();
    let mut inS = vec![false; n];
    let mut time = 0;
    for u in 0..n {
        if num[u] == 0 {
            visit(
                g,
                u,
                &mut sc,
                &mut stk,
                &mut inS,
                &mut low,
                &mut num,
                &mut time,
            );
        }
    }
}
fn main() {
    let (n, m) = readln();
    let mut edges: Vec<(usize, usize, i64)> = Vec::new();
    let mut g: Vec<Vec<usize>> = vec![Vec::new(); n];
    for i in 0..m {
        let (x, y, d): (usize, usize, i64) = readln();
        edges.push((x - 1, y - 1, d));
        g[x - 1].push(y - 1);
    }
    let mut sc = Vec::new();
    scc(&g, &mut sc);
    let mut group = vec![0; n];
    for (i, ref v) in sc.iter().enumerate() {
        for x in *v {
            group[*x as usize] = i;
        }
    }

    let answer = if edges
        .iter()
        .any(|&(x, y, d)| group[x] == group[y] && d != 0)
    {
        "No"
    } else {
        let mut solve = || {
            let t = group.len();
            let mut dp = vec![None; t];
            dp[0] = Some(0i64);
            edges.sort_by_key(|&(x, _, _)| group[x]);
            let mut rest = Vec::new();
            rest.append(&mut edges);
            let mut cnt = 0;
            loop {
                let mut nrest = Vec::new();
                cnt += 1;
                if rest.len() == 0 || cnt >= 10 {
                    break;
                }
                for &(x, y, d) in &rest {
                    let gx = group[x];
                    let gy = group[y];
                    match (dp[gx], dp[gy]) {
                        (Some(l), Some(r)) => {
                            if l + d != r {
                                return "No";
                            }
                        }
                        (None, Some(r)) => {
                            dp[gx] = Some(r - d);
                        }
                        (Some(l), None) => {
                            dp[gy] = Some(l + d);
                        }
                        (None, None) => {
                            nrest.push((x, y, d));
                        }
                    }
                }
                rest.clear();
                rest.append(&mut nrest);
            }
            "Yes"
        };
        solve()
    };
    println!("{}", answer);
}

// --- template ---
#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{HashMap, HashSet};

pub trait FromLn {
    fn fromln(s: &str) -> Self;
}
pub fn readln<T: FromLn>() -> T {
    let mut buf = String::new();
    let _ = ::std::io::stdin().read_line(&mut buf).unwrap();
    T::fromln(buf.trim())
}
pub fn readlns<T: FromLn>(n: usize) -> Vec<T> {
    let mut vs = vec![];
    for _ in 0..n {
        vs.push(readln());
    }
    vs
}
macro_rules! fromln_primitives {
    ($($t:ty),*) => { $(
        impl FromLn for $t {
            fn fromln(s: &str) -> $t {
                s.parse().unwrap()
            }
        }
    )* }
}
fromln_primitives!(
    String,
    bool,
    f32,
    f64,
    isize,
    i8,
    i16,
    i32,
    i64,
    usize,
    u8,
    u16,
    u32,
    u64
);
impl<T> FromLn for Vec<T>
where
    T: FromLn,
{
    fn fromln(s: &str) -> Vec<T> {
        s.split_whitespace().map(T::fromln).collect()
    }
}
impl FromLn for Vec<char> {
    fn fromln(s: &str) -> Vec<char> {
        s.chars().collect()
    }
}
macro_rules! fromln_tuple {
    ($($t:ident),*) => {
        impl<$($t),*> FromLn for ($($t),*) where $($t: FromLn),* {
            fn fromln(s: &str) -> ($($t),*) {
                let mut it = s.split_whitespace();
                let t = ($($t::fromln(it.next().unwrap())),*);
                assert_eq!(it.next(), None);
                t
            }
        }
    }
}
fromln_tuple!(A, B);
fromln_tuple!(A, B, C);
fromln_tuple!(A, B, C, D);
fromln_tuple!(A, B, C, D, E);
fromln_tuple!(A, B, C, D, E, F);