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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using static System.Convert;
using static System.Math;
using static Extensions;
using static MathExtensions;

public static class Program
{
    struct Info
    {
        public int L, R, D;
    }

    static void Yes() { Console.WriteLine("Yes"); Exit(0); }
    static void No() { Console.WriteLine("No"); Exit(0); }

    public static void Solve()
    {
        var n = I;
        var m = I;
        if (m <= 1)
        {
            Console.WriteLine("Yes");
            Exit(0);
        }

        var uf = new UnionFind(n);
        var info = Repeat(m, () =>
        {
            var t = new Info
            {
                L = I - 1,
                R = I - 1,
                D = I,
            };
            uf.Unite(t.L, t.R);
            return t;
        })
        .ToList();

        var cntufg = uf.CountGroups();
        var uficmp = new CoordinateCompressor<int>(Repeat(n, i => uf.Find(i)).Distinct());
        var infog = Repeat(cntufg, () => new List<Info>()).ToArray();
        Repeat(m, i => { infog[uficmp.Compress(uf.Find(info[i].L))].Add(info[i]); });

        foreach (var i in infog)
        {
            var i1 = i;
            var p = new long?[n];
            var first = true;
            var cnt = default(int);
            var min = long.MaxValue;
            var max = long.MinValue;

            do
            {
                cnt = i1.Count;
                var i2 = new List<Info>();

                foreach (var j in i1)
                {
                    if (first)
                    {
                        p[j.L] = 0;
                        p[j.R] = j.D;
                        min = Min(Min(min, p[j.L].Value), p[j.R].Value);
                        max = Max(Max(max, p[j.L].Value), p[j.R].Value);
                        first = false;
                    }

                    if (p[j.L].HasValue)
                    {
                        if (p[j.R].HasValue)
                        {
                            if (p[j.R] - p[j.L] != j.D) No();
                        }
                        else
                        {
                            p[j.R] = p[j.L] + j.D;
                            min = Min(min, p[j.R].Value);
                            max = Max(max, p[j.R].Value);
                        }
                    }
                    else i2.Add(j);
                }

                i1 = i2;
            }
            while (i1.Count < cnt);

            if (min != long.MaxValue && max != long.MinValue && max - min > 1000000000) No();
        }

        Yes();
    }

    #region Scanners
    static TextScanner _ts;
    static char C => char.Parse(_ts.Next());
    static string S => _ts.Next();
    static int I => int.Parse(_ts.Next());
    static long L => long.Parse(_ts.Next());
    static BigInteger B => BigInteger.Parse(_ts.Next());
    static double D => double.Parse(_ts.Next());
    static decimal M => decimal.Parse(_ts.Next());
    #endregion

    public static void Main()
    {
        var sw = new StreamWriter(Console.OpenStandardOutput());
        sw.NewLine = "\n";
#if DEBUG
        sw.AutoFlush = true;
#else
        sw.AutoFlush = false;
#endif
        Console.SetOut(sw);
        _ts = new TextScanner(Console.In);
        Solve();
        Console.Out.Flush();
    }
}

public static partial class Extensions
{
}

#region Library
public class TextScanner
{
    private readonly TextReader _tr;

    public TextScanner(TextReader tr)
    {
        _tr = tr;
    }

    public string Next()
    {
        var sb = new StringBuilder();
        int i;
        do
        {
            i = _tr.Read();
            if (i == -1) throw new EndOfStreamException();
        }
        while (char.IsWhiteSpace((char)i));
        while (i != -1 && !char.IsWhiteSpace((char)i))
        {
            sb.Append((char)i);
            i = _tr.Read();
        }
        return sb.ToString();
    }
}

public class UnionFind
{
    private readonly int[] _tree;

    public UnionFind(int length)
    {
        _tree = Enumerable.Repeat(-1, length).ToArray();
    }

    public bool Unite(int i, int j)
    {
        i = Find(i);
        j = Find(j);
        if (i == j) return false;
        if (_tree[i] > _tree[j]) Swap(ref i, ref j);
        _tree[i] += _tree[j];
        _tree[j] = i;
        return true;
    }

    public bool AreUnited(int i, int j) => Find(i) == Find(j);

    public int Count(int i) => -_tree[Find(i)];

    public int CountGroups() => _tree.Count(x => x < 0);

    public int Find(int i) => _tree[i] < 0 ? i : _tree[i] = Find(_tree[i]);
}

public class CoordinateCompressor<T> where T : IComparable<T>, IEquatable<T>
{
    private readonly Dictionary<T, int> _compress;
    private readonly T[] _decompress;

    public CoordinateCompressor(IEnumerable<T> coordinates)
    {
        var arr = coordinates.ToArray();
        Array.Sort(arr);
        _compress = new Dictionary<T, int>(arr.Length);
        _decompress = arr;
        var pi = default(int);
        var pa = default(T);
        if (arr.Any()) _compress.Add(arr[0], 0);

        for (var i = 0; i < arr.Length; i++)
            if (i > 0 && !arr[i].Equals(pa)) _compress[pa = arr[i]] = pi = i;
    }

    public int Compress(T coordinate) => _compress[coordinate];

    public T Decompress(int index) => _decompress[index];
}

[DebuggerStepThrough]
public static partial class Extensions
{
    public static void Assert(bool condition)
    {
        if (!condition) throw new Exception("Assertion failed");
    }

    public static string AsString(this IEnumerable<char> source) => new string(source.ToArray());

    public static Dictionary<T, int> Bucket<T>(this IEnumerable<T> source) where T : IEquatable<T>
    {
        var dict = new Dictionary<T, int>();
        foreach (var item in source) if (dict.ContainsKey(item)) dict[item]++; else dict[item] = 1;
        return dict;
    }

    public static int[] Bucket<T>(this IEnumerable<T> source, int maxValue, Func<T, int> selector)
    {
        var arr = new int[maxValue + 1];
        foreach (var item in source) arr[selector(item)]++;
        return arr;
    }

    public static IEnumerable<int> CumSum(this IEnumerable<int> source)
    {
        var sum = 0;
        foreach (var item in source) yield return sum += item;
    }

    public static IEnumerable<long> CumSum(this IEnumerable<long> source)
    {
        var sum = 0L;
        foreach (var item in source) yield return sum += item;
    }

    public static void Exit(int exitCode)
    {
        Console.Out.Flush();
        Environment.Exit(exitCode);
    }

    public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
    {
        foreach (var item in source) action(item);
    }

    public static void ForEach<T, _>(this IEnumerable<T> source, Func<T, _> func)
    {
        foreach (var item in source) func(item);
    }

    public static void ForEach<T>(this IEnumerable<T> source, Action<T, int> action)
    {
        var i = 0;
        foreach (var item in source) action(item, i++);
    }

    public static void ForEach<T, _>(this IEnumerable<T> source, Func<T, int, _> func)
    {
        var i = 0;
        foreach (var item in source) func(item, i++);
    }

    public static void Repeat(int count, Action action)
    {
        for (var i = 0; i < count; i++) action();
    }

    public static void Repeat(int count, Action<int> action)
    {
        for (var i = 0; i < count; i++) action(i);
    }

    public static IEnumerable<T> Repeat<T>(int count, Func<T> func)
    {
        for (var i = 0; i < count; i++) yield return func();
    }

    public static IEnumerable<T> Repeat<T>(int count, Func<int, T> func)
    {
        for (var i = 0; i < count; i++) yield return func(i);
    }

    public static void Swap<T>(ref T x, ref T y)
    {
        var tmp = x; x = y; y = tmp;
    }
}

[DebuggerStepThrough]
public static class MathExtensions
{
    public static int DivCeil(int left, int right)
        => left / right + (left % right == 0 ? 0 : 1);

    public static long DivCeil(long left, long right)
        => left / right + (left % right == 0L ? 0L : 1L);

    public static int Gcd(int left, int right)
    {
        int r;
        while ((r = left % right) != 0) { left = right; right = r; }
        return right;
    }

    public static long Gcd(long left, long right)
    {
        long r;
        while ((r = left % right) != 0L) { left = right; right = r; }
        return right;
    }

    public static BigInteger Gcd(BigInteger left, BigInteger right)
        => BigInteger.GreatestCommonDivisor(left, right);

    public static int HighestOneBit(int x)
    {
        x |= x >> 01;
        x |= x >> 02;
        x |= x >> 04;
        x |= x >> 08;
        x |= x >> 16;
        return x - (x >> 1);
    }

    public static long HighestOneBit(long x)
    {
        x |= x >> 01;
        x |= x >> 02;
        x |= x >> 04;
        x |= x >> 08;
        x |= x >> 16;
        x |= x >> 32;
        return x - (x >> 1);
    }

    public static int Lcm(int left, int right) => left / Gcd(left, right) * right;

    public static long Lcm(long left, long right) => left / Gcd(left, right) * right;

    public static BigInteger Lcm(BigInteger left, BigInteger right)
        => left / Gcd(left, right) * right;

    public static int Pow(int value, int exponent)
    {
        var r = 1;
        while (exponent > 0)
        {
            if ((exponent & 1) == 1) r *= value;
            value *= value;
            exponent >>= 1;
        }
        return r;
    }

    public static long Pow(long value, int exponent)
    {
        var r = 1L;
        while (exponent > 0)
        {
            if ((exponent & 1) == 1) r *= value;
            value *= value;
            exponent >>= 1;
        }
        return r;
    }

    public static long Fact(int value)
    {
        var r = 1L;
        for (var i = 2; i <= value; i++) r *= i;
        return r;
    }
}
#endregion