Buggy python 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()

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 python 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()

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 python submission (verdict: Time Limit Exceeded):
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)]
    adj_matrix = [[None for _ in range(N)] for __ in range(N)]
    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()

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 python submission (verdict: Time Limit Exceeded):
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)]
    adj_matrix = [[None for _ in range(N)] for __ in range(N)]
    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()

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 python submission (verdict: Time Limit Exceeded):
def topological_sort(dag):
    n_nodes = len(dag)
    n_ins = [0]*n_nodes
    in_zeros = []
    sorted_nodes = []

    for src in range(n_nodes):
        for dst in dag[src]:
            n_ins[dst] += 1

    for i, v in enumerate(n_ins):
        if v == 0:
            in_zeros.append(i)

    while in_zeros:
        node = in_zeros.pop()
        sorted_nodes.append(node)
        for dst in dag[node]:
            n_ins[dst] -= 1
            if n_ins[dst] == 0:
                in_zeros.append(dst)
    
    return sorted_nodes

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)]
    adj_matrix = [[None for _ in range(N)] for __ in range(N)]
    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)
    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()


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()