Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
edges = {}
connections = {}
for _ in range(M):
    l, r, d = map(int, input().split())
    l -= 1
    r -= 1
    assert (l, r) not in edges
    assert (r, l) not in edges
    edges[l, r] = d

    if l not in connections:
        connections[l] = set()
    connections[l].add(r)


def solve():
    fixed = [None] * N
    for l, rs in connections.items():
        if fixed[l] is None:
            fixed[l] = 0
        for r in rs:
            if fixed[r] is not None and fixed[l] + edges[l, r] != fixed[r]:
                return False
            elif fixed[r] is None:
                fixed[r] = fixed[l] + edges[l, r]
    xmin = xmax = None
    for i in range(N):
        if fixed[i] is None:
            continue
        if xmin is None:
            xmin = xmax = fixed[i]
        else:
            xmin = min(xmin, fixed[i])
            xmax = max(xmax, fixed[i])
    if 10 ** 9 < xmax - xmin:
        return False
    return True


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

Fixed submission:
from collections import deque

N, M = map(int, input().split())
edges = {}
connections = {}
for _ in range(M):
    l, r, d = map(int, input().split())
    l -= 1
    r -= 1
    assert (l, r) not in edges
    assert (r, l) not in edges
    edges[l, r] = d

    if l not in connections:
        connections[l] = set()
    if r not in connections:
        connections[r] = set()
    connections[l].add(r)
    connections[r].add(l)


def solve():
    fixed = [None] * N
    undone = set(range(N))

    while undone:
        start = undone.pop()
        undone.add(start)
        queue = deque()
        queue.append(start)

        while queue:
            x = queue.popleft()
            if x not in undone:
                continue
            undone.remove(x)
            if x not in connections:
                continue

            for y in connections[x]:
                if y in undone:
                    queue.append(y)
                if (x, y) in edges:
                    if fixed[x] is None:
                        if fixed[y] is None:
                            fixed[x] = 0
                            fixed[y] = edges[x, y]
                        else:
                            fixed[x] = fixed[y] - edges[x, y]
                    else:
                        if fixed[y] is None:
                            fixed[y] = fixed[x] + edges[x, y]
                        else:
                            if fixed[x] + edges[x, y] != fixed[y]:
                                return False
                elif (y, x) in edges:
                    if fixed[y] is None:
                        if fixed[x] is None:
                            fixed[y] = 0
                            fixed[x] = edges[y, x]
                        else:
                            fixed[y] = fixed[x] - edges[y, x]
                    else:
                        if fixed[x] is None:
                            fixed[x] = fixed[y] + edges[y, x]
                        else:
                            if fixed[y] + edges[y, x] != fixed[x]:
                                return False

    xmin = xmax = None
    for i in range(N):
        if fixed[i] is None:
            continue
        if xmin is None:
            xmin = xmax = fixed[i]
        else:
            xmin = min(xmin, fixed[i])
            xmax = max(xmax, fixed[i])
    if xmax is not None and xmin is not None:
        if 10 ** 9 < xmax - xmin:
            return False
    return True


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