Problem p03450 (python)

Fixed submission:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(200002)

def dfs(s):
    for t, d in G[s]:
        if x[t] is None:
            x[t] = x[s] + d
            if not dfs(t):
                return False
        else:
            if x[t] - x[s] != d:
                return False
    return True

N, M = map(int, input().split())
x = [None] * N
G = [[] for _ in range(N)]
for i in range(M):
    L, R, D = map(int, input().split())
    L -= 1
    R -= 1
    G[L].append([R, D])
    G[R].append([L, -D])
ans = True
for i in range(N):
    if x[i] is None:
        x[i] = 0
        if not dfs(i):
            ans = False
print(('No', 'Yes')[ans])

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
inf = float('inf')

ans = True
def dfs(s):
    global ans
    global x
    for t, d in G[s]:
        if x[t] == inf:
            x[t] = x[s] + d
            dfs(t)
        else:
            if x[t] - x[s] != d:
                ans = False

N, M = map(int, input().split())
x = [inf] * N
G = [[] for _ in range(N)]
for i in range(M):
    L, R, D = map(int, input().split())
    L -= 1
    R -= 1
    G[L].append([R, D])
    G[R].append([L, -D])
for i in range(N):
    if x[i] == inf:
        x[i] = 0
        dfs(i)
print(('No', 'Yes')[ans])

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
inf = float('inf')

ans = True
def dfs(s):
    global ans
    for t, d in G[s]:
        if x[t] == inf:
            x[t] = x[s] + d
            dfs(t)
        else:
            if x[t] - x[s] != d:
                ans = False

N, M = map(int, input().split())
x = [inf] * N
G = [[] for _ in range(N)]
for i in range(M):
    L, R, D = map(int, input().split())
    L -= 1
    R -= 1
    G[L].append([R, D])
    G[R].append([L, -D])
for i in range(N):
    if x[i] == inf:
        x[i] = 0
        dfs(i)
print(('No', 'Yes')[ans])

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
inf = float('inf')

def dfs(s):
    for t, d in G[s]:
        if x[t] == inf:
            x[t] = x[s] + d
            dfs(t)
        else:
            if x[t] - x[s] != d:
                return False
    return True

N, M = map(int, input().split())
x = [inf] * N
G = [[] for _ in range(N)]
ans = True
for i in range(M):
    L, R, D = map(int, input().split())
    L -= 1
    R -= 1
    G[L].append([R, D])
    G[R].append([L, -D])
for i in range(N):
    if x[i] == inf:
        x[i] = 0
        if not dfs(i):
            ans = False
print(('No', 'Yes')[ans])

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
N, M = map(int, input().split())
x = [0] * N
used = [False] * N
G = [[] for _ in range(N)]

def dfs(s):
    for t, d in G[s]:
        if not used[t]:
            used[t] = True
            x[t] = x[s] + d
            dfs(t)
        else:
            if x[t] - x[s] != d:
                return False
    return True

ans = True
for i in range(M):
    L, R, D = map(int, input().split())
    L -= 1
    R -= 1
    G[L].append([R, D])
    G[R].append([L, -D])
for i in range(N):
    if not used[i]:
        used[i] = True
        if not dfs(i):
            ans = False
            break
print(('No', 'Yes')[ans])

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
N, M = map(int, input().split())
x = [-1] * N
used = [False] * N
G = [[] for _ in range(N)]

def dfs(s):
    for t, d in G[s]:
        if not used[t]:
            used[t] = True
            x[t] = x[s] + d
            dfs(t)
        else:
            if x[t] - x[s] != d:
                return False
    return True

ans = True
for i in range(M):
    L, R, D = map(int, input().split())
    L -= 1
    R -= 1
    G[L].append([R, D])
    G[R].append([L, -D])
for i in range(N):
    if not used[i]:
        used[i] = True
        x[i] = 0
        if not dfs(i):
            ans = False
            break
print(('No', 'Yes')[ans])