Problem p03044 (python)

Fixed submission:
from collections import deque

N = int(input())
graph = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v, w = map(int, input().split())
    u, v = u - 1, v - 1
    graph[u].append([v, w])
    graph[v].append([u, w])

colors = [-1 for _ in range(N)]
stack = deque()
stack.append(0)
colors[0] = 0

while stack:
    n = stack.popleft()
    for _next, w in graph[n]:
        if colors[_next] != -1:
            continue
        colors[_next] = (colors[n] + w) % 2
        stack.append(_next)

for c in colors:
    print(c)

Buggy submission (verdict: Runtime Error):
from collections import deque

N = int(input())
graph = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v, w = map(int, input().split())
    u, v, w = u - 1, v - 1, w % 2
    graph[u].append([v, w])
    graph[v].append([u, w])

colors = [-1 for _ in range(N)]
stack = deque()
stack.append(0)
colors[0] = 0

while stack:
    n = stack.pop()
    for _next, w in graph[n]:
        if colors[_next] != -1:
            continue
        colors[_next] = (colors[n] + w) % 2
        stack.append([_next])

for c in colors:
    print(c)

Buggy submission (verdict: Runtime Error):
def dfs(node, color=0):
    colors[node] = color
    for next_n, weight in graph.get(node, set()):
        if colors[next_n] != -1:
            continue
        if weight % 2 == 0:
            color = colors[node]
        else:
            color = 1 if colors[node] == 0 else 0
        colors[next_n] = color
        dfs(next_n, color)


N = int(input())
if N == 1:
    u_v_w_l = []
else:
    u_v_w_l = [map(int, input().split()) for _ in range(N - 1)]

graph = {}
for u, v, w in u_v_w_l:
    u, v = u - 1, v - 1
    u_set = graph.get(u, set())
    u_set.add((v, w))
    v_set = graph.get(v, set())
    v_set.add((u, w))
    graph[u] = u_set
    graph[v] = v_set

colors = [-1 for _ in range(N)]

if N == 1:
    colors[0] = 0
else:
    for n in range(N):
        if colors[n] != -1:
            continue
        dfs(n)

for c in colors:
    print(c)

Buggy submission (verdict: Runtime Error):
def dfs(node, color=0):
    colors[node] = color
    for next_n, weight in graph.get(node, set()):
        if colors[next_n] != -1:
            continue
        if weight % 2 == 0:
            color = colors[node]
        else:
            color = 1 if colors[node] == 0 else 0
        colors[next_n] = color
        dfs(next_n, color)


N = int(input())
u_v_w_l = [map(int, input().split()) for _ in range(N - 1)]

graph = {}
for u, v, w in u_v_w_l:
    u, v = u - 1, v - 1
    u_set = graph.get(u, set())
    u_set.add((v, w))
    v_set = graph.get(v, set())
    v_set.add((u, w))
    graph[u] = u_set
    graph[v] = v_set

colors = [-1 for _ in range(N)]

if N == 1:
    colors[0] = 0
else:
    for n in range(N):
        if colors[n] != -1:
            continue
        dfs(n)

for c in colors:
    print(c)

Buggy submission (verdict: Runtime Error):
def dfs(node, color=0):
    colors[node] = color
    for next_n, weight in graph.get(node, set()):
        if colors[next_n] != -1:
            continue
        if weight % 2 == 0:
            color = colors[node]
        else:
            color = 1 if colors[node] == 0 else 0
        colors[next_n] = color
        dfs(next_n, color)


N = int(input())
u_v_w_l = [map(int, input().split()) for _ in range(N - 1)]

graph = {}
for u, v, w in u_v_w_l:
    u, v = u - 1, v - 1
    u_set = graph.get(u, set())
    u_set.add((v, w))
    v_set = graph.get(v, set())
    v_set.add((u, w))
    graph[u] = u_set
    graph[v] = v_set

colors = [-1 for _ in range(N)]

if N == 1:
    colors[0] = 0
else:
    for n in range(N):
        if colors[n] != -1:
            continue
        dfs(n)

for c in colors:
    print(c)