CodeEff/ECCO huggingface dataset:
# Original solution
n,m=list(map(int,input().split()))
lrd=[list(map(int,input().split())) for _ in range(m)]
g=[[] for _ in range(n)]
for l,r,d in lrd:
  g[l-1].append([d,r-1])
  g[r-1].append([-d,l-1])

from collections import deque
no_seen=set(range(n))
inf=float('inf')
dst=[inf]*n
while no_seen:
  todo=deque([[0,no_seen.pop()]])
  #print(todo)
  while todo:
    d,t=todo.popleft()
    no_seen.discard(t)
    dst[t]=d
    l=g[t]
    for di,ti in l:
      if dst[ti]!=inf and dst[ti]!=d+di:
        print('No')
        exit()
      elif dst[ti]!=inf and dst[ti]==d+di:
        pass
      else:
        todo.append([d+di,ti])

print('Yes')

# Optimized solution
n,m=list(map(int,input().split()))
lrd=[list(map(int,input().split())) for _ in range(m)]
g=[[] for _ in range(n)]
for l,r,d in lrd:
  g[l-1].append([d,r-1])
  g[r-1].append([-d,l-1])

no_seen=set(range(n))
inf=float('inf')
dst=[inf]*n
while no_seen:
  todo=[[0,no_seen.pop()]]
  #print(todo)
  while todo:
    d,t=todo.pop()
    no_seen.discard(t)
    l=g[t]
    for di,ti in l:
      if dst[ti]!=inf and dst[ti]!=d+di:
        print('No')
        exit()
      elif dst[ti]!=inf and dst[ti]==d+di:
        pass
      else:
        todo.append([d+di,ti])
        dst[ti]=d+di

print('Yes')
# Original solution
import sys


class UnionFind():
    def __init__(self, n):
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x


class WeightedUnionFind():
    def __init__(self, n):
        self.parents = [-1] * n
        self.par_weight = [0] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            root = self.find(self.parents[x])
            self.par_weight[x] += self.par_weight[self.parents[x]]
            self.parents[x] = root
            return self.parents[x]

    def union(self, x, y, w):
        w = w + self.weight(x) - self.weight(y)
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            self.parents[y] += self.parents[x]
            self.parents[x] = y
            self.par_weight[x] = -w
        else:
            self.parents[x] += self.parents[y]
            self.parents[y] = x
            self.par_weight[y] = w

    def weight(self, x):
        if self.parents[x] < 0:
            return 0
        else:
            return self.par_weight[x] + self.weight(self.parents[x])

    def diff(self, x, y):
        if self.find(x) != self.find(y):
            raise Exception('"{}" belongs to a different tree from "{}"'.format(x, y))
        return self.weight(y) - self.weight(x)


N, M = list(map(int, input().split()))
uf = WeightedUnionFind(N)

ans = 'No'
for q in sys.stdin.readlines():
    L, R, D = list(map(int, q.split()))
    L -= 1; R -= 1

    if uf.find(L) == uf.find(R):
        if uf.diff(L, R) != D:
            break
    else:
        uf.union(L, R, D)
else:
    ans = 'Yes'

print(ans)


# Optimized solution
import sys
sys.setrecursionlimit(10 ** 6)


def dfs(p):
    result = True
    for np, D in edge[p]:
        if pos[np] is None:
            pos[np] = pos[p] + D
            result &= dfs(np)
        else:
            if pos[p] + D != pos[np]:
                result &= False
    return result


N, M = list(map(int, input().split()))
edge = [[] for _ in range(N)]
for q in sys.stdin.readlines():
    L, R, D = list(map(int, q.split()))
    L -= 1; R -= 1
    edge[L].append((R, D))
    edge[R].append((L, -D))

pos = [None] * N
result = True
for i in range(N):
    if pos[i] is None:
        pos[i] = 0
        result &= dfs(i)

ans = 'Yes' if result else 'No'
print(ans)
# Original solution
import sys
input = sys.stdin.readline

def main():
  n, m = list(map(int, input().split()))
  F = [[] for _ in range(n)]
  lrd = [list(map(int, input().split())) for _ in range(m)]
  for _ in range(2):
    for i in range(m):
      l, r, d = lrd[i]
      l -= 1
      r -= 1
      for nr, nd in F[l]:
        if r == nr:
          if d != nd:
            print("No")
            exit()
        else:
          F[nr].append((r, d-nd))
          F[r].append((nr, nd-d))
      F[l].append((r, d))
      for nl, nd in F[r]:
        if l == nl:
          if -d != nd:
            print("No")
            exit()
        else:
          F[l].append((nl, nd+d))
          F[nl].append((l, -nd-d))
      F[r].append((l, -d))
  print("Yes")
  
if __name__ == "__main__":
  main()

# Optimized solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

n, m = list(map(int, input().split()))
G = [[] for _ in range(n)]
for _ in range(m):
  l, r, d = list(map(int, input().split()))
  l -= 1
  r -= 1
  G[l].append((r, d))
  G[r].append((l, -d))
dist = [float("inf")]*n
def dfs(v):
  for nv, d in G[v]:
    if dist[nv] == float("inf"):
      dist[nv] = dist[v]+d
      if not dfs(nv):
        return False
    else:
      if dist[nv] != dist[v]+d:
        return False
  return True
for i in range(n):
  if dist[i] == float("inf"):
    dist[i] = 0
    if not dfs(i):
      print("No")
      break
else:
  print("Yes")
# Original solution
"""from collections import *
from itertools import *
from bisect import *
from heapq import *

import math
from fractions import gcd"""

N,M=list(map(int,input().split()))
LRD=[list(map(int,input().split())) for i in range(M)]

data=[[] for i in range(N+1)]
for m,(l,r,d) in enumerate(LRD):
    data[l].append([r,d,m])
    data[r].append([l,-d,m])
data2=[set([m[1] for m in data[i]]) for i in range(N+1)]
Mlst=set([i for i in range(M)])

visited=set()
X=[0 for i in range(N+1)]
while Mlst:
    stack=[LRD[Mlst.pop()][0]]
    while stack:
        l=stack.pop()
        visited.add(l)
        for r,d,m in data[l]:
            if not r in visited:
                visited.add(r)
                X[r]=X[l]+d
                stack.append(r)
                if m in Mlst:
                    Mlst.remove(m)
            else:
                if X[r]!=X[l]+d:
                    print("No")
                    exit()

print("Yes")


# Optimized solution
"""from collections import *
from itertools import *
from bisect import *
from heapq import *

import math
from fractions import gcd"""

N,M=list(map(int,input().split()))
LRD=[list(map(int,input().split())) for i in range(M)]

data=[[] for i in range(N+1)]
for m,(l,r,d) in enumerate(LRD):
    data[l].append([r,d,m])
    data[r].append([l,-d,m])
data2=[set([m[1] for m in data[i]]) for i in range(N+1)]
Nlst=set([i for i in range(1,N)])
Mlst=set([i for i in range(M)])
visited=set()
X=[0 for i in range(N+1)]
while Mlst:
    stack=[Nlst.pop()]
    while stack:
        l=stack.pop()
        visited.add(l)
        for r,d,m in data[l]:
            if m in Mlst:
                Mlst.remove(m)
                if not r in visited:
                    visited.add(r)
                    X[r]=X[l]+d
                    stack.append(r)

                else:
                    if X[r]!=X[l]+d:
                        print("No")
                        exit()

print("Yes")
# Original solution
class WeightedUnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        # 根への距離を管理
        self.weight = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            y = self.find(self.par[x])
            # 親への重みを追加しながら根まで走査
            self.weight[x] += self.weight[self.par[x]]
            self.par[x] = y
            return y

    # 併合
    def union(self, x, y, w):
        rx = self.find(x)
        ry = self.find(y)
        # xの木の高さ < yの木の高さ
        if self.rank[rx] < self.rank[ry]:
            self.par[rx] = ry
            self.weight[rx] = w - self.weight[x] + self.weight[y]
        # xの木の高さ ≧ yの木の高さ
        else:
            self.par[ry] = rx
            self.weight[ry] = -w - self.weight[y] + self.weight[x]
            # 木の高さが同じだった場合の処理
            if self.rank[rx] == self.rank[ry]:
                self.rank[rx] += 1

    # 同じ集合に属するか
    def same(self, x, y):
        return self.find(x) == self.find(y)

    # xからyへのコスト
    def diff(self, x, y):
        return self.weight[x] - self.weight[y]

def main():
    max_v, max_e = list(map(int, input().split()))
    uf = WeightedUnionFind(max_v)
    k = []
    s = 0
    for i in range(max_e):
        a,b,w= list(map(int, input().split()))
        a-=1
        b-=1
        if uf.same(a,b):
            if w != uf.diff(a,b):
                print("No")
                break
        else:
            uf.union(a,b,w)
    else:
        print("Yes")
if __name__ == '__main__':
    main()
 

# Optimized solution
def main():
    import sys
    input = sys.stdin.readline

    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 = list(map(int,input().split()))


    par = [-1]*n
    wei = [0]*n

    for i in range(m):
        l,r,d = list(map(int,input().split()))
        l,r = l-1,r-1
        if same(l,r):
            if d != diff(l,r):
                print('No')
                exit()
        else:
            unite(l,r,d)
    print('Yes')
            
if __name__ == '__main__':
    main()
# Original solution
import sys
from heapq import heapify,heappop,heappush,heapreplace

read=sys.stdin.read
readline=sys.stdin.readline

def main():
  n,m,*lrd=list(map(int,read().split()))
  hp=[]
  for l,r,d in zip(*[iter(lrd)]*3):
    if l>r:
      hp.append((r,l,-d))
    else:
      hp.append((l,r,d))
  heapify(hp)
  if not hp:
    print('Yes')
    return
  while hp:
    ce=heappop(hp)
    while hp:
      ne=hp[0]
      if ce[0]==ne[0]:
        if ce[1]==ne[1]:
          if ce[2]!=ne[2]:
            print('No')
            return
          else:
            heappop(hp)
        else:
          heapreplace(hp,(ce[1],ne[1],ne[2]-ce[2]))
      else:
        break      
  print('Yes')

if __name__=='__main__':
  main()
      
    
    

# Optimized solution
import sys
sys.setrecursionlimit(10**9)

read=sys.stdin.read
class Unionfind():
    def __init__(self,n):
        self.parents=[-1]*n
        self.dist=[0]*n 
    
    def find(self,x):
        if self.parents[x]<0:
            return self.dist[x],x
        else:
            tmp=self.find(self.parents[x])
            self.dist[x]+=tmp[0]
            self.parents[x]=tmp[1]
        return self.dist[x], self.parents[x]
    
    def union(self,x,y,d):
        rx=self.find(x)[1]
        ry=self.find(y)[1]
        diff=self.dist[y]-self.dist[x]-d
        if rx==ry:
            if diff!=0:
                return True
            return False
        if diff<0:
            rx,ry=ry,rx
            diff=-diff
        self.parents[ry]=min(self.parents[ry],self.parents[rx]-diff)
        self.parents[rx]=ry
        self.dist[rx]=diff
        return False
    
def main():
    n,m,*lrd=list(map(int,read().split()))
    v=Unionfind(n)
    for l,r,d in zip(*[iter(lrd)]*3):
        if v.union(l-1,r-1,d):
            print('No')
            break
    else:
        if max(-d-1 for d in v.parents if d<0) >10**9:
            print('No')
        else:
            print('Yes')
if __name__=='__main__':
    main()
# Original solution
import sys
sys.setrecursionlimit(10**9)

read=sys.stdin.read
class Unionfind():
    def __init__(self,n):
        self.parents=[-1]*n
        self.dist=[0]*n 
    
    def find(self,x):
        if self.parents[x]<0:
            return x,self.dist[x]
        else:
            tmp=self.find(self.parents[x])
            self.parents[x]=tmp[0]
            self.dist[x]+=tmp[1]
            return self.parents[x],self.dist[x]
    
    def union(self,x,y,d):
        rx=self.find(x)[0]
        ry=self.find(y)[0]
        diff=self.dist[y]-self.dist[x]-d
        if rx==ry:
            if diff!=0:
                return True
            return False
        if diff<0:
            rx,ry=ry,rx
            diff=-diff
        self.parents[ry]=min(self.parents[ry],self.parents[rx]-diff)
        self.parents[rx]=ry
        self.dist[rx]=diff
        return False
    
def main():
    n,m,*lrd=list(map(int,read().split()))
    v=Unionfind(n)
    for l,r,d in zip(*[iter(lrd)]*3):
        if v.union(l-1,r-1,d):
            print('No')
            break
    else:
        print('Yes')
if __name__=='__main__':
    main()


# Optimized solution
import sys
sys.setrecursionlimit(10**9)

read=sys.stdin.read
class Unionfind():
    def __init__(self,n):
        self.parents=[-1]*n
        self.dist=[0]*n 
    
    def find(self,x):
        if self.parents[x]<0:
            return self.dist[x],x
        else:
            tmp=self.find(self.parents[x])
            self.dist[x]+=tmp[0]
            self.parents[x]=tmp[1]
        return self.dist[x], self.parents[x]
    
    def union(self,x,y,d):
        _,rx=self.find(x)
        _,ry=self.find(y)
        diff=self.dist[y]-self.dist[x]-d
        if rx==ry:
            if diff!=0:
                return True
            return False
        if diff<0:
            rx,ry=ry,rx
            diff=-diff
        self.parents[ry]=min(self.parents[ry],self.parents[rx]-diff)
        self.parents[rx]=ry
        self.dist[rx]=diff
        return False
    
def main():
    n,m,*lrd=list(map(int,read().split()))
    v=Unionfind(n)
    for l,r,d in zip(*[iter(lrd)]*3):
        if v.union(l-1,r-1,d):
            print('No')
            break
    else:
        if max(-d-1 for d in v.parents if d<0) >10**9:
            print('No')
        else:
            print('Yes')
if __name__=='__main__':
    main()
# Original solution
import sys
sys.setrecursionlimit(10**5)

n, m = [int(i) for i in input().split(" ")]

par = [i for i in range(0, n + 1)]
d2p = [0 for i in range(0, n + 1)]

def root(x):
    global par
    global d2p
    if par[x] == x:
        return (x, 0)
    else:
        r = root(par[x])
        par[x] = r[0]
        d2p[x] += r[1]
        #print(par, d2p)
        return (r[0], d2p[x])


def unite(x, y, d):
    global par
    global d2p
    #print(x, y, d)
    rx, dx = root(x)
    ry, dy = root(y)
    if rx != ry:
        par[rx] = ry
        d2p[rx] = dy + d - dx
        #print(par, d2p,"noz")
        return True
    else:
        if dx - dy != d:
            #print(rx, ry, dx, dy)
            return False
        else:
            return True

l = [0] * m
r = [0] * m
d = [0] * m

for i in range(0, m):
    l[i], r[i], d[i] = [int(j) for j in input().split(" ")]

f = True
for i in range(0, m):
    if not unite(l[i], r[i], d[i]):
        f = False
        break

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

# Optimized solution
import sys
sys.setrecursionlimit(10**5)

n, m = [int(i) for i in input().split(" ")]

par = [i for i in range(0, n + 1)]
d2p = [0 for i in range(0, n + 1)]

def d2r(x):
    global par
    global d2p
    k = x
    s = 0
    while k != par[k]:
        s += d2p[k]
        k = par[k]
    return s

def root(x):
    global par
    global d2p
    #print(par, d2p)
    if par[x] == x:
        return x
    else:
        r = root(par[x])
        s = d2r(x)
        par[x] = r
        d2p[x] = s
        return r


def unite(x, y, d):
    global par
    global d2p
    #print(x, y, d)
    rx = root(x)
    dx = d2r(x)
    ry = root(y)
    dy = d2r(y)
    if rx != ry:
        par[rx] = ry
        d2p[rx] = dy + d - dx
        #print(par, d2p,"noz")
        return True
    else:
        if dx - dy != d:
            #print(rx, ry, dx, dy)
            return False
        else:
            return True

l = [0] * m
r = [0] * m
d = [0] * m

for i in range(0, m):
    l[i], r[i], d[i] = [int(j) for j in input().split(" ")]

f = True
for i in range(0, m):
    if not unite(l[i], r[i], d[i]):
        f = False
        break

if f:
    print("Yes")
else:
    print("No")
# Original solution
import sys
import queue
input = sys.stdin.readline
INF=float('inf')

def solve():
    n,m=list(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 i in range(m):
        nbr[l[i][0]-1].append([l[i][0]-1,l[i][1]-1,l[i][2],i])
        nbr[l[i][1]-1].append([l[i][1]-1,l[i][0]-1,-l[i][2],i])
    #print(nbr)
    for i in range(n):
        if d[i]==INF:
            d[i]=0
            q=queue.Queue()
            for x in nbr[i]:
                q.put(x)
            while not q.empty():
                eg=q.get()
                used[eg[3]]=True
                if d[eg[1]]==d[eg[0]]+eg[2]:
                    pass
                elif d[eg[1]]==INF:
                    d[eg[1]]=d[eg[0]]+eg[2]
                else:
                    print('No')
                    exit()
                for x in nbr[eg[1]]:
                    if not used[x[3]]:
                        q.put(x)
    #print(d)
    print('Yes')
solve()

# Optimized solution
def solve():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    INF=float('inf')
    n,m=list(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
    #print(l)
    for i in range(m):
        nbr[l[i][0]].append(i)
        nbr[l[i][1]].append(i)
    #print(nbr)
    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(d)
    print('Yes')
solve()
# Original solution
N, M = list(map(int, input().split()))
LRD = [tuple(map(int, input().split())) for i in range(M)]


class unionFind:
    parent = []

    def __init__(self, N):
        self.parent = [i for i in range(N)]
        self.weight = [0]*N

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

    def same(self, x, y):
        x, y = x-1, y-1
        return(self.root(x) == self.root(y))

    def unite(self, x, y, w):
        w -= self.getWeight(x)
        w += self.getWeight(y)
        x, y = x-1, y-1
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        else:
            self.parent[x] = y
            self.weight[x] = self.weight[y]+w
            return

    def getWeight(self, x):
        x -= 1
        self.root(x)
        return(self.weight[x])


G = unionFind(N)
flag = True

for l, r, d in LRD:
    if G.same(l, r) == False:
        G.unite(l, r, d)
    else:
        if G.getWeight(l) - G.getWeight(r) == d:
            continue
        else:
            flag = False
            break

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


# Optimized solution
class unionFind:
    parent = []

    def __init__(self, N):
        self.parent = [i for i in range(N)]
        self.weight = [0]*N

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

    def same(self, x, y):
        x, y = x-1, y-1
        return(self.root(x) == self.root(y))

    def unite(self, x, y, w):
        w -= self.getWeight(x)
        w += self.getWeight(y)
        x, y = x-1, y-1
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        else:
            self.parent[x] = y
            self.weight[x] = self.weight[y]+w
            return

    def getWeight(self, x):
        x -= 1
        self.root(x)
        return(self.weight[x])


def main():
    import sys
    input = sys.stdin.readline

    N, M = list(map(int, input().split()))
    G = unionFind(N)
    for i in range(M):
        L, R, D = list(map(int, input().split()))
        if G.same(L, R):
            x = G.getWeight(L)
            y = G.getWeight(R)
            if x == y + D:
                continue
            else:
                print("No")
                exit(0)
        else:
            G.unite(L, R, D)
    print("Yes")


main()
# Original solution
def sub(a, b, n):
 r = a[:]
 for i in range(len(b)):
  r[i] -= b[i] * n
 return r

def mult(a, n):
 r = a[:]
 for i in range(len(a)):
  r[i] *= n
 return r

def pm(matr):
 return ""

def rref(matr):
 if len(matr) == 0 or len(matr[0]) == 0:
  return matr
 pivotCt = 0
 for d in range(max(len(matr), len(matr[0])-1)):
  #scan for first pivot
  pivot = None
  for i in range(len(matr)):
   allZeroes = True
   for j in range(d):
    if matr[i][j] != 0:
     allZeroes = False
     break
   if matr[i][d] != 0 and allZeroes:
    pivot = mult(matr[i], 1/matr[i][d])
    del matr[i]
    matr.insert(pivotCt, pivot)
    break
  if pivot != None:
   for i in range(len(matr)):
    if i != pivotCt and matr[i][d] != 0:
     matr[i] = sub(matr[i], pivot, matr[i][d])
   pivotCt += 1
 return matr

s = input().split(" ")
vs = int(s[0])
es = int(s[1])

matr = []
for i in range(es):
 matr.append([0] * (vs + 1))
 q = input().split(" ")
 x = int(q[0])
 y = int(q[1])
 z = int(q[2])
 matr[i][x] = 1
 matr[i][y] = -1
 matr[i][-1] = z

matr = rref(matr)
bad = False
for i in range(len(matr)):
 allZeroes = True
 for j in range(len(matr[0])-1):
  if matr[i][j] != 0:
   allZeroes = False
   break
 if allZeroes and matr[i][-1] != 0:
   bad = True
   break

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

# Optimized solution
n, m = list(map(int, input().split()))
inf = 10**10
G = [{} for i in range(n)]
for i in range(m):
    l, r, d = list(map(int, input().split()))
    G[l-1][r-1] = d
    G[r-1][l-1] = -d
V = [False for i in range(n)]
D = [inf for i in range(n)]
for i in range(n):
    if V[i]:
        continue
    Q = [i]
    D[i] = 0
    while Q:
        j = Q.pop()
        V[j] = True
        for k in list(G[j].keys()):
            if not V[k]:
                Q.append(k)
            if D[k] == inf:
                D[k] = D[j]+G[j][k]
            elif D[k] != D[j]+G[j][k]:
                print('No')
                exit()
print('Yes')