CodeEff/ECCO huggingface dataset:
# Original solution
def main():
    import sys
    input = sys.stdin.buffer.readline
    N, M = (int(i) for i in input().split())

    par = [i for i in range(N)]
    rank = [1]*N
    weight = [0]*N

    def find_root(x):
        if par[x] == x:
            return x
        else:
            r = find_root(par[x])
            weight[x] += weight[par[x]]
            par[x] = r
            return par[x]

    def get_weight(x):
        find_root(x)
        return weight[x]

    def get_diff(x, y):
        return get_weight(y) - get_weight(x)

    def is_same_group(x, y):
        return find_root(x) == find_root(y)

    def unite(x, y, w):
        w += get_weight(x)
        w -= get_weight(y)
        x = find_root(x)
        y = find_root(y)
        if x == y:
            return
        if rank[x] < rank[y]:
            x, y = y, x
            w = -w
        rank[x] += rank[y]
        par[y] = x
        weight[y] = w

    def size(x):
        # 連結成分のサイズを返す
        return rank[find_root(x)]

    for i in range(M):
        le, ri, d = (int(i) for i in input().split())
        le -= 1
        ri -= 1
        if is_same_group(le, ri):
            diff = get_diff(le, ri)
            if diff != d:
                return print("No")
        else:
            unite(le, ri, d)
    print("Yes")


if __name__ == '__main__':
    main()


# Optimized 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():
    import sys
    input = sys.stdin.buffer.readline
    N, M = (int(i) for i in input().split())
    uf = WeightedUnionFind(N)
    LRD = [[int(i) for i in input().split()] for j in range(M)]
    for le, ri, d in LRD:
        if uf.same(le, ri):
            if uf.diff(le, ri) != d:
                return print("No")
        uf.union(le, ri, d)
    print("Yes")


if __name__ == '__main__':
    main()
# Original solution
N, M = list(map(int, input().split()))
LRD = [list(map(int, input().split())) for _ in range(M)]

roots = [(i,0) for i in range(N+1)] #(root, distance_to_root)

def find_root(a, roots=roots):
  if roots[a][0] == a:
    return (a,0)
  else:
    r, d = find_root(roots[a][0])
    roots[a] = (r, roots[a][1] + d)
  return roots[a]

def unite(l, r, d, roots=roots):
  root_l, d_l = find_root(l)
  root_r, d_r = find_root(r)
  if root_l == root_r:
    if d_l == d + d_r:
      return True
    else:
      return False
  else:
    roots[root_l] = (r, d - d_l)
    return True

for l,r,d in LRD:
  if not unite(l,r,d):
    print('No')
    break
else:
  print('Yes')

# Optimized solution
N, M = list(map(int, input().split()))
LRD = [list(map(int, input().split())) for _ in range(M)]

roots = [(i,0) for i in range(N+1)] #(root, distance_from_i_to_root)

def find_root(a, roots=roots):
  if roots[a][0] == a:
    return (a,0)
  else:
    r, d = find_root(roots[a][0])
    roots[a] = (r, roots[a][1] + d)
  return roots[a]

def unite(l, r, d, roots=roots):
  root_l, d_l = find_root(l)
  root_r, d_r = find_root(r)
  if root_l == root_r:
    if d_l == d + d_r:
      return True
    else:
      return False
  else:
    roots[root_l] = (root_r, d + d_r - d_l)
    return True

for l,r,d in LRD:
  if not unite(l,r,d):
    print('No')
    break
else:
  print('Yes')
# Original solution
from collections import deque
n, m = list(map(int, input().split()))
graph = [[] for _ in range(n)]
for _ 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))
seen = [False]*n
dist = [None]*n
for i in range(n):
    if seen[i]:
        continue
    q = deque([i])
    dist[i] = 0
    while q:
        node = q.popleft()
        seen[node] = True
        for c_node, d in graph[node]:
            if dist[c_node] is None:
                dist[c_node] = dist[node] + d
            elif dist[c_node] != dist[node] + d:
                print('No')
                exit(0)
            if not seen[c_node]:
                q.append(c_node)
print('Yes')

# Optimized solution
from collections import deque
n, m = list(map(int, input().split()))
graph = [[] for _ in range(n)]
for _ 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))
seen = [False]*n
dist = [None]*n
for i in range(n):
    if seen[i]:
        continue
    q = deque([i])
    dist[i] = 0
    while q:
        node = q.popleft()
        if seen[node]:
            continue
        seen[node] = True
        for c_node, d in graph[node]:
            if dist[c_node] is None:
                dist[c_node] = dist[node] + d
            elif dist[c_node] != dist[node] + d:
                print('No')
                exit(0)
            if not seen[c_node]:
                q.append(c_node)
print('Yes')
# Original solution
# Union Find 木
from sys import setrecursionlimit


def find(parent, diff_weight, i):
    t = parent[i]
    if t < 0:
        return i
    t = find(parent, diff_weight, t)
    diff_weight[i] += diff_weight[parent[i]]
    parent[i] = t
    return t


def weight(parent, diff_weight, i):
    find(parent, diff_weight, i)
    return diff_weight[i]


def unite(parent, diff_weight, i, j, d):
    d -= weight(parent, diff_weight, j)
    d += weight(parent, diff_weight, i)
    i = find(parent, diff_weight, i)
    j = find(parent, diff_weight, j)
    if i == j:
        return
    diff_weight[j] = d
    parent[i] += parent[j]
    parent[j] = i


setrecursionlimit(10 ** 6)

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

parent = [-1] * (N + 1)
diff_weight = [0] * (N + 1)
for L, R, D in LRD:
    i = find(parent, diff_weight, L)
    j = find(parent, diff_weight, R)
    if i != j:
        unite(parent, diff_weight, L, R, D)
    else:
        if weight(parent, diff_weight, L) + D != weight(parent, diff_weight, R):
            print('No')
            exit()
print('Yes')


# Optimized solution
# Union Find 木
from sys import setrecursionlimit, stdin


def find(parent, diff_weight, i):
    t = parent[i]
    if t < 0:
        return i
    t = find(parent, diff_weight, t)
    diff_weight[i] += diff_weight[parent[i]]
    parent[i] = t
    return t


def weight(parent, diff_weight, i):
    find(parent, diff_weight, i)
    return diff_weight[i]


def unite(parent, diff_weight, i, j, d):
    d -= weight(parent, diff_weight, j)
    d += weight(parent, diff_weight, i)
    i = find(parent, diff_weight, i)
    j = find(parent, diff_weight, j)
    if i == j:
        return
    diff_weight[j] = d
    parent[i] += parent[j]
    parent[j] = i


setrecursionlimit(10 ** 6)

N, M = list(map(int, stdin.readline().split()))
LRD = [tuple(map(int, stdin.readline().split())) for _ in range(M)]

parent = [-1] * (N + 1)
diff_weight = [0] * (N + 1)
for L, R, D in LRD:
    i = find(parent, diff_weight, L)
    j = find(parent, diff_weight, R)
    if i != j:
        unite(parent, diff_weight, L, R, D)
    else:
        if weight(parent, diff_weight, L) + D != weight(parent, diff_weight, R):
            print('No')
            exit()
print('Yes')
# Original solution
# Union Find 木
from sys import setrecursionlimit, stdin


def find(parent, diff_weight, i):
    t = parent[i]
    if t < 0:
        return i
    t = find(parent, diff_weight, t)
    diff_weight[i] += diff_weight[parent[i]]
    parent[i] = t
    return t


def weight(parent, diff_weight, i):
    find(parent, diff_weight, i)
    return diff_weight[i]


def unite(parent, diff_weight, i, j, d):
    d -= weight(parent, diff_weight, j)
    d += weight(parent, diff_weight, i)
    i = find(parent, diff_weight, i)
    j = find(parent, diff_weight, j)
    if i == j:
        return
    diff_weight[j] = d
    parent[i] += parent[j]
    parent[j] = i


setrecursionlimit(10 ** 6)

N, M = list(map(int, stdin.readline().split()))
LRD = [tuple(map(int, stdin.readline().split())) for _ in range(M)]

parent = [-1] * (N + 1)
diff_weight = [0] * (N + 1)
for L, R, D in LRD:
    i = find(parent, diff_weight, L)
    j = find(parent, diff_weight, R)
    if i != j:
        unite(parent, diff_weight, L, R, D)
    else:
        if weight(parent, diff_weight, L) + D != weight(parent, diff_weight, R):
            print('No')
            exit()
print('Yes')


# Optimized solution
# 深さ優先探索
from sys import stdin

N, M = list(map(int, stdin.readline().split()))

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

t = [None] * (N + 1)
for i in range(1, N + 1):
    if t[i] is not None:
        continue
    t[i] = 0
    s = [i]
    while s:
        j = s.pop()
        for k, l in links[j]:
            if t[k] is None:
                t[k] = t[j] + l
                s.append(k)
            else:
                if t[k] != t[j] + l:
                    print('No')
                    exit()
print('Yes')
# Original solution
def main():    
    from collections import deque
    import sys
    input = sys.stdin.readline
    n,m = list(map(int,input().split()))
    e = [[] for i in range(n)]

    for i in range(m):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        e[a].append([b,c])
        e[b].append([a,-c])


    dis = [False]*n

    def search(x):

        q = deque([])
        dis[x] = 0
        q.append((x,0))
        while q:
            now,cnt = q.popleft()
            for nex,d in e[now]:
                if dis[nex] == False:
                    dis[nex] = cnt+d
                    q.append((nex,cnt+d))
                else:
                    if dis[nex] != cnt+d:
                        print("No")
                        exit()
            
    for i in range(n):
        if e[i] and dis[i] == False:
            search(i)
    print("Yes")

if __name__ == "__main__":
    main()

# Optimized solution
def main():    
    from collections import deque
    import sys
    input = sys.stdin.readline
    n,m = list(map(int,input().split()))
    e = [[] for i in range(n)]

    for i in range(m):
        a,b,c = list(map(int,input().split()))
        a -= 1
        b -= 1
        e[a].append([b,c])
        e[b].append([a,-c])


    dis = [float("INF")]*n

    def search(x):

        q = deque([])
        dis[x] = 0
        q.append(x)
        while q:
            now = q.popleft()
            for nex,d in e[now]:
                if dis[nex] == float("INF"):
                    dis[nex] = dis[now]+d
                    q.append(nex)
                else:
                    if dis[nex] != dis[now]+d:
                        print("No")
                        exit()
            
    for i in range(n):
        if e[i] and dis[i] == float("INF"):
            search(i)
    print("Yes")

if __name__ == "__main__":
    main()
# Original solution
from collections import defaultdict
import sys
input = sys.stdin.readline
inf = float('INF')

n, m = list(map(int, input().split()))
if m == 0:
    print('Yes')
    exit()
edges = defaultdict(list)

for i in range(m):
    left, right, dist = list(map(int, input().split()))
    edges[left-1].append([right-1, dist])
    edges[right-1].append([left-1, -dist])

seen = [inf] * n
for node in range(n):
    if seen[node] != inf:
        continue

    seen[node] = 0
    todo = []
    while edges[node]:
        to, dis = edges[node].pop()
        todo.append([to, dis])
    while todo:
        new, dis = todo.pop()
        if seen[new] == inf:
            seen[new] = dis
        elif seen[new] != dis:
            print('No')
            exit()

        while edges[new]:
            to, n_dis = edges[new].pop()
            todo.append([to, dis + n_dis])

print('Yes')


# Optimized solution
from collections import defaultdict
import sys
input = sys.stdin.readline


n, m = list(map(int, input().split()))
if m == 0:
    print('Yes')
    exit()
edges = defaultdict(list)

for i in range(m):
    left, right, dist = list(map(int, input().split()))
    edges[left-1].append((right-1, dist))
    edges[right-1].append((left-1, -dist))

seen = [None] * n
for node in range(n):
    if seen[node] is not None:
        continue

    seen[node] = 0
    todo = []
    while edges[node]:
        to, dis = edges[node].pop()
        seen[to] = dis
        todo.append([to, dis])

    while todo:
        new, dis = todo.pop()

        while edges[new]:
            to, n_dis = edges[new].pop()
            if seen[to] is None:
                seen[to] = dis + n_dis
            elif seen[to] != dis + n_dis:
                print('No')
                exit()
            todo.append([to, dis + n_dis])

print('Yes')
# Original solution
n, m = list(map(int, input().split()))
G = [[] for _ in range(n)]
indeg = [0] * n

for _ in range(m):
    l, r, d = list(map(int, input().split()))
    l -= 1
    r -= 1
    G[r].append((l, d))
    indeg[r] += 1

from collections import deque
ng = False
visited = [0] * n
for start, deg in enumerate(indeg):
    if deg == 0 or visited[start] == 1:
        continue
    st = deque([[start, 0]])
    visited_cost = [-1] * n
    while st:
        i, cost = st.pop()
        for ni, nd in G[i]:
            if visited_cost[ni] == -1:
                # まだ記録されていない
                visited[ni] = 1
                visited_cost[ni] = cost + nd
                st.append([ni, cost + nd])
            elif visited_cost[ni] == cost + nd:
                visited[ni] = 1
                continue
            else:
                # 既に記録されていて、矛盾している場合
                # 終了
                ng = True
                break
        if ng:
            break
    if ng:
        break

print(('No' if ng else 'Yes'))


# Optimized solution
n, m = list(map(int, input().split()))
G = [[] for _ in range(n)]
indeg = [0] * 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))

from collections import deque
ng = False
visited = [0] * n
visited_cost = [0] * n

for start in range(n):
    if visited[start]:
        continue
    visited[start] = 1
    st = deque([[start, 0]])
    while st:
        i, cost = st.pop()
        for ni, nd in G[i]:
            if visited[ni] == 0:
                visited[ni] = 1
                visited_cost[ni] = cost + nd
                st.append([ni, cost + nd])
            else:
                # 既に記録されていて、矛盾している場合
                if visited_cost[ni] != cost + nd:
                    # 終了
                    ng = True
                    break
        if ng:
            break
    if ng:
        break
print(('No' if ng else 'Yes'))
# Original solution
N,M = list(map(int,input().split()))
LRD = [(lambda x: [int(x[0])-1,int(x[1])-1,int(x[2])])(input().split()) for _ in [0]*M]

connect = [[] for _ in range(N)]
for L,R,D in LRD:
    connect[L].append([R,D])
    connect[R].append([L,-D])

ans = True
n = [None] * N
q = []
n0 = 0
while ans:
    n[n0] = 0
    q.append(n0)
    for i in q:
        for j,d in connect[i]:
            if n[j] == None:
                n[j] = n[i] + d
                q.append(j)
            elif n[j]-n[i] != d:
                ans = False
                break
        if not ans:
            break
    for i in range(n0,N):
        if n[i] == None:
            n0 = i
            break
    else:
        break

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

# Optimized solution
N,M = list(map(int,input().split()))
LRD = [(lambda x: [int(x[0])-1,int(x[1])-1,int(x[2])])(input().split()) for _ in [0]*M]

connect = [[] for _ in range(N)]
for L,R,D in LRD:
    connect[L].append([R,D])
    connect[R].append([L,-D])

ans = True
n = [None] * N
n0 = 0
while ans:
    q = [n0]
    n[n0] = 0
    for i in q:
        for j,d in connect[i]:
            if n[j] == None:
                n[j] = n[i] + d
                q.append(j)
            elif n[j]-n[i] != d:
                ans = False
                break
        if not ans:
            break
    for i in range(n0,N):
        if n[i] == None:
            n0 = i
            break
    else:
        break

if ans:
    print("Yes")
else:
    print("No")
# Original solution
N,M = list(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 = [-1]*N
breakFlg = False
for i in range(N):
    if V[i]!=-1:continue
    V[0] = 0
    q = [i]
    while q:
        j = q.pop()
        v = V[j]
        for k,d in list(E[j].items()):
            if V[k] == -1:
                V[k] = v+d
                q.append(k)
ans = True
for l,e in enumerate(E):
    for r,d in list(e.items()):
        ans = ans and V[r]-V[l]==d
    if not ans:break
print(("Yes" if ans else "No"))

# Optimized solution
N,M = list(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 list(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 list(e.items()):
        ans = ans and V[r]-V[l]==d
    if not ans:break
print(("Yes" if ans else "No"))
# Original solution
class UnionFind:
    def __init__(self, size):
        self.parent = list(range(size))
        self.height = [0] * size
        self.size = [1] * size
        self.component = size

    def root(self, index):
        if self.parent[index] == index:  # 根の場合
            return index
        rootIndex = self.root(self.parent[index])  # 葉の場合親の根を取得
        self.parent[index] = rootIndex  # 親の付け直し
        return rootIndex

    def union(self, index1, index2):  # 結合
        root1 = self.root(index1)
        root2 = self.root(index2)

        if root1 == root2:  # 連結されている場合
            return

        self.component -= 1  # 連結成分を減らす

        if self.height[root1] < self.height[root2]:
            self.parent[root1] = root2  # root2に結合
            self.size[root2] += self.size[root1]
        else:
            self.parent[root2] = root1  # root1に結合
            self.size[root1] += self.size[root2]
            if self.height[root1] == self.height[root2]:
                self.height[root1] += 1
        return

    def isSameRoot(self, index1, index2):
        return self.root(index1) == self.root(index2)

    def sizeOfSameRoot(self, index):
        return self.size[self.root(index)]

    def getComponent(self):
        return self.component

N, M = list(map(int, input().split()))
INF = 10**18
tree = UnionFind(N)

edges = [[] for _ in range(N)]
for _ in range(M):
    fr, to, d = list(map(int, input().split()))
    fr -= 1
    to -= 1
    edges[to].append((fr, d))
    edges[fr].append((to, -d))
    tree.union(fr, to)

minDist = [INF] * N
for start in range(N):
    if tree.root(start) != start:
        continue
    st = [(start, 0)]
    while st:
        now, dist = st.pop()

        if minDist[now] == dist:
            continue

        if minDist[now] != INF and minDist[now] != dist:
            print('No')
            exit()
        minDist[now] = dist

        for to, d in edges[now]:
            if minDist[to] == d + dist:
                continue
            st.append((to, dist + d))

print('Yes')


# Optimized solution
N, M = list(map(int, input().split()))
edges = [[] for _ in range(N)]

for _ in range(M):
    fr, to, dist = list(map(int, input().split()))
    fr -= 1
    to -= 1
    edges[fr].append((to, dist))
    edges[to].append((fr, -dist))

INF = 10**18
minDist = [INF] * N
for s in range(N):
    if minDist[s] < INF:
        continue
    st = [s]
    minDist[s] = 0
    while st:
        now = st.pop()
        dist = minDist[now]
        for to, d in edges[now]:
            if minDist[to] == INF:
                minDist[to] = dist + d
                st.append(to)
                continue
            if minDist[to] != dist + d:
                print('No')
                exit()
print('Yes')
# Original solution
N, M = list(map(int, input().split()))
edges = [[] for _ in range(N)]

for _ in range(M):
    fr, to, dist = list(map(int, input().split()))
    fr -= 1
    to -= 1
    edges[fr].append((to, dist))
    edges[to].append((fr, -dist))

INF = 10**18
minDist = [INF] * N
for s in range(N):
    if minDist[s] < INF:
        continue
    st = [s]
    minDist[s] = 0
    while st:
        now = st.pop()
        dist = minDist[now]
        for to, d in edges[now]:
            if minDist[to] == INF:
                minDist[to] = dist + d
                st.append(to)
                continue
            if minDist[to] != dist + d:
                print('No')
                exit()
print('Yes')


# Optimized solution
N, M = list(map(int, input().split()))
edges = [[] for _ in range(N)]

for _ in range(M):
    fr, to, dist = list(map(int, input().split()))
    fr -= 1
    to -= 1
    edges[fr].append((to, dist))
    edges[to].append((fr, -dist))

def isOk():
    visited = [False] * N
    INF = 10**18
    minDist = [INF] * N
    for s in range(N):
        if visited[s]:
            continue
        st = [(s, 0)]
        minDist[s] = 0
        visited[s] = True
        while st:
            now, dist = st.pop()

            for to, d in edges[now]:
                d += dist
                if visited[to]:
                    if minDist[to] != d:
                        return False
                else:
                    visited[to] = True
                    minDist[to] = d
                    st.append((to, d))
    return True

print(('Yes' if isOk() else 'No'))