Problem p03450 (python)

Fixed submission:
import sys
sys.setrecursionlimit(200002)

N, M = map(int, input().split())
G = [[] for _ in range(N)]
x = [None] * (N)

def dfs(s):
    global x, G
    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

for _ in range(M):
    L, R, D = map(int, input().split())
    G[L-1].append([R-1,  D])
    G[R-1].append([L-1, -D])

ans = True
for i in range(N):
    if x[i] is None:
        x[i] = 0
        ans &= dfs(i)

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

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
G = [[] for _ in range(N)]
x = [None] * (N)

def dfs(s):
    global x, G
    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

for _ in range(M):
    L, R, D = map(int, input().split())
    G[L-1].append([R-1,  D])
    G[R-1].append([L-1, -D])

ans = True
for i in range(N):
    if x[i] is None:
        x[i] = 0
        ans &= dfs(i)

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

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
G = [[] for _ in range(N)]
x = [None] * (N)

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

for _ in range(M):
    L, R, D = map(int, input().split())
    G[L-1].append([R-1,  D])
    G[R-1].append([L-1, -D])

ans = True
for i in range(N):
    if x[i] is None:
        x[i] = 0
        ans &= dfs(i)

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

Buggy submission (verdict: Runtime Error):
N, M = list(map(int, input().split()))
G = [[] for _ in range(N+1)]
x = [None] * (N+1)

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

for _ in range(M):
    L, R, D = map(int, input().split())
    G[L-1].append([R-1,  D])
    G[R-1].append([L-1, -D])

ans = True
for i in range(N):
    if x[i] is None:
        x[i] = 0
        ans &= dfs(i)

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

Buggy submission (verdict: Runtime Error):
N, M = list(map(int, input().split()))
G = [[] for _ in range(N+1)]
x = [None] * (N+1)

def fail():
    print("No")
    # exit()

def dfs(s):
    for t, d in G[s]:
        if x[t] is None:
            x[t] = x[s] + d
            dfs(t)
        else:
            if x[t] != x[s] + d:
                fail()

for _ in range(M):
    L, R, D = map(int, input().split())
    G[L-1].append([R-1,  D])
    G[R-1].append([L-1, -D])

for i in range(N):
    if x[i] is None:
        x[i] = 0
        dfs(i)

print("Yes")

Buggy submission (verdict: Runtime Error):
N, M = list(map(int, input().split()))
G = [[] for _ in range(N+1)]
x = [None] * (N+1)

def fail():
    print("No")
    exit()

def dfs(s):
    for t, d in G[s]:
        if x[t] is None:
            x[t] = x[s] + d
            dfs(t)
        else:
            if x[t] != x[s] + d:
                fail()

for _ in range(M):
    L, R, D = map(int, input().split())
    G[L-1].append([R-1,  D])
    G[R-1].append([L-1, -D])

for i in range(N):
    if x[i] is None:
        x[i] = 0
        dfs(i)

print("Yes")

Buggy submission (verdict: Runtime Error):
N, M = list(map(int, input().split()))
G = [[] for _ in range(N+1)]
x = [None] * (N+1)

def fail():
    print("No")
    exit()

def dfs(s):
    for t, d in G[s]:
        if x[t] is None:
            x[t] = x[s] + d
            dfs(t)
        else:
            if x[t] != x[s] + d:
                fail()

for _ in range(M):
    L, R, D = map(int, input().split())
    G[L].append([R,  D])
    G[R].append([L, -D])

for i in range(N):
    if x[i] is None:
        x[i] = 0
        dfs(i)

print("Yes")

Buggy submission (verdict: Runtime Error):
N, M = list(map(int, input().split()))
LRD = [list(map(int, input().split())) for i in range(M) ]
x = [-1]*(N+1)
unsolve = LRD

if M == 0:
    print("Yes")
    exit()

def sr(unsolves):
    nextUnsolve = []
    for u in unsolves:
        l, r, d = u
        if x[l] == -1 and x[r] == -1:
            nextUnsolve.append(u)
        elif x[l] == -1:
            x[l] = x[r] - d
        elif x[r] == -1:
            x[r] = x[l] + d
        else:
            if x[r] != x[l] + d:
                print("No")
                exit()
    if not not nextUnsolve:
        sr(nextUnsolve)

x[LRD[0][0]] = 0
sr(unsolve)

print("Yes")