Buggy python submission (verdict: Runtime Error):
import sys

input = sys.stdin.readline

def hantei(i, oya=-1):
    if ans[i] != -1:
        return ans[i]
    for u, w in pea[i]:
        if u == oya: continue
        if hantei(u, i):
            re = ans[i] = (hantei(u, i) + w) % 2
            return re

n = int(input())
pea = [[] for _ in range(n + 1)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    w %= 2
    pea[u].append([v, w])
    pea[v].append([u, w])
ans = [-1] * (n + 1)
ans[1] = 1
for i in range(1, n + 1):
    print(hantei(i))


Fixed submission:
n = int(input())
pea = [[] for _ in range(n + 1)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    w %= 2
    pea[u].append([v, w])
    pea[v].append([u, w])
ans = [-1] * (n + 1)
ans[1]=1
nodes=[1]
used=set()
used.add(1)
ansi=1
while nodes:
    nn=[]
    for i in nodes:
        ansi=ans[i]
        for u,w in pea[i]:
            if u in used:continue
            ans[u]=(ansi+w)%2
            used.add(u)
            nn+=[u]
    nodes=nn
for x in ans[1:]:
    print(x)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

input = sys.stdin.readline

def f(n):
    uvw = [list(map(int, input().split())) for _ in range(n - 1)]
    ans = [-1] * (n + 1)
    ans[uvw[0][0]] = True
    while uvw:
        nuvw = []
        for u, v, w in uvw:
            if ans[u] != -1:
                if w % 2:
                    ans[v] = not ans[u]
                else:
                    ans[v] = ans[u]
            elif ans[v] != -1:
                if w % 2:
                    ans[u] = not ans[v]
                else:
                    ans[u] = ans[v]
            else:
                nuvw.append([u, v, w])
        uvw = nuvw
    for x in ans[1:]:
        print(x * 1)

n = int(input())
f(n)


Fixed submission:
n = int(input())
pea = [[] for _ in range(n + 1)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    w %= 2
    pea[u].append([v, w])
    pea[v].append([u, w])
ans = [-1] * (n + 1)
ans[1]=1
nodes=[1]
used=set()
used.add(1)
ansi=1
while nodes:
    nn=[]
    for i in nodes:
        ansi=ans[i]
        for u,w in pea[i]:
            if u in used:continue
            ans[u]=(ansi+w)%2
            used.add(u)
            nn+=[u]
    nodes=nn
for x in ans[1:]:
    print(x)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

input = sys.stdin.readline

def f(n):
    uvw = [list(map(int, input().split())) for _ in range(n - 1)]
    ans = [-1] * (n + 1)
    ans[uvw[0][0]] = True
    while uvw:
        nuvw = []
        for u, v, w in uvw:
            if ans[u] != -1:
                if w % 2:
                    ans[v] = not ans[u]
                else:
                    ans[v] = ans[u]
            elif ans[v] != -1:
                if w % 2:
                    ans[u] = not ans[v]
                else:
                    ans[u] = ans[v]
            else:
                nuvw.append([u, v, w])
        uvw = nuvw
    for x in ans[1:]:
        print(x * 1)

n = int(input())
f(n)


Fixed submission:
n = int(input())
pea = [[] for _ in range(n + 1)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    w %= 2
    pea[u].append([v, w])
    pea[v].append([u, w])
ans = [-1] * (n + 1)
ans[1]=1
nodes=[1]
used=set()
used.add(1)
ansi=1
while nodes:
    nn=[]
    for i in nodes:
        ansi=ans[i]
        for u,w in pea[i]:
            if u in used:continue
            ans[u]=(ansi+w)%2
            used.add(u)
            nn+=[u]
    nodes=nn
for x in ans[1:]:
    print(x)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
uvw = [list(map(int, input().split())) for _ in range(n - 1)]
ans = [-1] * (n + 1)
ans[1] = True
while uvw:
    nuvw = []
    for u, v, w in uvw:
        if ans[u] != -1:
            if w % 2:
                ans[v] = not ans[u]
            else:
                ans[v] = ans[u]
        elif ans[v] != -1:
            if w % 2:
                ans[u] = not ans[v]
            else:
                ans[u] = ans[v]
        else:
            nuvw.append([u, v, w])
    uvw = nuvw
for x in ans[1:]:
    print(x * 1)


Fixed submission:
n = int(input())
pea = [[] for _ in range(n + 1)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    w %= 2
    pea[u].append([v, w])
    pea[v].append([u, w])
ans = [-1] * (n + 1)
ans[1]=1
nodes=[1]
used=set()
used.add(1)
ansi=1
while nodes:
    nn=[]
    for i in nodes:
        ansi=ans[i]
        for u,w in pea[i]:
            if u in used:continue
            ans[u]=(ansi+w)%2
            used.add(u)
            nn+=[u]
    nodes=nn
for x in ans[1:]:
    print(x)