Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
set0=set([0])
set1 =set()
from collections import deque
ls = deque([])
for _ in range(n-1):
    u, v, w= map(int, input().split())
    ls.append((u-1, v-1, w%2))

ans = [-1] * n
ans[0] = 0
while ls:
    u, v, w = ls.popleft()
    if w == 0: # same
        if u in set0 or v in set0:
            #set0 |= set([u, v])
            set0.add(u)
            set0.add(v)
            ans[u]=0
            ans[v]=0
        elif u in set1 or v in set1:
            #set1 |= set([u, v])
            set1.add(u)
            set1.add(v)
            ans[u] = 1
            ans[v] = 1
        else:
            ls.append((u, v, w))
    else:#different
        if u in set0:
            set1.add(v)
            ans[v] =1
        elif v in set0:
            set1.add(u)
            ans[u] =1
        elif v in set1:
            set0.add(u)
            ans[u] =0
        elif u in set1:
            set0.add(v)
            ans[v] =0
        else:
            ls.append((u, v, w))

for i in ans:
    print(i)

Fixed submission:
n = int(input())
explored = set()
next = [[]for _ in range(n)]

from collections import deque
ls = []
for i in range(n-1):
    u, v, w= map(int, input().split())
    ls.append((u-1, v-1, w%2))
    next[u-1].append((v-1, w%2))
    next[v-1].append((u-1, w%2))

ans = [-1] * n

exploring = deque()
next_que = deque([0])
ans[0]=0
explored.add(0)
while next_que:
    i = next_que.popleft()
    exploring.extend(next[i])
    while exploring:
        a, b = exploring.popleft()
        ans[a] = (ans[i]+b)%2
        list = next[a]
        if a not in explored:
            next_que.append(a)
        explored.add(a)


for i in ans:
    print(i)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
set0=set([0])
set1 =set()
from collections import deque
ls = deque([])
for _ in range(n-1):
    u, v, w= map(int, input().split())
    ls.append((u-1, v-1, w%2))

ans = [-1] * n
ans[0] = 0
while ls:
    u, v, w = ls.popleft()
    if w == 0: # same
        if u in set0 or v in set0:
            set0 |= set([u, v])
            ans[u]=0
            ans[v]=0
        elif u in set1 or v in set1:
            set1 |= set([u, v])
            ans[u] = 1
            ans[v] = 1
        else:
            ls.append((u, v, w))
    else:#different
        if u in set0:
            set1.add(v)
            ans[v] =1
        elif v in set0:
            set1.add(u)
            ans[u] =1
        elif v in set1:
            set0.add(u)
            ans[u] =0
        elif u in set1:
            set0.add(v)
            ans[v] =0
        else:
            ls.append((u, v, w))

for i in ans:
    print(i)

Fixed submission:
n = int(input())
explored = set()
next = [[]for _ in range(n)]

from collections import deque
ls = []
for i in range(n-1):
    u, v, w= map(int, input().split())
    ls.append((u-1, v-1, w%2))
    next[u-1].append((v-1, w%2))
    next[v-1].append((u-1, w%2))

ans = [-1] * n

exploring = deque()
next_que = deque([0])
ans[0]=0
explored.add(0)
while next_que:
    i = next_que.popleft()
    exploring.extend(next[i])
    while exploring:
        a, b = exploring.popleft()
        ans[a] = (ans[i]+b)%2
        list = next[a]
        if a not in explored:
            next_que.append(a)
        explored.add(a)


for i in ans:
    print(i)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
set0=set([0])
set1 =set()
from collections import deque
ls = deque([])
for _ in range(n-1):
    u, v, w= map(int, input().split())
    ls.append((u-1, v-1, w%2))

ans = [-1] * n
ans[0] = 0
while ls:
    u, v, w = ls.popleft()
    if w == 0: # same
        if u in set0 or v in set0:
            set0 |= set([u, v])
            ans[u]=0
            ans[v]=0
        elif u in set1 or v in set1:
            set1 |= set([u, v])
            ans[u] = 1
            ans[v] = 1
        else:
            ls.append((u, v, w))
    else:#different
        if u in set0:
            set1.add(v)
            ans[v] =1
        elif v in set0:
            set1.add(u)
            ans[u] =1
        elif v in set1:
            set0.add(u)
            ans[u] =0
        elif u in set1:
            set0.add(v)
            ans[v] =0
        else:
            ls.append((u, v, w))

for i in ans:
    print(i)

Fixed submission:
n = int(input())
explored = set()
next = [[]for _ in range(n)]

from collections import deque
ls = []
for i in range(n-1):
    u, v, w= map(int, input().split())
    ls.append((u-1, v-1, w%2))
    next[u-1].append((v-1, w%2))
    next[v-1].append((u-1, w%2))

ans = [-1] * n

exploring = deque()
next_que = deque([0])
ans[0]=0
explored.add(0)
while next_que:
    i = next_que.popleft()
    exploring.extend(next[i])
    while exploring:
        a, b = exploring.popleft()
        ans[a] = (ans[i]+b)%2
        list = next[a]
        if a not in explored:
            next_que.append(a)
        explored.add(a)


for i in ans:
    print(i)