Problem p03452 (python)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)

n, m = map(int,input().split())

a = [[] for _ in range(n)]

for _ in range(m):
    l, r, d = map(int,input().split())
    a[l-1].append((r-1,d))
    a[r-1].append((l-1,-d))

x = [None for _ in range(n)]

def check(y):
    for z,d in a[y]:
        if x[z] is None:
            x[z] = x[y] + d
            if not check(z):
                return False
        elif x[z] != x[y] + d:
            return False
    return True

for i in range(n):
    if x[i] is None:
        x[i] = 0
        if not check(i):
            print('No')
            exit(0)
print('Yes')

Buggy submission (verdict: Runtime Error):
n, m = map(int,input().split())

a = [[] for _ in range(n)]

for _ in range(m):
    l, r, d = map(int,input().split())
    a[l-1].append((r-1,d))
    a[r-1].append((l-1,-d))

x = [None for _ in range(n)]

def check(y):
    for z,d in a[y]:
        if x[z] == None:
            x[z] = x[y] + d
            return check(z)
        elif x[z] != x[y] + d:
            return False
    return True

for i in range(n):
    if x[i] == None:
        x[i] = 0
        if not check(i):
            print('No')
            exit(0)
print('Yes')

Buggy submission (verdict: Runtime Error):
n, m = map(int,input().split())

a = [[] for _ in range(n)]

for _ in range(m):
    l, r, d = map(int,input().split())
    a[l-1].append((r-1,d))
    a[r-1].append((l-1,-d))

x = [None for _ in range(n)]

def check(y):
    for z,d in a[y]:
        if x[z] == None:
            x[z] = x[y] + d
            return check(z)
        elif x[z] != x[y] + d:
            return False
    return True

for i in range(n):
    if x[i] == None:
        x[i] = 0
        if not check(i):
            print('No')
            break
else:
    print('Yes')

Buggy submission (verdict: Runtime Error):
n, m = map(int,input().split())

a = [[] for _ in range(n+1)]

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

x = [float('inf') for _ in range(n+1)]

flag = [True]



def check(start,destination,cost):
    #x[destination] = x[start] + cost
    if x[destination] == float('inf'):
        x[destination] = x[start] + cost
        while a[destination]:
            n_destination, n_cost = a[destination].pop()
            check(destination,n_destination,n_cost)
    else:
        if x[destination] == x[start] + cost:
            while a[destination]:
                n_destination, n_cost = a[destination].pop()
                check(destination,n_destination,n_cost)
        else:
            flag[0] = False

for i in range(1,n+1):
    if x[i] == float('inf'):
        x[i] = 0
        for y in a[i]:
            check(i,y[0],y[1])
    else:
        pass

if flag[0]:
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
n, m = map(int,input().split())

a = [[] for _ in range(n+1)]

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

x = [float('inf') for _ in range(n+1)]

flag = [True]



def check(start,destination,cost):
    #x[destination] = x[start] + cost
    if x[destination] == float('inf'):
        x[destination] = x[start] + cost
        while a[destination]:
            n_destination, n_cost = a[destination].pop()
            check(destination,n_destination,n_cost)
    else:
        if x[destination] == x[start] + cost:
            while a[destination]:
                n_destination, n_cost = a[destination].pop()
                check(destination,n_destination,n_cost)
        else:
            flag[0] = False

for i in range(1,n+1):
    if a[i]:
        x[i] = 0
        for y in a[i]:
            check(i,y[0],y[1])
    else:
        pass

if flag[0]:
    print('Yes')
else:
    print('No')