Problem p03450 (python)

Fixed submission:
def resolve():
    N, M = list(map(int, input().split()))
    LRD = [list(map(int, input().split())) for _ in range(M)]
    adj_list = [[] for _ in range(N)]
    for lrd in LRD:
        l, r, d = lrd
        adj_list[l-1].append((l-1, r-1, d))
        adj_list[r-1].append((r-1, l-1, -d))
    
    X = [None]*N
    import collections

    for i in range(N):
        if X[i] is not None:
            continue
        q = collections.deque(adj_list[i])
        X[i] = 0
        while q:
            src, nxt, d = q.pop()
            if X[nxt] is not None:
                if X[nxt] != X[src] + d:
                    print("No")
                    return
            else:
                X[nxt] = X[src] + d
                q.extend(adj_list[nxt])
    print("Yes")

    


if '__main__' == __name__:
    resolve()

Buggy submission (verdict: Runtime Error):
def topo_sort(g):
    from collections import deque
    V = len(g)
    deg = [0]*V
    for src in range(V):
        for dst in g[src]:
            deg[dst] += 1
    ans = list(v for v in range(V) if deg[v]==0)
    deq = deque(ans)
    used = [0]*V

    while deq:
        v = deq.popleft()
        for t in g[v]:
            deg[t] -= 1
            if deg[t]==0:
                deq.append(t)
                ans.append(t)
    return ans

def resolve():
    N, M = list(map(int, input().split()))
    LRD = [list(map(int, input().split())) for _ in range(M)]
    adj_list = [[] for _ in range(N)]
    import numpy
    adj_matrix = numpy.full((N, N), None)
    for lrd in LRD:
        l, r, d = lrd
        adj_list[l-1].append(r-1)
        adj_matrix[l-1][r-1] = d
    
    #top_order = topological_sort(adj_list)
    top_order = topo_sort(adj_list)
    
    if len(top_order) != len(adj_list):
        print("No")
        return
    
    acc = 0
    start = top_order[0]
    now = top_order[0]
    for i in range(1, len(top_order)):
        nxt = top_order[i]
        if adj_matrix[now][nxt] is not None:
            if adj_matrix[start][nxt] is not None and acc+adj_matrix[now][nxt] != adj_matrix[start][nxt]:
                print("No")
                return
            acc += adj_matrix[now][nxt]
        now = nxt
        
    print("Yes")


if '__main__' == __name__:
    resolve()

Buggy submission (verdict: Runtime Error):
def topo_sort(g):
    from collections import deque
    V = len(g)
    deg = [0]*V
    for src in range(V):
        for dst in g[src]:
            deg[dst] += 1
    ans = list(v for v in range(V) if deg[v]==0)
    deq = deque(ans)
    used = [0]*V

    while deq:
        v = deq.popleft()
        for t in g[v]:
            deg[t] -= 1
            if deg[t]==0:
                deq.append(t)
                ans.append(t)
    return ans

def resolve():
    N, M = list(map(int, input().split()))
    LRD = [list(map(int, input().split())) for _ in range(M)]
    adj_list = [[] for _ in range(N)]
    import numpy
    adj_matrix = numpy.full((N, N), None)
    for lrd in LRD:
        l, r, d = lrd
        adj_list[l-1].append(r-1)
        adj_matrix[l-1][r-1] = d
    
    #top_order = topological_sort(adj_list)
    top_order = topo_sort(adj_list)
    
    if len(top_order) != len(adj_list):
        print("No")
        return
    
    acc = 0
    start = top_order[0]
    now = top_order[0]
    for i in range(1, len(top_order)):
        nxt = top_order[i]
        if adj_matrix[now][nxt] is not None:
            if adj_matrix[start][nxt] is not None and acc+adj_matrix[now][nxt] != adj_matrix[start][nxt]:
                print("No")
                return
            acc += adj_matrix[now][nxt]
        now = nxt
        
    print("Yes")


if '__main__' == __name__:
    resolve()