CodeEff/ECCO huggingface dataset:
# Original solution
from queue import Queue
import sys

inf = float("inf")
N, M = list(map(int, input().split()))
to = [[] for x in range(N+1)]
cost = [[] for x in range(N+1)]
value = [inf for x in range(N+1)]

for i in range(M):
    l, r, d = list(map(int, input().split()))
    to[l].append(r)
    cost[l].append(d)
    to[r].append(l)
    cost[r].append(-d)

for i in range(1, N+1):
    if value[i] != inf: continue
    
    value[i] = 0
    q = Queue()
    q.put(i)
    while not q.empty():
        tmp = q.get()
        for j in range(len(to[tmp])):
            nxt = to[tmp][j]
            nxt_val = value[tmp] + cost[tmp][j]
            if value[nxt] != inf:
                if value[nxt] == nxt_val: continue
                print("No")
                sys.exit()
            value[nxt] = nxt_val
            q.put(nxt)
print("Yes")

# Optimized solution
from queue import Queue
import sys

inf = float("inf")
N, M = list(map(int, input().split()))
dist = [[] for x in range(N+1)]
value = [inf for x in range(N+1)]

for i in range(M):
    l, r, d = list(map(int, input().split()))
    dist[l].append((r, d))
    dist[r].append((l, -d))

for i in range(1, N+1):
    if value[i] != inf: continue
    
    value[i] = 0
    q = Queue()
    q.put(i)
    while not q.empty():
        tmp = q.get()
        for x in dist[tmp]:
            nxt = x[0]
            nxt_val = value[tmp] + x[1]
            if value[nxt] != inf:
                if value[nxt] == nxt_val: continue
                print("No")
                sys.exit()
            value[nxt] = nxt_val
            q.put(nxt)
print("Yes")
# Original solution
n, m = list(map(int, input().split()))
lrd = []

MAX = 999999

p = [[] for _ in range(n)]
for i in range(m):
    l, r, d = list(map(int, input().split()))
    lrd.append([l-1, r-1, d, 0])
    p[l-1].append(i)
    p[r-1].append(i)

if m <= 1:
    print('Yes')
    exit()

a = [MAX for _ in range(n)]

a[lrd[0][0]] = 0
temp = [lrd[0][0]]
next = []
t = 0
s = 0
max_a = 0
min_a = 0
while 1:
    next = []
    for x in temp:
        for y in p[x]:
            if lrd[y][0] == x:
                t = lrd[y][1]
                s = a[x] + lrd[y][2]
            else:
                t = lrd[y][0]
                s = a[x] - lrd[y][2]

            lrd[y][3] = 1
            if a[t] == MAX:
                a[t] = s
                next.append(t)
            elif a[t] != s:
                print('No')
                exit()

    if len(next) == 0:
        target = 0
        i = 0
        check = 0
        for i in range(m):
            if lrd[i][3] == 0:
                target = lrd[i][0]
                check = 1
        if check == 0:
            break
        a[target] = 0
        next = [target]

    temp = [] + list(set(next))

print('Yes')


# Optimized solution
n, m = list(map(int, input().split()))
lrd = []

MAX = 999999

p = [[] for _ in range(n)]
for i in range(m):
    l, r, d = list(map(int, input().split()))
    lrd.append([l-1, r-1, d, 0])
    p[l-1].append(i)
    p[r-1].append(i)

if m <= 1:
    print('Yes')
    exit()

a = [MAX for _ in range(n)]


target = lrd[0][0]
a[target] = 0
temp = [target]
t = 0
while 1:
    next = []
    for x in temp:
        for y in p[x]:
            if lrd[y][3] == 1:
                continue

            if lrd[y][0] == x:
                t = lrd[y][1]
                d = a[x] + lrd[y][2]
            else:
                t = lrd[y][0]
                d = a[x] - lrd[y][2]

            lrd[y][3] = 1

            if a[t] == MAX:
                a[t] = d
                next.append(t)
            elif a[t] != d:
                print('No')
                exit()

    if len(next) == 0:
        check = 0
        for i in range(m):
            if lrd[i][3] == 0:
                check = 1
                target = lrd[i][0]
                break

        if check == 1:
            temp = [target]
            a[target] = 0
            continue
        else:
            break

    temp = [] + list(set(next))

print('Yes')
# Original solution
def main():
    n, m = list(map(int, input().split()))
    g = [[] for i in range(n)]
    for i in range(m):
        l, r, d = list(map(int, input().split()))
        g[l - 1].append((r - 1, d))
        g[r - 1].append((l - 1, -d))
    dis = [0] * n
    visited = [False] * n
    stack = list(range(n))
    while len(stack):
        v = stack[-1]
        stack.pop()
        if visited[v]:
            continue
        visited[v] = True
        for u, d in g[v]:
            if not visited[u]:
                dis[u] = dis[v] + d
                stack.append(u)
    for v in range(n):
        for u, d in g[v]:
            if dis[v] + d != dis[u]:
                print('No')
                return
    print('Yes')


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    n, m = list(map(int, input().split()))
    g = [[] for i in range(n)]
    for i in range(m):
        l, r, d = list(map(int, input().split()))
        g[l - 1].append((r - 1, d))
        g[r - 1].append((l - 1, -d))
    dis = [0] * n
    visited = [False] * n
    for i in range(n):
        if visited[i]:
            continue
        stack = list([i])
        visited[i] = True
        while len(stack):
            v = stack[-1]
            stack.pop()
            for u, d in g[v]:
                if not visited[u]:
                    dis[u] = dis[v] + d
                    stack.append(u)
                    visited[u] = True
    for v in range(n):
        for u, d in g[v]:
            if dis[v] + d != dis[u]:
                print('No')
                return
    print('Yes')


if __name__ == '__main__':
    main()
# Original solution
import sys


def main():
    n, m = list(map(int, sys.stdin.readline().split()))
    g = [[] for i in range(n)]
    for i in range(m):
        l, r, d = list(map(int, sys.stdin.readline().split()))
        g[l - 1].append((r - 1, d))
        g[r - 1].append((l - 1, -d))
    dis = [0] * n
    visited = [False] * n
    for i in range(n):
        if visited[i]:
            continue
        stack = list([i])
        visited[i] = True
        while len(stack):
            v = stack[-1]
            stack.pop()
            for u, d in g[v]:
                if not visited[u]:
                    dis[u] = dis[v] + d
                    stack.append(u)
                    visited[u] = True
    for v in range(n):
        for u, d in g[v]:
            if dis[v] + d != dis[u]:
                print('No')
                return
    print('Yes')


if __name__ == '__main__':
    main()


# Optimized solution
import sys


def main():
    n, m = list(map(int, sys.stdin.readline().split()))
    g = [[] for i in range(n)]
    deg = [0] * n
    lines = sys.stdin.read().splitlines()
    for line in lines:
        l, r, d = list(map(int, line.split()))
        g[l - 1].append((r - 1, d))
        deg[r - 1] += 1
    dis = [-1] * n
    for i in range(n):
        if deg[i]:
            continue
        stack = list([i])
        dis[i] = 0
        while len(stack):
            v = stack[-1]
            stack.pop()
            for u, d in g[v]:
                if dis[u] == -1:
                    dis[u] = dis[v] + d
                    stack.append(u)
    for v in range(n):
        for u, d in g[v]:
            if dis[v] + d != dis[u]:
                print('No')
                return
    print('Yes')


if __name__ == '__main__':
    main()
# Original solution
import sys


def main():
    n, m = list(map(int, sys.stdin.readline().split()))
    g = [[] for i in range(n)]
    deg = [0] * n
    lines = sys.stdin.read().splitlines()
    for line in lines:
        l, r, d = list(map(int, line.split()))
        g[l - 1].append((r - 1, d))
        deg[r - 1] += 1
    dis = [-1] * n
    for i in range(n):
        if deg[i]:
            continue
        stack = list([i])
        dis[i] = 0
        while len(stack):
            v = stack[-1]
            stack.pop()
            for u, d in g[v]:
                if dis[u] == -1:
                    dis[u] = dis[v] + d
                    stack.append(u)
    for v in range(n):
        for u, d in g[v]:
            if dis[v] + d != dis[u]:
                print('No')
                return
    print('Yes')


if __name__ == '__main__':
    main()


# Optimized solution
import sys


def main():
    n, m = list(map(int, sys.stdin.readline().split()))
    g = [[] for i in range(n)]
    deg = [0] * n
    for i in range(m):
        l, r, d = list(map(int, sys.stdin.readline().split()))
        g[l - 1].append((r - 1, d))
        deg[r - 1] += 1
    dis = [-1] * n
    for i in range(n):
        if deg[i]:
            continue
        stack = list([i])
        dis[i] = 0
        while len(stack):
            v = stack[-1]
            stack.pop()
            for u, d in g[v]:
                if dis[u] == -1:
                    dis[u] = dis[v] + d
                    stack.append(u)
    for v in range(n):
        for u, d in g[v]:
            if dis[v] + d != dis[u]:
                print('No')
                return
    print('Yes')


if __name__ == '__main__':
    main()
# Original solution

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

num = [0] * N
far = [0] * N
RD = []
tps = []
p = []

for i in range(N):
    RD.append([])
    p.append(set([i]))

for i in range(M):

    L,R,D = list(map(int,input().split()))

    num[R-1] += 1
    RD[L-1].append([R-1,D])

Q = []

for i in range(N):
    if num[i] == 0:
        Q.append(i)

while len(Q) > 0:

    q = Q.pop(0)
    num[q] -= 1
    tps.append(q)

    for rd in RD[q]:

        r = rd[0]
        num[r] -= 1
        if num[r] == 0:
            Q.append(r)


flag = True
for i in range(N):

    if num[i] > 0:
        flag = False

maxfar = 0
#print (flag)


if flag:

    for i in tps:

        for ra in RD[i]:

            if len(p[ra[0]] & p[i]) > 0 and far[ra[0]] != far[i] + ra[1]:
                flag = False

            p[ra[0]] = p[ra[0]] | p[i]
            far[ra[0]] = max(far[ra[0]],far[i] + ra[1])
            maxfar = max(maxfar,far[ra[0]])

            

#print (far)
if flag and maxfar <= 10 ** 9:
    print ("Yes")
else:
    print ("No")
    




# Optimized solution


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

point = ["x"] * N
lis = []
notend = set([])

for i in range(N):

    lis.append([])
    notend = notend | set([i])


for i in range(M):

    L,R,D = list(map(int,input().split()))

    lis[L-1].append([R-1,D])
    lis[R-1].append([L-1,-1 * D])

ans = "Yes"
Q = []

while True:

    if len(notend) > 0:
        Q.append(notend.pop())
    else:
        break

    nmin = 0
    nmax = 0

    while len(Q) > 0:

        q = Q.pop(0)
        if point[q] == "x":
            point[q] = 0

        for rd in lis[q]:

            #ind = bisect.bisect_left(notend,rd[0])

            #print (ind,notend,rd[0])

            if rd[0] in notend:
                notend = notend - set([rd[0]])
                point[rd[0]] = point[q] + rd[1]
                nmin = min(nmin,point[rd[0]])
                nmax = max(nmin,point[rd[0]])
                Q.append(rd[0])

            elif point[rd[0]] != point[q] + rd[1]:
                ans = "No"
                break

        if ans == "No":
            break

    if nmax - nmin > 10 ** 9:
        ans = "No"

    if  ans == "No":
        break

print (ans)
# Original solution
import heapq 
n, m = list(map(int, input().split()))
if m == 0:
    print('Yes')
    exit()
lrd = [list(map(int, input().split())) for _ in range(m)]
lrd.sort()
di = {i:'?' for i in range(n+1)}
di[lrd[0][0]] = 0

while lrd:
    l, r, d = lrd.pop(0)
    if di[l] == '?' and di[r] == '?':
        lrd += [[l, r, d]]
    elif di[l] != '?' and di[r] == '?':
        di[r] = di[l] + d  
    elif di[l] == '?' and di[r] != '?':
        di[l] = di[r] - d 
    else:
        if di[l] + d != di[r]:
            print('No')
            exit()

print('Yes')

# Optimized solution

import sys
sys.setrecursionlimit(10**5)
class UnionFind():
    def __init__(self, n):
        self.n = 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:
            r = self.Find_Root(self.root[x])
            self.weight[x] += self.weight[self.root[x]]
            self.root[x] = r
            return r
    def GetWeight(self, x):
        self.Find_Root(x)
        return self.weight[x]
    def Unite(self, x, y, w):
        w += self.GetWeight(x)
        w -= self.GetWeight(y)
        x = self.Find_Root(x)
        y = self.Find_Root(y)
        if(x == y):
            return False
        if self.rnk[x] < self.rnk[y]:
            x, y = y, x
            w = -w
        if self.rnk[x] == self.rnk[y]:
            self.rnk[x] += 1
        self.root[y] = x
        self.weight[y] = w
        return True
    def isSameGroup(self, x, y):
        return self.Find_Root(x) == self.Find_Root(y)
    def Count(self, x):
        return -self.root[self.Find_Root(x)]
    def Diff(self, x, y):
        return self.GetWeight(y) - self.GetWeight(x)

n, m = list(map(int, input().split()))
if m == 0:
    print('Yes')
    exit()
lrd = [list(map(int, input().split())) for _ in range(m)]
uf = UnionFind(n)
for l, r, d in lrd:
    if uf.isSameGroup(l, r)==False:
        uf.Unite(l, r, d)
    else:
        if uf.Diff(l, r) != d:
            print('No')
            exit()
print('Yes')
# Original solution

import sys
sys.setrecursionlimit(10**5)
class UnionFind():
    def __init__(self, n):
        self.n = 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:
            r = self.Find_Root(self.root[x])
            self.weight[x] += self.weight[self.root[x]]
            self.root[x] = r
            return r
    def GetWeight(self, x):
        self.Find_Root(x)
        return self.weight[x]
    def Unite(self, x, y, w):
        w += self.GetWeight(x)
        w -= self.GetWeight(y)
        x = self.Find_Root(x)
        y = self.Find_Root(y)
        if(x == y):
            return False
        if self.rnk[x] < self.rnk[y]:
            x, y = y, x
            w = -w
        if self.rnk[x] == self.rnk[y]:
            self.rnk[x] += 1
        self.root[y] = x
        self.weight[y] = w
        return True
    def isSameGroup(self, x, y):
        return self.Find_Root(x) == self.Find_Root(y)
    def Count(self, x):
        return -self.root[self.Find_Root(x)]
    def Diff(self, x, y):
        return self.GetWeight(y) - self.GetWeight(x)

n, m = list(map(int, input().split()))
if m == 0:
    print('Yes')
    exit()
lrd = [list(map(int, input().split())) for _ in range(m)]
uf = UnionFind(n)
for l, r, d in lrd:
    if uf.isSameGroup(l, r)==False:
        uf.Unite(l, r, d)
    else:
        if uf.Diff(l, r) != d:
            print('No')
            exit()
print('Yes')





# Optimized solution
import sys
sys.setrecursionlimit(10**5)
class UnionFind():
    def __init__(self, n):
        self.n = 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:
            r = self.Find_Root(self.root[x])
            self.weight[x] += self.weight[self.root[x]]
            self.root[x] = r
            return r
    def GetWeight(self, x):
        self.Find_Root(x)
        return self.weight[x]
    def Unite(self, x, y, w):
        w += self.GetWeight(x)
        w -= self.GetWeight(y)
        x = self.Find_Root(x)
        y = self.Find_Root(y)
        if(x == y):
            return False
        if self.rnk[x] < self.rnk[y]:
            x, y = y, x
            w = -w
        if self.rnk[x] == self.rnk[y]:
            self.rnk[x] += 1
        self.root[y] = x
        self.weight[y] = w
        return True
    def isSameGroup(self, x, y):
        return self.Find_Root(x) == self.Find_Root(y)
    def Count(self, x):
        return -self.root[self.Find_Root(x)]
    def Diff(self, x, y):
        return self.GetWeight(y) - self.GetWeight(x)

N, M = list(map(int, input().split()))
uf = UnionFind(N+1)
for _ in range(M):
    l, r, d = list(map(int, input().split()))
    if not uf.isSameGroup(l, r):
        uf.Unite(l, r, d)
    else:
        if uf.Diff(l, r) != d:
            print('No')
            exit()

print('Yes')
# Original solution
I=lambda:list(map(int,input().split()))
n,m=I()
r=list(range(n))
y=[[] for i in r]
z=[None for i in r]

for _ in [0]*m:
  i,j,k=I()
  y[i-1]+=[[j-1, k]]
  y[j-1]+=[[i-1,-k]]

for i in r:
  if z[i]==None:
    z[i]=0
    q=[i]
    while q:
      p=q.pop()
      for j,k in y[p]:
        if z[j]==None:
          q+=[j]
          z[j]=z[p]+k
        elif z[j]!=z[p]+k:
          print("No")
          exit()
print("Yes")


# Optimized solution
I=lambda:list(map(int,input().split()))
n,m=I()
r=list(range(n))
y=[[] for i in r]
z=[None for i in r]

for _ in [0]*m:
  i,j,k=I()
  y[i-1]+=[[j-1, k]]
  y[j-1]+=[[i-1,-k]]

for i in r:
  if z[i]==None:
    z[i]=0 
  q=[i]
  while q:
    p=q.pop()
    for j,k in y[p]:
      if z[j]==None:
        q+=[j]
        z[j]=z[p]+k
      elif z[j]!=z[p]+k:
        print("No")
        exit()
print("Yes")
# Original solution
#http://at274.hatenablog.com/entry/2018/02/03/140504
#重み付きUnion-Find木

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]

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

for _ in range(M):
    L,R,D = list(map(int,input().split()))
    L -= 1; R -= 1
    if uf.find(L) == uf.find(R):
        if uf.diff(L,R) != D:
            print("No")
            break
    else:
        uf.union(L,R,D)
else:
    print("Yes")


# Optimized solution
#http://at274.hatenablog.com/entry/2018/02/03/140504
#重み付きUnion-Find木

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]

import sys
input = sys.stdin.readline  #文字列入力では注意！

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

for _ in range(M):
    L,R,D = list(map(int,input().split()))
    L -= 1; R -= 1
    if uf.find(L) == uf.find(R):
        if uf.diff(L,R) != D:
            print("No")
            break
    else:
        uf.union(L,R,D)
else:
    print("Yes")
# Original solution
from collections import deque

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

graph = [[] for i in range(N)]

for i in range(M):
    L,R,D = list(map(int,input().split()))
    graph[L-1].append([R-1,D])
    graph[R-1].append([L-1,-D])

#print(graph)
x = [None for i in range(N)]

def f():
    for i in range(N):
        if x[i] == None:
            x[i] = 0
            q = deque([i])
            while len(q):
                j = q.popleft()
                for k,d in graph[j]:
                    if x[k] == None:
                        q.append(k)
                        x[k] = x[j]+d
                    elif x[k] != x[j]+d:
                        return False
                        
    return True

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

# Optimized solution
from collections import deque

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

graph = [[] for i in range(N)]

for i in range(M):
    L,R,D = list(map(int,input().split()))
    graph[L-1].append([R-1,D])
    graph[R-1].append([L-1,-D])

x = [None for i in range(N)]

def f():
    for i in range(N):
        if x[i] == None:
            x[i] = 0
            q = deque([i])
            while len(q):
                j = q.popleft()
                for k,d in graph[j]:
                    if x[k] == None:
                        q.append(k)
                        x[k] = x[j]+d
                    elif x[k] != x[j]+d:
                        return False
                        
    return True

if f():
    print("Yes")
else:
    print("No")
# Original solution
import sys
sys.setrecursionlimit(10**5+5)
n,m=list(map(int,input().split()))
graph=[[] for i in range(n)]
for i in range(m):
    l,r,d=list(map(int,input().split()))
    graph[r-1].append((l-1,-d))
    graph[l-1].append((r-1,d))
INF=10**9
x_list=[INF]*n
def dfs(x):
    for i in graph[x]:
        if x_list[i[0]]==INF:
            x_list[i[0]]=x_list[x]+i[1]
            dfs(i[0])
        else:
            if x_list[i[0]]==x_list[x]+i[1]:
                pass
            else:
                print('No')
                sys.exit()
for i in range(n):
    if x_list[i]==INF:
        x_list[i]=0
        dfs(i)
print('Yes')


# Optimized solution
import sys
n,m=list(map(int,input().split()))
graph=[[] for i in range(n)]
for i in range(m):
    l,r,d=list(map(int,input().split()))
    graph[r-1].append((l-1,-d))
    graph[l-1].append((r-1,d))
INF=10**9
x_list=[INF]*n
stack=[]
for i in range(n):
    if x_list[i]==INF:
        x_list[i]=0
        stack.append(i)
        while len(stack)>0:
            now=stack.pop()
            for j in graph[now]:
                if x_list[j[0]]==INF:
                    x_list[j[0]]=x_list[now]+j[1]
                    stack.append(j[0])
                else:
                    if x_list[j[0]]==x_list[now]+j[1]:
                        pass
                    else:
                        print('No')
                        sys.exit()
print('Yes')