Problem p03450 (python)

Fixed submission:
n, m = map(int, input().split())
D = [list(map(int, input().split())) for i in range(m)]

A = [0] * (n+1)

M = [[] for i in range(n+1)]

for i in range(m):
    M[D[i][0]].append([D[i][1], D[i][2]])
    M[D[i][1]].append([D[i][0], -D[i][2]])

V = [0] * (n+1)


# print(M)

for ii in range(1, n+1):
    if V[ii] == 0 and len(M[ii]) > 0:

        Q = [[ii, 0]]
        st = 0
        while st < len(Q):
            # print(A,V,Q)
            node = Q[st][0]
            dist = Q[st][1]
            if V[node] == 0 and len(M[node]) > 0:
                V[node] = 1
                A[node] += dist
                for i in M[node]:
                    if V[i[0]] == 0:
                        Q.append([i[0], A[node] + i[1]])
            st += 1

# print(A)

def d():
    for i in range(m):
        if A[D[i][1]] - A[D[i][0]] != D[i][2]:
            return "No"
    return "Yes"

print(d())

Buggy submission (verdict: Runtime Error):
#import matplotlib.pyplot as plt
#import networkx as nx

n, m = map(int, input().split())
D = [list(map(int, input().split())) for i in range(m)]

G = [[0, 0, []] for i in range(n+1)]


#print(G.nodes())
for i in range(m) :
    G[D[i][0]][2].append([D[i][1], D[i][2]])
    G[D[i][1]][2].append([D[i][0], -D[i][2]])

global no
no = 0

def d(a):
    G[a][1] = 1
    A = []
    #print(len(G[a][2]))
    #print(G[a][2][0][0])
    for j in range(len(G[a][2])) :
        i = G[a][2][j][0]
        #print(set(list(nx.all_neighbors(G, a))))
        #print("{} {}".format(a,i))
        if G[i][1] == 0 :
            A.append(i)
            G[i][0] = G[a][0] + G[a][2][j][1]
            G[i][1] = 1
        else :
            #print("{} {} {}".format(G[i][0],G[a][0],G[a][2][j][1]))
            if G[i][0] != G[a][0] + G[a][2][j][1] :
                #print("A")
                global no
                no = 1
                #print("{} {}".format("no",no))
    for i in A :
        d(i)

for i in range(1,n+1):
    if G[i][1] == 0 :
        d(i)
        
if no == 1 :
    print("No")
else :
    print("Yes")

Buggy submission (verdict: Runtime Error):
#import matplotlib.pyplot as plt
import networkx as nx

n, m = map(int, input().split())
D = [list(map(int, input().split())) for i in range(m)]

G = [[0, 0, []] for i in range(n+1)]


#print(G.nodes())
for i in range(m) :
    G[D[i][0]][2].append([D[i][1], D[i][2]])
    G[D[i][1]][2].append([D[i][0], -D[i][2]])

global no
no = 0

def d(a):
    G[a][1] = 1
    A = []
    #print(len(G[a][2]))
    #print(G[a][2][0][0])
    for j in range(len(G[a][2])) :
        i = G[a][2][j][0]
        #print(set(list(nx.all_neighbors(G, a))))
        #print("{} {}".format(a,i))
        if G[i][1] == 0 :
            A.append(i)
            G[i][0] = G[a][0] + G[a][2][j][1]
            G[i][1] = 1
        else :
            #print("{} {} {}".format(G[i][0],G[a][0],G[a][2][j][1]))
            if G[i][0] != G[a][0] + G[a][2][j][1] :
                #print("A")
                global no
                no = 1
                #print("{} {}".format("no",no))
    for i in A :
        d(i)

for i in range(1,n+1):
    if G[i][1] == 0 :
        d(i)
        
if no == 1 :
    print("No")
else :
    print("Yes")

Buggy submission (verdict: Runtime Error):
#import matplotlib.pyplot as plt
import networkx as nx

n, m = map(int, input().split())
D = [list(map(int, input().split())) for i in range(m)]

G = nx.DiGraph()
for i in range(n) :
    G.add_node(i+1, seisu = 0, houmon = 0)

#print(G.nodes())
for i in range(m) :
    G.add_edge(D[i][0], D[i][1], weight=D[i][2])
    G.add_edge(D[i][1], D[i][0], weight=-D[i][2])

global no
no = 0

def d(a):
    G.nodes[a]["houmon"] = 1
    A = []
    for i in set(list(nx.all_neighbors(G, a))) :
        #print(set(list(nx.all_neighbors(G, a))))
        #print("{} {}".format(a,i))
        if G.nodes[i]["houmon"] == 0 :
            A.append(i)
            G.nodes[i]["seisu"] = G.nodes[a]["seisu"] + G.edges[a, i]["weight"]
            G.nodes[i]["houmon"] = 1
        else :
            #print("{} {} {}".format(G.nodes[i]["seisu"],G.nodes[a]["seisu"],G.edges[a, i]["weight"]))
            if G.nodes[i]["seisu"] != G.nodes[a]["seisu"] + G.edges[a, i]["weight"] :
                #print("A")
                global no
                no = 1
                #print("{} {}".format("no",no))
    for i in A :
        d(i)

for i in range(1,n+1):
    if G.nodes[i]["houmon"] == 0 :
        d(i)
        
if no == 1 :
    print("No")
else :
    print("Yes")