Problem p03450 (python)

Fixed submission:
import sys
sys.setrecursionlimit(200002)


def dfs(i, c):
    C[i] = True
    P[i] = c
    for t in G[i]:
        if (C[t[0]] == True):
            continue
        dfs(t[0], c+t[1])
    return


N, M = list(map(int, input().split()))
G = [[] for _ in range(N+1)]
P = [None for _ in range(N+1)]
C = [False for _ in range(N+1)]

for i in range(M):
    l, r, d = list(map(int, input().split()))
    G[l].append((r,d))
    G[r].append((l,-d))

for i in range(1, N+1):
    if (C[i] == False):
        dfs(i,0)

f = False
for i in range(1, N+1):
    for t in G[i]:
        if ((P[t[0]] - P[i]) != t[1]):
            f = True
if (f):
    print('No')
else:
    print('Yes')

Buggy submission (verdict: Runtime Error):
def dfs(i, c):
    C[i] = True
    P[i] = c
    for t in G[i]:
        if (C[t[0]] == True):
            continue
        dfs(t[0], c+t[1])
    return


N, M = list(map(int, input().split()))
G = [[] for _ in range(N+1)]
P = [0 for _ in range(N+1)]
C = [False for _ in range(N+1)]

for i in range(M):
    l, r, d = list(map(int, input().split()))
    G[l].append((r,d))
    G[r].append((l,-d))

for i in range(1, N+1):
    if (C[i] == False):
        dfs(i,0)

f = False
#for i in range(1, N+1):
#    for t in G[i]:
#        if ((P[t[0]] - P[i]) != t[1]):
#            f = True
if (f):
    print('No')
else:
    print('Yes')

Buggy submission (verdict: Runtime Error):
def dfs(i, c):
    C[i] = True
    P[i] = c
    for t in G[i]:
        if (C[t[0]] == True):
            continue
        dfs(t[0], c+t[1])
    return


N, M = list(map(int, input().split()))
G = [[] for _ in range(N+1)]
P = [0 for _ in range(N+1)]
C = [False for _ in range(N+1)]

for i in range(M):
    l, r, d = list(map(int, input().split()))
    G[l].append((r,d))
    G[r].append((l,-d))

for i in range(1, N+1):
    if (C[i] == False):
        dfs(i,0)

f = False
for i in range(1, N+1):
    for t in G[i]:
        if ((P[t[0]] - P[i]) != t[1]):
            f = True
if (f):
    print('No')
else:
    print('Yes')

Buggy submission (verdict: Runtime Error):
def dfs(i, c):
    C[i] = True
    P[i] = c
    for t in G[i]:
        if (C[t[0]] == True):
            continue
        dfs(t[0], c+t[1])
    return


N, M = list(map(int, input().split()))
G = [[] for _ in range(N+1)]
P = [None for _ in range(N+1)]
C = [False for _ in range(N+1)]

for i in range(M):
    l, r, d = list(map(int, input().split()))
    G[l].append((r,d))
    G[r].append((l,-d))

for i in range(1, N+1):
    if (C[i] == False):
        dfs(i,0)

f = False
for i in range(1, N+1):
    for t in G[i]:
        if ((P[t[0]] - P[i]) != t[1]):
            f = True
if (f):
    print('No')
else:
    print('Yes')

Buggy submission (verdict: Runtime Error):
def dfs(i, c):
    C[i] = True
    P[i] = c
    for t in G[i]:
        if (C[t[0]] == True):
            continue
        dfs(t[0], c+t[1])
    return


N, M = list(map(int, input().split()))
G = [[] for _ in range(N+1)]
P = [None for _ in range(N+1)]
C = [False for _ in range(N+1)]

for i in range(M):
    l, r, d = list(map(int, input().split()))
    G[l].append((r,d))
    G[r].append((l,-d))

for i in range(1, N+1):
    if (C[i] == False):
        dfs(i,0)

f = False
for i in range(1, N+1):
    for t in G[i]:
        if (P[t[0]] - P[i] != t[1]):
            f = True
if (f):
    print('No')
else:
    print('Yes')

Buggy submission (verdict: Runtime Error):
def dfs(i, c):
    C[i] = True
    P[i] = c
    for t in G[i]:
        if (C[t[0]] == True):
            continue
        dfs(t[0], c+t[1])
    return


N, M = list(map(int, input().split()))
G = [[] for _ in range(N+1)]
P = [None for _ in range(N+1)]
C = [False for _ in range(N+1)]

for i in range(M):
    l, r, d = list(map(int, input().split()))
    G[l].append((r,d))
    G[r].append((l,-d))

for i in range(1, N+1):
    if (C[i] == False):
        dfs(i,0)

f = False
for i in range(N):
    for t in G[i]:
        if (P[t[0]] - P[i] != t[1]):
            f = True
if (f):
    print('No')
else:
    print('Yes')

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

a = []
m = 1000000
for i in range(M):
    l, r, d = list(map(int, input().split()))
    if l > r:
        d = -d
        l,r = r,l
    m = min(m, l)
    a.append(tuple(l,r,d))
a = sorted(a, lambda t: t[0])
d = {}
d[m] = True
p[m] = 0
p = [0 for _ in range(N)]

for i in range(M):
    if (a[i][0] in d) and (a[i][1] in d):
        if ((a[i][1] - a[i][0]) != a[i][2]):
            print('No')
            sys.exit()
    elif (a[i][0] not in d) and (a[i][1] in d):
        np = p[a[i][1]] - a[i][2]
        p[a[i][0]] = np
        d[a[i][0]] = True
    elif (a[i][0] in d) and (a[i][1] not in d):
        np = p[a[i][0]] + a[i][2]
        p[a[i][1]] = np
        d[a[i][1]] = True
    else:
        p[a[i][1]] = 0
        d[a[i][1]] = True

        np = p[a[i][1]] - a[i][2]
        p[a[i][0]] = np
        d[a[i][0]] = True
print('Yes')