Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import copy


class UnionFind:
    __slots__ = ['id', 'sz']

    def __init__(self, n):
        self.id = [-1] * n
        self.sz = n

    def root(self, x):
        if self.id[x] < 0:
            return x
        else:
            self.id[x] = self.root(self.id[x])
            return self.id[x]

    def find(self, x, y):
        return uf.root(x) == uf.root(y)

    def union(self, x, y):
        s1, s2 = self.root(x), self.root(y)
        if s1 != s2:
            if self.id[s1] <= self.id[s2]:
                self.id[s1] += self.id[s2]
                self.id[s2] = s1
            else:
                self.id[s2] += self.id[s1]
                self.id[s1] = s2
            self.sz -= 1
            return True
        return False


n = int(input())
uvw = [list(map(int, input().split())) for i in range(n-1)]
groop = [-1]*n
groop[0] = 1
l = [0, 1]
uf = UnionFind(n)
uvw.sort()
while uvw:

    nextlist = []
    for u,v,w in uvw:
        if groop[u-1] >= 0:
            if w % 2:
                groop[v-1] = l[l.index(groop[u-1])-1]
            else:
                groop[v-1] = groop[u-1]
        elif groop[v-1] >= 0:
            if w % 2:
                groop[u-1] = l[l.index(groop[v-1])-1]
            else:
                groop[u-1] = groop[v-1]
        else:
            nextlist.append((u, v, w))
    uvw = copy.deepcopy(nextlist)
for g in groop:
    print(1 if g else 0)


Fixed submission:
#!/usr/bin/env python3
n=int(input())
uvw=[list(map(int,input().split())) for i in range(n-1)]
graph=[[] for i in range(n+1)]
color=[-1]*(n+1)
for u,v,w in uvw:
    graph[u].append((v,w))
    graph[v].append((u,w))

color[1]=1
ne=[1]
while ne:
    t=ne.pop()
    for v,w in graph[t]:
        if color[v]==-1:
            ne.append(v)
            color[v]=color[t] if w%2==0 else (color[t]+1)%2

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

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
import copy


class UnionFind:
    __slots__ = ['id', 'sz']

    def __init__(self, n):
        self.id = [-1] * n
        self.sz = n

    def root(self, x):
        if self.id[x] < 0:
            return x
        else:
            self.id[x] = self.root(self.id[x])
            return self.id[x]

    def find(self, x, y):
        return uf.root(x) == uf.root(y)

    def union(self, x, y):
        s1, s2 = self.root(x), self.root(y)
        if s1 != s2:
            if self.id[s1] <= self.id[s2]:
                self.id[s1] += self.id[s2]
                self.id[s2] = s1
            else:
                self.id[s2] += self.id[s1]
                self.id[s1] = s2
            self.sz -= 1
            return True
        return False


n = int(input())
uvw = [list(map(int, input().split())) for i in range(n-1)]
groop = [-1]*n
groop[0] = 1
l = [0, 1]
uf = UnionFind(n)
uvw.sort()
while uvw:

    nextlist = []
    for i in range(n-1):
        u, v, w = uvw[i]
        if groop[u-1] >= 0:
            if w % 2:
                groop[v-1] = l[l.index(groop[u-1])-1]
            else:
                groop[v-1] = groop[u-1]
        elif groop[v-1] >= 0:
            if w % 2:
                groop[u-1] = l[l.index(groop[v-1])-1]
            else:
                groop[u-1] = groop[v-1]
        else:
            nextlist.append((u, v, w))
    uvw = copy.deepcopy(nextlist)
for g in groop:
    print(1 if g else 0)


Fixed submission:
#!/usr/bin/env python3
n=int(input())
uvw=[list(map(int,input().split())) for i in range(n-1)]
graph=[[] for i in range(n+1)]
color=[-1]*(n+1)
for u,v,w in uvw:
    graph[u].append((v,w))
    graph[v].append((u,w))

color[1]=1
ne=[1]
while ne:
    t=ne.pop()
    for v,w in graph[t]:
        if color[v]==-1:
            ne.append(v)
            color[v]=color[t] if w%2==0 else (color[t]+1)%2

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