Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(65536)

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

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

for _ in range(M):
	l,r,d=map(int, input().split())
	path[l].append((r,+d))
	path[r].append((l,-d))

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

def walk(i, c):
	if cost[i] is None:
		cost[i] = c
		for j,d in path[i]:
			if not walk(j, c+d):
				return False
	return cost[i] == c

ans = "Yes"

for i in range(1, N+1):
	if cost[i] is None and not walk(i, 0):
		ans = "No"
		break

print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(1145141919)

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

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

for _ in range(M):
	l,r,d=map(int, input().split())
	path[l].append((r,+d))
	path[r].append((l,-d))

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

def walk(i, c):
	if cost[i] is None:
		cost[i] = c
		for j,d in path[i]:
			if not walk(j, c+d):
				return False
	return cost[i] == c

ans = "Yes"

for i in range(1, N+1):
	if cost[i] is None and not walk(i, 0):
		ans = "No"
		break

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(65536)

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

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

for _ in range(M):
	l,r,d=map(int, input().split())
	path[l].append((r,+d))
	path[r].append((l,-d))

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

def dfs(i, c):
	if cost[i] is None:
		cost[i] = c
		for j,d in path[i]:
			if not dfs(j, c+d):
				return False
	return cost[i] == c

ans = "Yes"

for i in range(1, N+1):
	if cost[i] is None and not dfs(i, 0):
		ans = "No"
		break

print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(1145141919)

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

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

for _ in range(M):
	l,r,d=map(int, input().split())
	path[l].append((r,+d))
	path[r].append((l,-d))

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

def walk(i, c):
	if cost[i] is None:
		cost[i] = c
		for j,d in path[i]:
			if not walk(j, c+d):
				return False
	return cost[i] == c

ans = "Yes"

for i in range(1, N+1):
	if cost[i] is None and not walk(i, 0):
		ans = "No"
		break

print(ans)

Buggy python submission (verdict: Runtime Error):
N,M=map(int, input().split())

fi=True
inl=[[] for _ in range(N+1)]
inr=[[] for _ in range(N+1)]
pos=[None for _ in range(N+1)]
det=set()

for _ in range(M):
	l,r,d=map(int, input().split())
	inl[l].append((l,r,d))
	inl[r].append((l,r,d))

	if fi:
		fi=False
		det.add(l)
		det.add(r)
		pos[l]=0
		pos[r]=d

ans = "Yes"

while len(det) > 0 and ans == "Yes":
	p = det.pop()

	for l,r,d in inl[p]:
		if pos[r] is not None:
			if pos[r] != pos[l] + d:
				ans = "No"
				break
		else:
			pos[r] = pos[l] + d
			det.add(r)

	for l,r,d in inr[p]:
		if pos[l] is not None:
			if pos[l] != pos[r] - d:
				ans = "No"
				break
		else:
			pos[l] = pos[r] - d
			det.add(l)

print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(1145141919)

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

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

for _ in range(M):
	l,r,d=map(int, input().split())
	path[l].append((r,+d))
	path[r].append((l,-d))

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

def walk(i, c):
	if cost[i] is None:
		cost[i] = c
		for j,d in path[i]:
			if not walk(j, c+d):
				return False
	return cost[i] == c

ans = "Yes"

for i in range(1, N+1):
	if cost[i] is None and not walk(i, 0):
		ans = "No"
		break

print(ans)