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 (Python):
class WeightedUnionFind:
    def __init__(self, n):
        self.root = [-1]*(n+1)
        self.rnk = [0]*(n+1)
        self.weight = [0]*(n+1)
    def Find_Root(self, x):
        if self.root[x] < 0:
            return x
        else:
            y = self.Find_Root(self.root[x])
            self.weight[x] += self.weight[self.root[x]]
            self.root[x] = y
            return y
    def Weigh(self, x):
        self.Find_Root(x)
        return self.weight[x]
    def Diff(self, x, y):
        return self.Weigh(y) - self.Weigh(x)
    
    def Unite(self, x, y, w):
        w += self.Weigh(x) - self.Weigh(y)
        x = self.Find_Root(x)
        y = self.Find_Root(y)
        if x == y:
            if w==0:
                return
            else:
                print('No')
                exit()
        else:
            if self.rnk[x] < self.rnk[y]:
                x, y = y, x
                w = -w
            self.root[x] += self.root[y]
            self.root[y] = x
            self.weight[y] = w
            if self.rnk[x] == self.rnk[y]:
                self.rnk[x] += 1
    def isSameGroup(self, x, y):
        return self.Find_Root(x) == self.Find_Root(y)
    def Size(self, x):
        return -self.root[self.Find_Root(x)]

import sys
input = sys.stdin.readline
n,m=map(int,input().split())
l=[list(map(int,input().split())) for _ in range(m)]
uf=WeightedUnionFind(n)
for x in l:
    uf.Unite(x[0],x[1],x[2])
print('Yes')

Solution (Python):
from collections import deque

N,M = map(int,input().split())

RD = [deque() for i in range(N)]
end = [False] * N

for i in range(M):

    L,R,D = map(int,input().split())
    RD[L-1].append([R-1,D])
    RD[R-1].append([L-1,-1 * D])
    end[L-1] = True
    end[R-1] = True
    
"""
    lp = wantp(L-1)
    rp = wantp(R-1)

    if lp != rp:

        if rank[lp] 
"""
    


endnum = 0
Q = deque()

cor = ["x"] * N
ans = "Yes"


for endp in range(N):

    if end[endp]:
        Q.append(endp)


        while len(Q) > 0:

            q = Q.pop()
            end[q] = False

            if cor[q] == "x":
                cor[q] = 0

            for r,d in RD[q]:

                if cor[r] == "x":
                    cor[r] = cor[q] + d
                    Q.append(r)

                elif cor[r] != cor[q] + d:
                    print ("No")
                    exit()


print ("Yes")

Solution (Python):
def solve():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    INF=float('inf')
    n,m=map(int,input().split())
    l=[list(map(int,input().split())) for _ in range(m)]
    d=[INF]*n
    nbr=[[] for _ in range(n)]
    used=[False]*m
    for x in l:
        x[0]-=1
        x[1]-=1
    
    for i in range(m):
        nbr[l[i][0]].append(i)
        nbr[l[i][1]].append(i)
    
    def sol(i):
        for x in nbr[i]:
            if used[x]:continue
            used[x]=True
            i1=l[x][0]^l[x][1]^i
            dd=d[i]+l[x][2]*(2*(l[x][0]==i)-1)
            if d[i1]!=dd and d[i1]!=INF:
                print('No')
                exit()
            elif d[i1]==INF:
                d[i1]=dd
                sol(i1)
    for i in range(n):
        if d[i]==INF:
            d[i]=0
            sol(i)
    
    print('Yes')
solve()

Solution (Python):
import collections
def main():
    N, M = map(int, input().split())
    graph = collections.defaultdict(dict)
    for _ in range(M):
        L, R, D = map(int, input().split())
        graph[L][R] = D
        graph[R][L] = -D

    visited = set()
    used = set()
    for i in range(1, N+1):
        if i not in visited:
            dist = dict()
            dist[i] = 0
            cur = [i]
            visited.add(i)
            while cur:
                temp = []
                for j in cur:
                    for k in graph[j]:
                        if (j, k) not in used and (k, j) not in used:
                            used.add((j, k))
                            used.add((k, j))
                            if k not in dist:
                                dist[k] = dist[j] + graph[j][k]
                                temp.append(k)
                                visited.add(k)
                            elif k in dist:
                                if dist[k] != dist[j] + graph[j][k]:
                                    return "No"
                cur = temp

    return "Yes"

if __name__ == '__main__':
    print(main())

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    a, b, c = IR(3)
    a -= b
    print(a % c)
    return


def B():
    a, b, c, x, ans = II(), II(), II(), II(), 0
    for i in range(a + 1):
        for j in range(b + 1):
            for k in range(c + 1):
                if i * 500 + j * 100 + k * 50 == x:
                    ans += 1
    print(ans)
    return


def C():
    n = II()
    a = LI()
    dp = [[0] * (n + 1) for i in range(2)]
    for i in range(1, n + 1):
        dp[0][i] += dp[0][i - 1] + a[i - 1]
    a = LI()
    for i in range(1, n + 1):
        dp[1][i] = max(dp[0][i], dp[1][i - 1]) + a[i - 1]
    print(dp[1][-1])
    
    return


class Value_UnionFind():
    def __init__(self,n):
        self.par = [i for i in range(n)]
        self.differ_weight = [0] * n
        self.rank = [0] * n

    def root(self,x):
        if x == self.par[x]:
            return x
        r = self.root(self.par[x])
        self.differ_weight[x] += self.differ_weight[self.par[x]]
        self.par[x] = r
        return r

    def weight(self, x):
        self.root(x)
        return self.differ_weight[x]

    def unit(self, x, y, w):
        w += self.weight(x)
        w -= self.weight(y)
        x = self.root(x)
        y = self.root(y)
        if x == y: return False
        if self.rank[x] < self.rank[y]: x, y, w = y, x, -w
        if self.rank[x] == self.rank[y]: self.rank[x] += 1
        self.par[y] = x
        self.differ_weight[y] = w
        return True

    def differ(self, x, y):
        return self.weight(y) - self.weight(x)

def D():
    n, m = LI()
    u = Value_UnionFind(n)
    for _ in range(m):
        l, r, d = LI_()
        if not u.unit(l, r, d + 1):
            if u.differ(l, r) != d + 1:
                print("No")
                return
    print("Yes")


    return


if __name__ == '__main__':
    D()

Solution (Python):
from collections import deque

N, M = map(int, input().split())
G = [deque() for i in range(N+1)]
x = [0] * (N+1)
visited = [True] * (N+1)

for i in range(M):
    L, R, D = map(int, input().split())
    G[L].append((R, D))
    G[R].append((L, -D))
    visited[L] = False
    visited[R] = False
    
for i in range(1, N+1):
    if not visited[i]:
        q = G[i]
        visited[i] = True
        
        while q:
            u, d = q.pop()
            
            for v, d_ in G[u]:
                if visited[v]:
                    if x[v] != d+d_:
                        print('No')
                        exit()
                else:
                    visited[v] = True
                    x[v] = d+d_
                    q.append((v, d+d_))
print('Yes')

Solution (Python):
import sys
input = sys.stdin.readline
 
n, m = map(int, input().split())
 
def Find(x, par, diff_weight):
  if par[x] < 0:
    return x
  else:
    
    px = Find(par[x], par, diff_weight)
    diff_weight[x] += diff_weight[par[x]]
    par[x] = px
    return px
 
def Unite(x, y, par, rank, diff_weight, w):
  w += diff_weight[x] - diff_weight[y]
  x = Find(x, par, diff_weight)
  y = Find(y, par, diff_weight)
  
  if x != y:
    
    if rank[x] < rank[y]:
      x, y =  y, x
      w = -w
    if rank[x] == rank[y]:
        rank[x] += 1
    par[x] += par[y]
    par[y] = x
    diff_weight[y] = w
 
def Same(x, y, par, diff_weight):
  return Find(x, par,diff_weight) == Find(y, par, diff_weight)
 
def Size(x, par):
  return -par[Find(x)]
 
def Weight(x, par, diff_weight):
  Find(x, par, diff_weight)
  return diff_weight[x]
 
def Diff(x, y, par, diff_weight):
  return Weight(y, par, diff_weight)  - Weight(x, par, diff_weight)
 
par = [-1]*n
rank = [0]*n
diff_weight = [0]*n
for i in range(m):
  l, r, d = map(int, input().split())
  l, r = l-1 ,r-1
  if Same(l, r, par, diff_weight) == False:
    Unite(l, r, par, rank, diff_weight, d)
  else:
    if d != Diff(l, r, par, diff_weight):
      print('No')
      exit()
print('Yes')

Solution (Python):
import sys
input = sys.stdin.readline

n, m = map(int, input().split())

def Find(x, par, diff_weight):
  if par[x] < 0:
    return x
  else:
    
    px = Find(par[x], par, diff_weight)
    diff_weight[x] += diff_weight[par[x]]
    par[x] = px
    return px

def Unite(x, y, par, rank, diff_weight, w):
  w += diff_weight[x] - diff_weight[y]
  x = Find(x, par, diff_weight)
  y = Find(y, par, diff_weight)
  
  if x != y:
    
    if rank[x] < rank[y]:
      x, y =  y, x
      w = -w
    if rank[x] == rank[y]:
        rank[x] += 1
    par[x] += par[y]
    par[y] = x
    diff_weight[y] = w

def Same(x, y, par, diff_weight):
  return Find(x, par,diff_weight) == Find(y, par, diff_weight)
 
def Size(x, par):
  return -par[Find(x)]

def Weight(x, par, diff_weight):
  Find(x, par, diff_weight)
  return diff_weight[x]

def Diff(x, y, par, diff_weight):
  return Weight(y, par, diff_weight)  - Weight(x, par, diff_weight)

par = [-1]*n
rank = [0]*n
diff_weight = [0]*n
for i in range(m):
  l, r, d = map(int, input().split())
  l, r = l-1 ,r-1
  if Same(l, r, par, diff_weight) == False:
    Unite(l, r, par, rank, diff_weight, d)
  else:
    if d != Diff(l, r, par, diff_weight):
      print('No')
      exit()
print('Yes')

Solution (Python):
n,m=map(int,input().split())
a=[[]for i in range(n+1)]
b=[""]*(n+1)
for i in range(m):
    l,r,d=map(int,input().split())
    a[l].append([r,d])
    a[r].append([l,-d])
for i in range(1,n+1):
    if b[i]=="":
        b[i]=0
        k=[i]
        while k:
            s=k.pop()
            for x,y in a[s]:
                if b[x]=="":
                    k.append(x)
                    b[x]=b[s]+y
c="Yes"
for i in range(1,n+1):
    for p,q in a[i]:
        if b[i]+q!=b[p]:
            c="No"
print(c)

Solution (Python):
"""
https://atcoder.jp/contests/abc087/tasks/arc090_b

xR-xL = D
Mこの情報が正しいかどうかの判定
グループを作って、グループ内の拘束条件があっているか確認
重み付きunionfindを使う
https://qiita.com/drken/items/cce6fc5c579051e64fab
http://at274.hatenablog.com/entry/2018/02/03/140504


5 5
1 2 2
2 3 3
4 5 4
2 5 10
3 4 3
"""

from collections import defaultdict
class UnionFind:
    def __init__(self, n):
        class KeyDict(dict):
            
            def __missing__(self,key):
                self[key] = key
                return key
        self.parent = KeyDict()
        self.rank = defaultdict(int)
        self.weight = defaultdict(int)

    
    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            
            
            y = self.find(self.parent[x])           
            self.weight[x] += self.weight[self.parent[x]]   
            self.parent[x] = y      
            return self.parent[x]

    
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
        
        if self.rank[x] == self.rank[y]:
            self.rank[x] += 1

    
    def weighted_union(self, x, y, w):
        
        px = self.find(x)
        py = self.find(y)
        
        
        if self.rank[px] < self.rank[py]:
            self.parent[px] = py
            self.weight[px] = - w - self.weight[x] + self.weight[y]
        else:
            self.parent[py] = px
            self.weight[py] =  w + self.weight[x] - self.weight[y]
        if self.rank[px] == self.rank[py]:
            self.rank[px] += 1
        return 0


    
    def judge(self, x, y):
        return self.find(x) == self.find(y)


N,M = map(int, input().split())
uf = UnionFind(N)
for i in range(N):
    uf.find(i)

for i in range(M):
    l,r,d = map(int, input().split())
    
    if not uf.judge(l, r):
        uf.weighted_union(l, r, d)
    
    else:
        if d == uf.weight[r] - uf.weight[l]:
            continue
        else:
            print("No")
            exit()
print("Yes")

Solution (Python):
"""
https://atcoder.jp/contests/abc087/tasks/arc090_b

xR-xL = D
Mこの情報が正しいかどうかの判定
グループを作って、グループ内の拘束条件があっているか確認
重み付きunionfindを使う
https://qiita.com/drken/items/cce6fc5c579051e64fab
http://at274.hatenablog.com/entry/2018/02/03/140504


5 5
1 2 2
2 3 3
4 5 4
2 5 10
3 4 3
"""

from collections import defaultdict
class UnionFind:
    def __init__(self, n):
        class KeyDict(dict):
            
            def __missing__(self,key):
                self[key] = key
                return key
        self.parent = KeyDict()
        self.rank = defaultdict(int)
        self.weight = defaultdict(int)

    
    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            
            
            y = self.find(self.parent[x])           
            self.weight[x] += self.weight[self.parent[x]]   
            self.parent[x] = y      
            return self.parent[x]

    
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
        
        if self.rank[x] == self.rank[y]:
            self.rank[x] += 1

    
    
    

    
    

    def weighted_union(self, x, y, w):
        
        px = self.find(x)
        py = self.find(y)
        
        
        if self.rank[px] < self.rank[py]:
            self.parent[px] = py
            self.weight[px] = w - self.weight[x] + self.weight[y]
        else:
            self.parent[py] = px
            self.weight[py] = - w + self.weight[x] - self.weight[y]
        if self.rank[px] == self.rank[py]:
            self.rank[px] += 1
        return 0


    
    def judge(self, x, y):
        return self.find(x) == self.find(y)


N,M = map(int, input().split())
uf = UnionFind(N)
for i in range(N):
    uf.find(i)

for i in range(M):
    l,r,d = map(int, input().split())
    
    if not uf.judge(l, r):
        uf.weighted_union(l, r, d)
    
    else:
        if d == -uf.weight[r]+uf.weight[l]:
            continue
        else:
            print("No")
            exit()
print("Yes")

Solution (Python):
N,M = map(int,input().split())
LRD = [list(map(int,input().split())) for _ in [0]*M]

E = [{} for _ in [0]*N]
for L,R,D in LRD:
    E[L-1][R-1] = D
    E[R-1][L-1] = -D

V = [None]*N
breakFlg = False
for i in range(N):
    if V[i]!=None:
      	continue
    V[i] = 0
    q = [i]
    while q:
        j = q.pop()
        v = V[j]
        for k,d in E[j].items():
            if V[k] == None:
                V[k] = v+d
                q.append(k)
ans = True
for l,e in enumerate(E):
    for r,d in e.items():
        ans = ans and V[r]-V[l]==d
    if not ans:
      	break
print("Yes" if ans else "No")

Solution (Python):
import sys
sys.setrecursionlimit(300000)

N, M = map(int, input().split())

nl = [[] for _ in range(N + 1)]
for _ in range(M):
    L, R, D = map(int, input().split())
    nl[L].append((R, D))
    nl[R].append((L, -D))


    
seen = [None for _ in range(N + 1)]

def dfs(v, d):
    
    seen[v] = d
    for next_v, D in nl[v]:
        next_d = d + D
        if seen[next_v] is None:
            dfs(next_v, next_d)
        else:
            if seen[next_v] != next_d:
                print('No')
                exit(0)
                
for v in range(1, N + 1):  
    if seen[v] is None:
        dfs(v, 0)

print('Yes')

Solution (Python):




import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

def root(x):
    if x == par[x]:
        return par[x]
    y = root(par[x])
    wei[x] += wei[par[x]]
    par[x] = y
    return y

def union(x,y,w):
    rx = root(x)
    ry = root(y)
    if rank[rx] < rank[ry]:
        par[rx] = ry
        wei[rx] = w - wei[x] + wei[y]
    else:
        par[ry] = rx
        wei[ry] = -w - wei[y] + wei[x]
        if rank[rx] == rank[ry]:
            rank[rx] += 1


n,m = LI()
par = [i for i in range(n)]
rank = [0]*n
wei = [0]*n
for i in range(m):
    l,r,w = LI()
    if root(l-1) != root(r-1):
        union(l-1,r-1,w)
    else:
        if wei[l-1] - wei[r-1] != w:
            print('No')
            quit()
print('Yes')

Solution (Python):
import sys
sys.setrecursionlimit(300000)

N, M = map(int, input().split())
nl = [[] for _ in range(N + 1)]
for _ in range(M):
    L, R, D = map(int, input().split())
    nl[L].append((R, D))
    nl[R].append((L, -D))

seen = [None for _ in range(N + 1)]


def dfs(v, d):
    seen[v] = d
    for next_v, D in nl[v]:
        next_d = d + D
        if seen[next_v] is None:
            dfs(next_v, next_d)
        else:
            if seen[next_v] != next_d:
                print('No')
                exit(0)


for v in range(1, N + 1):  
    if seen[v] is None:
        dfs(v, 0)

print('Yes')

Solution (Python):


def find(x):
    if par[x] < 0:
        return x
    else:
        px = find(par[x])
        wei[x] += wei[par[x]]
        par[x] = px
        return px


def weight(x):
    find(x)
    return wei[x]



def unite(x,y,w):
    w += wei[x]-wei[y]
    x = find(x)
    y = find(y)
    
    if x == y:
        return False
    else:
        
        if par[x] > par[y]:
            x,y = y,x
            w = -w
        par[x] += par[y]
        par[y] = x
        wei[y] = w
        return True


def same(x,y):
    return find(x) == find(y)


def size(x):
    return -par[find(x)]


def diff(x,y):
    return weight(y)-weight(x)

n,m = map(int,input().split())




par = [-1]*(n+1)
wei = [0]*(n+1)

ans=True
for i in range(m):
    p=[int(j) for j in input().split()]
    if same(p[0],p[1]):
        if diff(p[0],p[1])!=p[2]:
            ans=False
    else:
        unite(p[0],p[1],p[2])

if ans:
    print("Yes")
else:
    print("No")

Solution (Python):
from collections import defaultdict, deque
N, M = map(int, input().split())

diff_dic = defaultdict(lambda: [])
for _ in range(M):
    L, R, M = map(int, input().split())
    diff_dic[L-1].append([R-1, M])
    diff_dic[R-1].append([L-1, -M])

INF = 10 ** 10



ans_array = [INF] * N

for i in range(N):
    if ans_array[i] != INF:
        continue
    people_deque = deque()
    people_deque.append(i)
    if ans_array[i] == INF:
        ans_array[i] = 0
    while(len(people_deque)):
        
        person = people_deque.popleft()

        for v in diff_dic[person]:
            p, m = v
            if ans_array[p] == INF:
                ans_array[p] = ans_array[person] + m
                people_deque.append(p)
            elif ans_array[p] != (ans_array[person] + m):
                print("No")
                exit()
print("Yes")

Solution (Python):
from collections import defaultdict, deque
N, M = map(int, input().split())

diff_dic = defaultdict(lambda: [])
for _ in range(M):
    L, R, M = map(int, input().split())
    diff_dic[L-1].append([R-1, M])
    diff_dic[R-1].append([L-1, -M])

INF = 10 ** 10



ans_array = [INF] * N

for i in range(N):
    if ans_array[i] != INF:
        continue
    people_deque = deque()
    people_deque.append(i)
    if ans_array[i] == INF:
        ans_array[i] = 0
    while(len(people_deque)):
        
        person = people_deque.popleft()

        for v in diff_dic[person]:
            p, m = v
            if ans_array[p] == INF:
                ans_array[p] = ans_array[person] + m
                people_deque.append(p)
            elif ans_array[p] != (ans_array[person] + m):
                print("No")
                exit()
print("Yes")

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    n,m = LI()
    a = LIR(n)
    ans = 0
    for i in range(2):
        for j in range(2):
            for k in range(2):
                a.sort(key = lambda x:(-1)**i*x[0]+(-1)**j*x[1]+(-1)**k*x[2])
                p = [0,0,0]
                for l in range(m):
                    p[0] += a[l][0]
                    p[1] += a[l][1]
                    p[2] += a[l][2]
                p = abs(p[0])+abs(p[1])+abs(p[2])
                if p > ans:
                    ans = p
    print(ans)
    return


def B():
    def root(x):
        if x == par[x]:
            return x
        par[x] = root(par[x])
        return par[x]

    def unite(x,y,su):
        x = root(x)
        y = root(y)
        su += comb_2(s[x])+comb_2(s[y])
        if rank[x] < rank[y]:
            s[y] += s[x]
            su -= comb_2(s[y])
            par[x] = y
        else:
            s[x] += s[y]
            su -= comb_2(s[x])
            par[y] = x
            if rank[x] == rank[y]:
                rank[x] += 1
        return su

    def comb_2(a):
        return a*(a-1)//2

    n,m = LI()
    v = LIR(m)
    par = [i for i in range(n)]
    rank = [0]*n
    s = [1]*n
    su = comb_2(n)
    ans = [su]
    for a,b in v[:0:-1]:
        a -= 1
        b -= 1
        if root(a) != root(b):
            su = unite(a,b,su)
        ans.append(su)
    for i in ans[::-1]:
        print(i)
    return


def C():
    def root(x):
        if par[x] == x:
            return par[x]
        r = root(par[x])
        d[x] += d[par[x]]
        par[x] = r
        return par[x]

    def weight(x):
        root(x)
        return d[x]

    def unite(x,y,w):
        w += weight(x)
        w -= weight(y)
        x = root(x)
        y = root(y)
        if rank[x] < rank[y]:
            par[x] = y
            d[x] = -w
        else:
            par[y] = x
            d[y] = w
            if rank[x] == rank[y]:
                rank[x] += 1
    n,m = LI()
    par = [i for i in range(n)]
    rank = [0]*n
    d = [0]*n
    for i in range(m):
        l,r,w = LI()
        l -= 1
        r -= 1
        if root(l) != root(r):
            unite(l,r,w)
        else:
            if weight(r)-weight(l) != w:
                print("No")
                return
    print("Yes")
    return


def D():

    return


def E():

    return


def F():

    return


def G():

    return


def H():

    return


def I_():

    return


if __name__ == "__main__":
    C()

Solution (Python):
from collections import deque

N,M = map(int,input().split())
node = [[]for i in range(N)]
for i in range(M):
    L,R,D = map(int,input().split())
    node[L-1].append([R-1,D])
    node[R-1].append([L-1,-D])
Flag = [False]*N
queue = deque([])
X = [0]*N
for i in range(N):
    if Flag[i] == False:
        Flag[i] = True
        queue.append(i)
        
        while queue != deque([]):
            p = queue.popleft()
            for r,d in node[p]:
                if Flag[r] == False:
                    Flag[r] = True
                    queue.append(r)
                    X[r] = X[p] - d
                elif X[r] != X[p] - d:
                    print('No')
                    exit()
print('Yes')

Solution (Python):


def solve():
    N, M = list(map(int, input().split()))
    MAX = 10**9
    L, R, D = [], [], []
    for i in range(M):
        _in = list(map(int, input().split()))
        L.append(_in[0])
        R.append(_in[1])
        D.append(_in[2])

    E = [{} for _ in range(N)]
    for i in range(M):
        E[L[i]-1][R[i]-1] = D[i]  
        E[R[i]-1][L[i]-1] = -D[i] 
    
    V = [None] * N

    for i in range(N):
        if V[i] is not None:
            continue
        V[i] = 0
        q = [i]
        while q:
            j = q.pop()
            v = V[j]
            for k, d in E[j].items():
                if V[k] is None:
                    V[k] = v+d
                    q.append(k)
    ans = True
    for l, e in enumerate(E):
        for r, d in e.items():
            ans = ans and V[r]-V[l] == d
        if not ans:
            break
    print("Yes" if ans else "No")
        

if __name__ == "__main__":
    solve()

Solution (Python):
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque

from itertools import product

from itertools import permutations

from itertools import combinations

from itertools import accumulate
from bisect import bisect_left, bisect_right

import re



import sys
sys.setrecursionlimit(10**9)

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
def line_inpl(x): return [i_inpl() for _ in range(x)]

INF = int(1e18)
MOD = int(1e9)+7 


def create_grid(H, W, value = 0):
    return [[ value for _ in range(W)] for _ in range(H)]



def main():
    N, M = l_inpl()
 
    G = [[] * (N+1) for _ in range(N+1) ]
 
    for _ in range(M):
        Li, Ri, Di = l_inpl()
        Li, Ri = Li - 1, Ri - 1
        G[Li].append((Ri, Di))
        G[Ri].append((Li, -Di))
 
    
    x = [INF] * (N+1)
 
    
    def dfs(s, cost):
        
        if x[s] == INF:
            x[s] = cost
            
            for t, d in G[s]:
                if dfs(t, cost + d) == False:
                    return False
        return x[s] == cost
 
    for i in range(N):
        
        
        if x[i] == INF and dfs(i, 0) == False:
            print("No")
            return
    print("Yes")
 
if __name__ == "__main__":
    main()

Solution (Python):
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque

from itertools import product

from itertools import permutations

from itertools import combinations

from itertools import accumulate
from bisect import bisect_left, bisect_right

import re



import sys
sys.setrecursionlimit(10**9)

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
def line_inpl(x): return [i_inpl() for _ in range(x)]

INF = int(1e18)
MOD = int(1e9)+7 


def create_grid(H, W, value = 0):
    return [[ value for _ in range(W)] for _ in range(H)]



def main():
    N, M = l_inpl()

    G = [[] for _ in range(N+1)]

    for _ in range(M):
        Li, Ri, Di = l_inpl()
        Li, Ri = Li - 1, Ri - 1
        G[Li].append((Ri, Di))
        G[Ri].append((Li, -Di))

    
    x = [INF] * (N+1)

    
    def dfs(s, cost):
        
        if x[s] == INF:
            x[s] = cost
            
            for t, d in G[s]:
                if dfs(t, cost + d) == False:
                    return False
        return x[s] == cost

    for i in range(N):
        
        
        if x[i] == INF and dfs(i, 0) == False:
            print("No")
            return
    print("Yes")

if __name__ == "__main__":
    main()

Solution (Python):
import sys
sys.setrecursionlimit(200002)


def dfs(s):
    global x, G
    for t, d in G[s]:
        if x[t] is None:
            x[t] = x[s] + d
            if not dfs(t):
                return False
        else:
            if x[t] != x[s] + d:
                return False
    return True


n, m = map(int, input().split())
x = [None] * n
G = [[] for _ in range(n)]
for _ in range(m):
    l, r, d = map(int, input().split())
    G[l-1].append([r-1, d])
    G[r-1].append([l-1, -d])
res = True
for i in range(n):
    if x[i] is None:
        x[i] = 0
        res &= dfs(i)
print('Yes' if res==True else 'No')

Solution (Python):
N,M = map(int,input().split())
LRD = [list(map(int,input().split())) for _ in [0]*M]

E = [{} for _ in [0]*N]
for L,R,D in LRD:
    E[L-1][R-1] = D
    E[R-1][L-1] = -D

V = [None]*N
breakFlg = False
for i in range(N):
    if V[i]!=None:continue
    V[i] = 0
    q = [i]
    while q:
        j = q.pop()
        v = V[j]
        for k,d in E[j].items():
            if V[k] == None:
                V[k] = v+d
                q.append(k)
ans = True
for l,e in enumerate(E):
    for r,d in e.items():
        ans = ans and V[r]-V[l]==d
    if not ans:break
print("Yes" if ans else "No")