Problem p03044 (python)

Fixed submission:
import sys
 
n = int(input())
links = [set() for _ in [0] * n]
for line in sys.stdin:
    u, v, w = map(int, line.split())
    u -= 1
    v -= 1
    links[u].add((v, w))
    links[v].add((u, w))
ans = [-1] * n
q = [(0, 0, -1)]
while q:
    v, d, p = q.pop()
    if d % 2 == 0:
        ans[v] = 0
    else:
        ans[v] = 1
    for u, w in links[v]:
        if u == p:
            continue
        q.append((u, d + w, v))
print('\n'.join(map(str, ans)))

Buggy submission (verdict: Runtime Error):
N=int(input())
G=[[]for i in range(N)]

for i in range(N-1):
    a,b,c=map(int,input().split())
    a-=1
    b-=1
    G[a].append((b,c))
    G[b].append((a,c))

ans=[-1]*N
#ルートでは親はいないから-1
def dfs(v,p,c):
    ans[v]=c
    for u,w in G[v]:
        if u==p:
            continue
        if w%2==0:
            dfs(u,v,c)
        else:
            dfs(u,v,1-c)
dfs(0,-1,1)
for i in range(N):
    print(ans[i])

Buggy submission (verdict: Runtime Error):
N=int(input())
#G=[set() for _ in [0]*N]
G=[[]for i in range(N)]
#print(G)

for i in range(N-1):
    a,b,c=map(int,input().split())
    a-=1
    b-=1

    G[a].append((b,c))
    G[b].append((a,c))

#print(G)
#print(G)
ans=[-1]*N
#ルートでは親はいないから-1
#q=[(0,0,-1)]
"""
Q=[(0,0,-1)]
a,b,c=Q.pop()
print(a)
print(b)
print(c)
"""
"""
#print(q)
while q:
    #v：現在見ている点
    #p：vの親のノード
    #d：距離
    v,d,p=q.pop()
    if d%2==0:
        ans[v]=0
    else:
        ans[v]=1
    ans[v]=d
    for u,w in G[v]:
        #逆流を防ぐ
        if u==p:
            continue
        if w%2==0:
            pass
        else:
            d=1-d
        q.append((u,d,v))
    #print(q)
"""
def dfs(v,p,c):
    ans[v]=c
    #print(G[v])
    for u,w in G[v]:
        if u==p:
            continue
        if w%2==0:
            dfs(u,v,c)
        else:
            dfs(u,v,1-c)
dfs(0,-1,1)
print("\n".join(map(str,ans)))

Buggy submission (verdict: Runtime Error):
N=int(input())
#G=[set() for _ in [0]*N]
G=[[]for i in range(N)]
#print(G)
u=[]
v=[]
w=[]
for i in range(N-1):
    a,b,c=map(int,input().split())
    a-=1
    b-=1

    G[a].append((b,c))
    G[b].append((a,c))

#print(G)
#print(G)
ans=[-1]*N
#ルートでは親はいないから-1
q=[(0,0,-1)]
"""
Q=[(0,0,-1)]
a,b,c=Q.pop()
print(a)
print(b)
print(c)
"""
"""
#print(q)
while q:
    #v：現在見ている点
    #p：vの親のノード
    #d：距離
    v,d,p=q.pop()
    if d%2==0:
        ans[v]=0
    else:
        ans[v]=1
    ans[v]=d
    for u,w in G[v]:
        #逆流を防ぐ
        if u==p:
            continue
        if w%2==0:
            pass
        else:
            d=1-d
        q.append((u,d,v))
    #print(q)
"""
def dfs(v,p,c):
    ans[v]=c
    #print(G[v])
    for u,w in G[v]:
        if u==p:
            continue
        if w%2==0:
            dfs(u,v,c)
        else:
            dfs(u,v,1-c)
dfs(0,-1,1)
print("\n".join(map(str,ans)))

Buggy submission (verdict: Runtime Error):
N=int(input())
#G=[set() for _ in [0]*N]
G=[[]for i in range(N)]
#print(G)
u=[]
v=[]
w=[]
for i in range(N-1):
    a,b,c=map(int,input().split())
    a-=1
    b-=1
    
    G[a].append((b,c))
    G[b].append((a,c))

#print(G)
#print(G)
ans=[-1]*N
#ルートでは親はいないから-1
q=[(0,0,-1)]
"""
Q=[(0,0,-1)]
a,b,c=Q.pop()
print(a)
print(b)
print(c)
"""
"""
#print(q)
while q:
    #v：現在見ている点
    #p：vの親のノード
    #d：距離
    v,d,p=q.pop()
    if d%2==0:
        ans[v]=0
    else:
        ans[v]=1
    ans[v]=d
    for u,w in G[v]:
        #逆流を防ぐ
        if u==p:
            continue
        if w%2==0:
            pass
        else:
            d=1-d
        q.append((u,d,v))
    #print(q)
"""
def dfs(v,p,c):
    ans[v]=c
    print(G[v])
    for u,w in G[v]:
        if u==p:
            continue
        if w%2==0:
            dfs(u,v,c)
        else:
            dfs(u,v,1-c)
dfs(0,-1,1)
print("\n".join(map(str,ans)))