Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(2*10**6)
input = sys.stdin.readline
N, M = map(int, input().split())
G = [[] for _ in [0]*N]
for _ in [0]*M:
    a, b, d = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append((b, d))
    G[b].append((a, -d))

X = [None]*N

def dfs(i, x=0):
    for j, d in G[i]:
        if X[j] is None:
            X[j] = x+d
            dfs(j, x+d)
        elif X[j] != x+d:
            print('No')
            exit()

for i in range(N):
    if X[i] is not None:
        continue
    X[i] = 0
    dfs(i)
print('Yes')

Fixed submission:
import sys
sys.setrecursionlimit(2*10**6)
input = sys.stdin.readline
N, M = map(int, input().split())
G = [[] for _ in [0]*N]
for _ in [0]*M:
    a, b, d = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append((b, d))
    G[b].append((a, -d))

X = [None]*N

def dfs(i, x=0):
    for j, d in G[i]:
        if X[j] is None:
            X[j] = x+d
            dfs(j, x+d)
        elif X[j] != x+d:
            print('No')
            exit()

for i in range(N):
    if X[i] is not None:
        continue
    X[i] = 0
    dfs(i)
print('Yes')

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(2*10**6)
input = sys.stdin.readline
N, M = map(int, input().split())
G = [[] for _ in [0]*N]

for _ in [0]*M:
    a, b, d = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append((b, d))
    G[b].append((a, -d))

X = [None]*N

def dfs(i, x=0):
    X[i] = x
    for j, d in G[i]:
        if X[j] is not None:
            if X[j] != x+d:
                print('No')
                exit()
            else:
                continue
        dfs(j, x+d)


for i in range(N):
    if X[i] is not None:
        continue
    dfs(i)
print('Yes')

Fixed submission:
import sys
sys.setrecursionlimit(2*10**6)
input = sys.stdin.readline
N, M = map(int, input().split())
G = [[] for _ in [0]*N]
for _ in [0]*M:
    a, b, d = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append((b, d))
    G[b].append((a, -d))

X = [None]*N

def dfs(i, x=0):
    for j, d in G[i]:
        if X[j] is None:
            X[j] = x+d
            dfs(j, x+d)
        elif X[j] != x+d:
            print('No')
            exit()

for i in range(N):
    if X[i] is not None:
        continue
    X[i] = 0
    dfs(i)
print('Yes')

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
N, M = map(int, input().split())
G = [[] for _ in [0]*N]

for _ in [0]*M:
    a, b, d = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append((b, d))
    G[b].append((a, -d))

visited = [False]*N
X = [None]*N


def isok(i, x=0):
    visited[i] = True
    X[i] = x

    for j, d in G[i]:
        if X[j] is not None and X[j] != x+d:
            return False
        elif visited[j]:
            continue
        if not isok(j, x+d):
            return False
    return True


for i in range(N):
    if visited[i]:
        continue
    if not isok(i):
        print('No')
        break
else:
    print('Yes')


Fixed submission:
import sys
sys.setrecursionlimit(2*10**6)
input = sys.stdin.readline
N, M = map(int, input().split())
G = [[] for _ in [0]*N]
for _ in [0]*M:
    a, b, d = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append((b, d))
    G[b].append((a, -d))

X = [None]*N

def dfs(i, x=0):
    for j, d in G[i]:
        if X[j] is None:
            X[j] = x+d
            dfs(j, x+d)
        elif X[j] != x+d:
            print('No')
            exit()

for i in range(N):
    if X[i] is not None:
        continue
    X[i] = 0
    dfs(i)
print('Yes')