Problem p03044 (python)

Fixed submission:
import sys
from collections import deque

n = int(input())

color_ls = {i: None for i in range(n)}
dict_map = {i : [] for i in range(n)}
already = set()
for i in range(n - 1):
    u, v, w = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        root = u
    dict_map[u].append([v, w])
    dict_map[v].append([u, w])
    already.update({u, v})
color_ls[root] = 0
cur = root
already.clear()
que = deque()
while True:
    candidate = dict_map[cur]
    for cand, w in candidate:
        if cand in already:
            continue
        already.add(cand)
        que.append(cand)
        if w % 2 == 0:
            color_ls[cand] = 0 if color_ls[cur] == 0 else 1
        else:
            color_ls[cand] = 1 if color_ls[cur] == 0 else 0
    if len(que) == 0 or len(already) == n:
        break
    cur = que.popleft()

for i in range(n):
    print(color_ls[i])

Buggy submission (verdict: Runtime Error):
import sys

n = int(input())

node_ls = {i: Node(-1, i, []) for i in range(n)}
_map = {j: [0 for i in range(n)] for j in range(n)}
already = set()
color_ls = [-1 for i in range(n)]
for i in range(n - 1):
    u, v, w = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        color_ls[u] = 0
    _map[u][v] = w
    _map[v][u] = w
    if u not in already and v in already:
        node_ls[u].parent = v
        node_ls[v].child.append(u)
        color_ls[u] = 0 if (w % 2 == 0 and color_ls[v] == 0) or (w % 2 == 1 and color_ls[v] == 1) else 1
    else:
        node_ls[v].parent = u
        node_ls[u].child.append(v)
        color_ls[v] = 0 if (w % 2 == 0 and color_ls[u] == 0) or (w % 2 == 1 and color_ls[u] == 1) else 1
    already.update({u, v})
for c in color_ls:
    print(c)

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
from collections import deque

n = int(input())


color_ls = {i:None for i in range(n)}
_map = np.zeros((n, n))
already = set()
for i in range(n-1):
    u, v, w = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        root = u
    _map[u][v] = w
    _map[v][u] = w
    already.update({u, v})
color_ls[root] = 0
cur = root
already.clear()
que = deque()
while True:
    candidate = np.nonzero(_map[cur])[0]
    for cand in candidate:
        if cand in already:
            continue
        already.add(cand)
        que.append(cand)
        if _map[cur][cand] % 2 == 0:
            color_ls[cand] = 0 if color_ls[cur] == 0 else 1
        else:
            color_ls[cand] = 1 if color_ls[cur] == 0 else 0
    if len(que) == 0:
        break
    cur = que.popleft()

for i in range(n):
    print(color_ls[i])

Buggy submission (verdict: Runtime Error):
import sys
import queue
n = int(input())
class Node:

    def __init__(self, parent, id, child):
        self.parent = parent
        self.id = id
        self.child = child
        self.color = None

def get_root(node):
    while True:
        parent = node.parent
        if parent == -1:
            break
        node = node_ls[parent]
    return node.id

node_ls = {i:Node(-1, i, []) for i in range(n)}
_map = {j:[0 for i in range(n)] for j in range(n)}
already = set()
for i in range(n-1):
    u, v, w = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        root = u
    _map[u][v] = w
    _map[v][u] = w
    if u not in already and v in already:
        node_ls[u].parent = v
        node_ls[v].child.append(u)
    else:
        node_ls[v].parent = u
        node_ls[u].child.append(v)
    already.update({u, v})
node_ls[root].color = 0
cur = node_ls[root]
que = []
while True:
    children = cur.child
    for child in children:
        if node_ls[child].color is not None:
            continue
        queue.heappush(que, child)
        if _map[cur.id][child] % 2 == 0:
            node_ls[child].color = 1 if node_ls[cur.id].color == 1 else 0
        else:
            node_ls[child].color = 0 if node_ls[cur.id].color == 1 else 1
    if len(que) == 0:
        break
    cur = queue.heappop(que)
    cur = node_ls[cur]
res = [node.color for node in node_ls]
for r in res:
    print(r)

Buggy submission (verdict: Runtime Error):
import sys
import queue
n = int(input())
class Node:

    def __init__(self, parent, id, child):
        self.parent = parent
        self.id = id
        self.child = child
        self.color = None

def get_root(node):
    while True:
        parent = node.parent
        if parent == -1:
            break
        node = node_ls[parent]
    return node.id

node_ls = [Node(-1, i, []) for i in range(n)]
_map = [[0 for i in range(n)] for j in range(n)]
already = set()
for i in range(n-1):
    u, v, w = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        root = u
    _map[u][v] = w
    _map[v][u] = w
    if u not in already and v in already:
        node_ls[u].parent = v
        node_ls[v].child.append(u)
    else:
        node_ls[v].parent = u
        node_ls[u].child.append(v)
    already.update({u, v})
node_ls[root].color = 0
cur = node_ls[root]
que = []
while True:
    children = cur.child
    for child in children:
        if node_ls[child].color is not None:
            continue
        queue.heappush(que, child)
        if _map[cur.id][child] % 2 == 0:
            node_ls[child].color = 1 if node_ls[cur.id].color == 1 else 0
        else:
            node_ls[child].color = 0 if node_ls[cur.id].color == 1 else 1
    if len(que) == 0:
        break
    cur = queue.heappop(que)
    cur = node_ls[cur]
res = [node.color for node in node_ls]
for r in res:
    print(r)