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

N = int(input())
color = [0]*(N+1)
color_val = -1
finded = set()
codebook = {0:0, 1:1}

for _ in range(N-1):
    u, v, w = map(int, input().split())
    if u in finded and v in finded:
        if color[u] in codebook:
            codebook[color[v]] = (codebook[color[u]] + (w % 2)) % 2
            if color[v] % 2 == 0:
                codebook[color[v] + 1] = (codebook[color[v]] + 1) % 2
            else:
                codebook[color[v] - 1] = (codebook[color[v]] + 1) % 2
        else:
            codebook[color[u]] = (codebook[color[v]] + (w % 2)) % 2
            if color[u] % 2 == 0:
                codebook[color[u] + 1] = (codebook[color[u]] + 1) % 2
            else:
                codebook[color[u] - 1] = (codebook[color[u]] + 1) % 2
    elif u in finded:
        finded.add(v)
        color[v] = (color[u] + (w % 2)) % 2 + color[u]//2 * 2
    elif v in finded:
        finded.add(u)
        color[u] = (color[v] + (w % 2)) % 2 + color[v]//2 * 2
    else:
        finded.add(u)
        finded.add(v)
        color_val += 1
        if w % 2 == 0:
            color[u], color[v] = 2 * color_val, 2 * color_val
        if w % 2 != 0:
            color[u], color[v] = 2 * color_val, 2 * color_val + 1

for i in range(1, N+1):
    print(codebook[color[i]])

Fixed submission:
from collections import deque

N = int(input())
tree = [dict() for _ in range(N+1)]
color = [0]*(N+1)
parent = [0]*(N+1)
for _ in range(N-1):
    u, v, w = map(int, input().split())
    w %= 2
    tree[u][v] = w
    tree[v][u] = w

que = deque([1])
while len(que) != 0:
    u = que.pop()
    Rel, c, p = tree[u], color[u], parent[u]
    for v in Rel.keys():
        if v == p:
            continue
        elif Rel[v] == 0:
            color[v] = c
        else:
            color[v] = c^1
        parent[v] = u
        que.append(v)

for c in color[1:]:
    print(c)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N = int(input())
color = [0]*(N+1)
q = deque()

u, v, w = map(int, input().split())
finded = set([u, v])
color[v] = (color[u] + (w % 2)) % 2

for _ in range(N-2):
    u, v, w = map(int, input().split())
    if u in finded:
        finded.add(v)
        color[v] = (color[u] + (w % 2)) % 2
    elif v in finded:
        finded.add(u)
        color[u] = (color[v] + (w % 2)) % 2
    else:
        q.append([u, v, w])

while len(q) != 0:
    u, v, w = q.popleft()
    if u in finded:
        finded.add(v)
        color[v] = (color[u] + (w % 2)) % 2
    elif v in finded:
        finded.add(u)
        color[u] = (color[v] + (w % 2)) % 2
    else:
        q.append([u, v, w])

for i in range(1, N+1):
    print(color[i])

Fixed submission:
from collections import deque

N = int(input())
tree = [dict() for _ in range(N+1)]
color = [0]*(N+1)
parent = [0]*(N+1)
for _ in range(N-1):
    u, v, w = map(int, input().split())
    w %= 2
    tree[u][v] = w
    tree[v][u] = w

que = deque([1])
while len(que) != 0:
    u = que.pop()
    Rel, c, p = tree[u], color[u], parent[u]
    for v in Rel.keys():
        if v == p:
            continue
        elif Rel[v] == 0:
            color[v] = c
        else:
            color[v] = c^1
        parent[v] = u
        que.append(v)

for c in color[1:]:
    print(c)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N = int(input())
finded = set([1])
color = [0]*(N+1)
q = deque()

for _ in range(N-1):
    u, v, w = map(int, input().split())
    if u in finded:
        finded.add(v)
        color[v] = (color[u] + (w % 2)) % 2
    elif v in finded:
        finded.add(u)
        color[u] = (color[v] + (w % 2)) % 2
    else:
        q.append([u, v, w])

while len(q) != 0:
    u, v, w = q.popleft()
    if u in finded:
        finded.add(v)
        color[v] = (color[u] + (w % 2)) % 2
    elif v in finded:
        finded.add(u)
        color[u] = (color[v] + (w % 2)) % 2
    else:
        q.append([u, v, w])

for i in range(1, N+1):
    print(color[i])

Fixed submission:
from collections import deque

N = int(input())
tree = [dict() for _ in range(N+1)]
color = [0]*(N+1)
parent = [0]*(N+1)
for _ in range(N-1):
    u, v, w = map(int, input().split())
    w %= 2
    tree[u][v] = w
    tree[v][u] = w

que = deque([1])
while len(que) != 0:
    u = que.pop()
    Rel, c, p = tree[u], color[u], parent[u]
    for v in Rel.keys():
        if v == p:
            continue
        elif Rel[v] == 0:
            color[v] = c
        else:
            color[v] = c^1
        parent[v] = u
        que.append(v)

for c in color[1:]:
    print(c)