Problem:
 Score : 400 points    Problem Statement We have a tree with N vertices numbered 1 to N .
The i -th edge in the tree connects Vertex u_i and Vertex v_i , and its length is w_i .
Your objective is to paint each vertex in the tree white or black (it is fine to paint all vertices the same color) so that the following condition is satisfied:   For any two vertices painted in the same color, the distance between them is an even number.   Find a coloring of the vertices that satisfies the condition and print it. It can be proved that at least one such coloring exists under the constraints of this problem.      Constraints  All values in input are integers.  1 \leq N \leq 10^5  1 \leq u_i < v_i \leq N  1 \leq w_i \leq 10^9         Input Input is given from Standard Input in the following format:  N  u_1  v_1  w_1  u_2  v_2  w_2  .  .  .  u_{N - 1}  v_{N - 1}  w_{N - 1}       Output Print a coloring of the vertices that satisfies the condition, in N lines.
The i -th line should contain 0 if Vertex i is painted white and 1 if it is painted black.  If there are multiple colorings that satisfy the condition, any of them will be accepted.        Sample Input 1 3
1 2 2
2 3 1      Sample Output 1 0
0
1       Sample Input 2 5
2 5 2
2 3 10
1 3 8
3 4 2      Sample Output 2 1
0
1
0
1

Solution (Python):

import sys
import math
from collections import defaultdict


sys.setrecursionlimit(10**7)
def input():
  return sys.stdin.readline()[:-1]


mod = 10**9 + 7


def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
  read_all = [tuple(map(int, input().split())) for _ in range(N)]
  return map(list,zip(*read_all))



N = I()
u,v,w = Line(N-1)

c = [[] for _ in range(N)]
for i in range(N-1):
  c[u[i]-1].append([v[i]-1, w[i]])
  c[v[i]-1].append([u[i]-1, w[i]])

d=[-1]*N
v=0
check_visited = [False]*N
check_visited[v] = True
S = [v]
d[v]=0
while S:
  v1 = S.pop()
  for i in c[v1]:
    if check_visited[i[0]] == False:
      check_visited[i[0]] = True
      d[i[0]] = d[v1] + i[1]
      S.append(i[0])

for i in range(N):
  if d[i]%2==0:
    print('0')
  else:
    print('1')

Solution (Python):
N = int(input())
S = [[] for i in range(N)]
for i in range(N-1):
  u, v, w = map(int, input().split())
  S[u-1].append((v-1, w))
  S[v-1].append((u-1, w))

C = [-1] * N
Q = [0]
C[0] = 0
while Q:
  p = Q.pop()
  Ns = S[p]
  
  for v, w in Ns:
    if C[v] < 0:
      C[v] = (C[p]+w)%2
      Q.append(v)
for i in range(N):
  print(C[i])

Solution (Python):

import sys


def solve(N: int, u: "List[int]", v: "List[int]", w: "List[int]"):
    A = [0] * N
    G = [[] for _ in range(N)]
    for i in range(N-1):
        G[u[i]-1].append((v[i]-1, w[i]))
        G[v[i]-1].append((u[i]-1, w[i]))
    stack = [0]
    visited = [0] * N
    while(len(stack) > 0):
        vv = stack.pop()
        visited[vv] = 1
        for k in G[vv]:
            if visited[k[0]] == 0:
                if k[1] % 2 == 0:
                    A[k[0]] = A[vv]
                else:
                    A[k[0]] = abs(A[vv] - 1)
                stack.append(k[0])
    for a in A:
        print(a)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    u = [int()] * (N-1)  
    v = [int()] * (N-1)  
    w = [int()] * (N-1)  
    for i in range(N-1):
        u[i] = int(next(tokens))
        v[i] = int(next(tokens))
        w[i] = int(next(tokens))
    solve(N, u, v, w)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(20000000)


def dfs(node, distance):
    for next_node, edge_size in G[node]:
        if ans[next_node] == -1:
            ans[next_node] = ((distance+edge_size) % 2)
            dfs(next_node, distance+edge_size)


N = int(input())

G = [[] for i in range(N)]
for i in range(N-1):
    u, v, w = map(int, input().split())
    G[u-1].append([v-1, w])
    G[v-1].append([u-1, w])

ans = [-1]*N
dfs(0, 0)
print(*ans, sep='\n')

Solution (Python):
import sys
sys.setrecursionlimit(100000000)

N = int(input())

q = {}

for i in range(N):
    q[i] = []

for _ in range(N - 1):
    _u, _v, _w = map(int, input().split())

    q[_u - 1].append((_v - 1, _w))
    q[_v - 1].append((_u - 1, _w))

ans = [None] * N
ans[0] = 0


def search(x, parent_num):
    for s in q[x]:
        if ans[s[0]] is None:
            ans[s[0]] = (parent_num + s[1]) % 2
            search(s[0], ans[s[0]])


search(0, 0)

for i in range(N):
    print(ans[i])

Solution (Python):
import numpy as np
import sys
INF = float('inf')
sys.setrecursionlimit(10**5)
max = 10**5+5

def dk(start,node,edge):
  ans = [INF] * node
  ans[0] = 0
  q = [start]
  
  while(len(q) != 0):
    i = q.pop()
    for j, cost in edge[i]:
      if(ans[j] != INF):
        continue
      ans[j] = (ans[i] + cost)%2
      q.append(j)
  return ans


N = int(input())

E = [[] for _ in [0]*(max)]

for i in range(N-1):
  a,b,w = (int(x) for x in input().split())
  E[a-1].append((b-1, w))
  E[b-1].append((a-1, w))


ans = dk(0, N, E)


print(*ans, sep = "\n")

Solution (Python):

import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
import math

N = int(input())
uvw = []
for i in range(N - 1):
	uvw.append(list(map(int, input().split())))

tree = [dict() for i in range(N)]
for i in range(N):
	tree[i]["edge"] = []
	tree[i]["color"] = -1
for i in range(N - 1):
	tree[uvw[i][0] - 1]["edge"].append([uvw[i][0], uvw[i][1], uvw[i][2]])
	tree[uvw[i][1] - 1]["edge"].append([uvw[i][1], uvw[i][0], uvw[i][2]])

tansaku = deque([1])
tree[0]["color"] = 0
while len(tansaku) > 0:
	node = tansaku.pop()

	if len(tree[node - 1]["edge"]) == 0:
		continue
	for edge in tree[node - 1]["edge"]:
		if edge[2] % 2 == 0:
			if tree[edge[1] - 1]["color"] == -1:
				tree[edge[1] - 1]["color"] = copy.deepcopy(tree[node - 1]["color"])
				tansaku.append(edge[1])
		else:
			if tree[edge[1] - 1]["color"] == -1:
				tree[edge[1] - 1]["color"] = tree[node - 1]["color"] ^ 1
				tansaku.append(edge[1])
for i in range(N):
	print(tree[i]["color"])

Solution (Python):
n = int(input())
links = [set() for _ in range(n)]

for _ in range(n - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    w %= 2
    links[v].add((u, w))
    links[u].add((v, w))

colors = [-1] * n
q = [(0, 0, -1)]

while q:
    v, c, p = q.pop()
    colors[v] = c
    for u, w in links[v]:
        if u == p:
            continue
        q.append((u, c ^ w, v))

print('\n'.join(map(str, colors)))

Solution (Python):
import sys
sys.setrecursionlimit(1000000)
n=int(input())
graph=[[] for i in range(n)]
color=[-1]*n
for i in range(n-1):
    a,b,c=map(int,input().split())
    graph[a-1].append([b-1,c%2])
    graph[b-1].append([a-1,c%2])
def coloring(pre,c):
  color[pre]=c
  for i,j in graph[pre]:
    if color[i]<0:
      coloring(i,color[pre]^j)
coloring(0,1)
for i in color:
  print(i)

Solution (Python):
import sys
sys.setrecursionlimit(1000000)

def dfs(graph, node, dist):
    if graph[node-1][2] == 1:
        return
    else:
        graph[node-1][2] = 1
        graph[node-1][3] += dist
        if graph[node-1][3] % 2 == 0:
            graph[node-1][4] = 1
        else:
            pass
        ll = graph[node-1][0]
        for i in range(len(ll)):
            dfs(graph, ll[i], graph[node-1][3] + graph[node-1][1][i]);

def main():
    n = int(input())
    edge = []
    for i in range(n-1): edge.append(list(map(int,input().split())))



    graph = [[[],[],0,0,0] for i in range(n)]
    for i in range(n-1):
        node1 = edge[i][0]
        node2 = edge[i][1]
        weight = edge[i][2]
        graph[node1-1][0].append(node2)
        graph[node2-1][0].append(node1)
        graph[node1-1][1].append(weight)
        graph[node2-1][1].append(weight)
    
    dfs(graph, 1, 0)
    for i in range(n):
        print(graph[i][4])

if __name__ == "__main__":
    main()

Solution (Python):
import sys
sys.setrecursionlimit(100000)

N = int(input())
W = [list(map(int, input().split())) for _ in range(N-1)]
nodes = [[] for _ in range(N+1)]
for w in W:
    nodes[w[0]].append((w[1], w[2]))
    nodes[w[1]].append((w[0], w[2]))
ans = [-1] * (N+1)
ans[1] = 0
def search(num, root, dis):
    for node in nodes[num]:
        sum_d = dis
        if node[0] != root:
            idx = node[0]
            sum_d += node[1]%2
            if sum_d%2 != 0:
                ans[idx] = 1
            else:
                ans[idx] = 0
            search(idx, num, sum_d)

search(1, 0, 0)
for answer in ans[1:]:
    print(answer)

Solution (Python):
import sys

sys.setrecursionlimit(2**31-1)

def paint(point, color):
    colors[point] = color

    
    for friend, w in reration[point]:
        if not colors[friend]:  
            
            nextcolor = color*(-1)**w
            paint(friend, nextcolor)


N = int(input())
reration = [[] for i in range(N)]  
colors = ["" for i in range(N)]  
for i in range(N-1):
    U, V, W = map(int, input().split())
    reration[U-1].append((V-1, W % 2))
    reration[V-1].append((U-1, W % 2))



for i in range(N):
    if not colors[i]:
        paint(i, 1)
for co in colors:
    print((co+1)//2)

Solution (Python):
import sys
from collections import deque
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = lambda x: sys.stdout.write(str(x))
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = False

def main():

    N = II()
    t = [-1] * N


    E = [  [] for _ in range(N)]
 
    reached = [False] * N
    for _ in range(N-1):
        u,v,w = LMIIS()
        u -=1
        v -=1
        E[u].append((v,w))
        E[v].append((u,w))
    dbg(E)
    t[E[0][0][0]] = 0

     
    
    
    
    
    
                

    
    reached = [False] * N
    q = []
    q.append(E[0][0][0])
    while q:
        u = q.pop()
        reached[u] = True
        for v,w in E[u]:
            if reached[v] != True and  t[v] == -1:
                t[v] = t[u] if w % 2 == 0 else t[u]^1
                q.append(v)

                



    for i in t:
        write(i)
        write(' ')






    pass
if __name__ == '__main__':
    main()

Solution (Python):
import sys
from collections import deque
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = lambda x: sys.stdout.write(str(x))
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = False

def main():

    N = II()
    t = [-1] * N


    E = [  [] for _ in range(N)]
 
    reached = [False] * N
    for _ in range(N-1):
        u,v,w = LMIIS()
        u -=1
        v -=1
        E[u].append((v,w))
        E[v].append((u,w))
    dbg(E)
    t[E[0][0][0]] = 0

     
    
    
    
    
    
                

    
    reached = [False] * N
    q = deque()
    q.append(E[0][0][0])
    while q:
        u = q.pop()
        reached[u] = True
        for v,w in E[u]:
            if reached[v] != True and  t[v] == -1:
                t[v] = t[u] if w % 2 == 0 else t[u]^1
                q.append(v)

                



    for i in t:
        write(i)
        write(' ')






    pass
if __name__ == '__main__':
    main()

Solution (Python):
import sys
from collections import deque
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = lambda x: sys.stdout.write(str(x))
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = False

def main():

    N = II()
    t = [-1] * N


    E = [  [] for _ in range(N)]
 
    reached = [False] * N
    for _ in range(N-1):
        u,v,w = LMIIS()
        u -=1
        v -=1
        E[u].append((v,w))
        E[v].append((u,w))
    dbg(E)
    t[E[0][0][0]] = 0

     
    
    
    
    
    
                

    
    reached = [False] * N
    q = deque()
    q.append(E[0][0][0])
    while q:
        u = q.pop()
        reached[u] = True
        for v,w in E[u]:
            if reached[v] != True and  t[v] == -1:
                t[v] = t[u] if w % 2 == 0 else t[u]^1
                q.append(v)

                



    for i in t:
        write(i)
        write(' ')






    pass
if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = lambda x: sys.stdout.write(str(x))
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = False

def main():

    N = II()
    t = [-1] * N


    E = [  [] for _ in range(N)]
 
    reached = [False] * N
    for _ in range(N-1):
        u,v,w = LMIIS()
        u -=1
        v -=1
        E[u].append((v,w))
        E[v].append((u,w))
    dbg(E)
    t[E[0][0][0]] = 0

     
    
    
    
    
    
                

    
    reached = [False] * N
    q = [E[0][0][0]]
    while q:
        u = q.pop()
        reached[u] = True
        for v,w in E[u]:
            if reached[v] != True and  t[v] == -1:
                t[v] = t[u] if w % 2 == 0 else t[u]^1
                q.append(v)

                



    for i in t:
        write(i)
        write(' ')






    pass
if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = lambda x: sys.stdout.write(str(x))
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = False

def main():

    N = II()
    t = [-1] * N


    E = [  [] for _ in range(N)]
 
    reached = [False] * N
    for _ in range(N-1):
        u,v,w = LMIIS()
        u -=1
        v -=1
        E[u].append((v,w))
        E[v].append((u,w))
    dbg(E)
    t[E[0][0][0]] = 0

     
    def search(u):
        for v,w in E[u]:
            if t[v] == -1:
                t[v] = t[u] if w % 2 == 0 else t[u]^1
                search(v)
                

    search(E[0][0][0])

    for i in t:
        write(i)
        write(' ')






    pass
if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = lambda x: sys.stdout.write(str(x))
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = False

def main():

    N = II()
    t = [-1] * N


    E = [  [] for _ in range(N)]
 
    reached = [False] * N
    for _ in range(N-1):
        u,v,w = LMIIS()
        u -=1
        v -=1
        E[u].append((v,w))
        E[v].append((u,w))
    dbg(E)
    t[E[0][0][0]] = 0

     
    def search(u):
        for v,w in E[u]:
            if t[v] == -1:
                t[v] = t[u] if w % 2 == 0 else t[u]^1
                search(v)
                

    search(E[0][0][0])

    for i in t:
        write(i)
        write(' ')






    pass
if __name__ == '__main__':
    main()

Solution (Python):

N = int(input())
UVW = [list(map(int,input().split())) for _ in [0]*(N-1)]

E = [{} for _ in [0]*N]
for u,v,c in UVW:
    E[u-1][v-1] = c
    E[v-1][u-1] = c

def dist_dfs_tree(N,E,start):
    d = [-1]*N
    d[start] = 0
    q = [start]
    while q:
        i = q.pop()
        ci = d[i]
        for j,cj in E[i].items():
            if d[j] !=-1:continue
            d[j] = ci+cj
            q.append(j)
    return d

d = dist_dfs_tree(N,E,0)
print(*[l%2 for l in d],sep="\n")

Solution (Python):
import heapq
N = int(input())
UVW = [list(map(int,input().split())) for _ in [0]*(N-1)]

E = [{} for _ in [0]*N]
for u,v,c in UVW:
    E[u-1][v-1] = c
    E[v-1][u-1] = c

def Dijkstra(N,E,start):
    d = [-1]*N
    q = [(0,start)]
    while q:
        ci,i = heapq.heappop(q)
        if d[i]!= -1:continue
        d[i] = ci
        for j,cj in E[i].items():
            if d[j]!=-1:continue
            heapq.heappush(q,(ci+cj,j))
    return d

d = Dijkstra(N,E,0)
print(*[l%2 for l in d],sep="\n")

Solution (Python):
n = int(input())
graph = [[] for _ in range(n + 1)]

for i in range(n - 1):
    a, b, w = map(int, input().split())
    graph[a].append((b, w))
    graph[b].append((a, w))

que = [(1, 0)]

colors = [None] * (n + 1)
while que:
    now, color = que.pop(0)
    colors[now] = color

    for b, w in graph[now]:
        if colors[b] is not None:
            continue
        que.append((b, (color + (w & 1)) & 1))
print("\n".join(map(str, colors[1:])))

Solution (Python):



def genTree(N, E):
    E2 = [set() for _ in range(N+1)]
    for u, v, w in E:
        E2[u].add((v, w))
        E2[v].add((u, w))

    V = [None]*(N+1)
    Q = [1]

    while len(Q) > 0:
        q = Q.pop()
        s = list(filter(lambda x: V[x[0]] == None, E2[q]))

        V[q] = s
        Q.extend(map(lambda x: x[0], s))

    return V


def main():
    N = int(input())
    E = [tuple(map(int, input().split())) for _ in range(N-1)]

    V = genTree(N, E)
    X = [None]*(N+1)

    X[1] = 0
    Q = [1]
    
    while len(Q) > 0:
        q = Q.pop()
        x = X[q]

        for u, w in V[q]:
            X[u] = (x + w)%2

        Q.extend(map(lambda x: x[0], V[q]))

    for x in X[1:]:
        print(x)

        

if __name__ == '__main__':
    main()

Solution (Python):
from Queue import Queue
n = int(raw_input())
adjList = [[] for _ in range(n+1)]
colors = [-1] * (n+1)
start = None
for i in range(n-1):
    u, v, w = map(int, raw_input().split())
    adjList[u].append((v, w))
    adjList[v].append((u, w))
    if start is None:
        start = u
def bfs(adjList, colors, start):
    colors[start] = 0
    q = Queue()
    q.put(start)
    while not q.empty():
        u = q.get()
        for adj, w in adjList[u]:
            if colors[adj] == -1:
                if w % 2 == 0:
                    colors[adj] = colors[u]
                else:
                    colors[adj] = colors[u]^1
                q.put(adj)
bfs(adjList, colors, start)
for i in range(1, n+1):
    if colors[i] != -1:
        print colors[i]

Solution (Python):

from collections import deque

n = int(input())

lw = [[] for _ in range(n+1)]
for i in range(n-1):
    u,v,w = map(int,input().split())
    lw[u].append((v,w))
    lw[v].append((u,w))



ans = [-1] * (n+1)
q = deque()
ans[1] = 0
q.append(1)
while q:
    r = q.popleft()
    prev = ans[r]
    
    for x in lw[r]:
        
        y = x[0]
        if ans[y] >= 0:
            continue
        q.append(y)
        if x[1]%2==0:
            ans[y] = ans[r]
        else:
            ans[y] = 1 - ans[r]

for x in ans[1:]:
    print(x)

Solution (Python):
n = int(input())
d = {}
for i in range(n-1):
    u,v,w = map(int,input().split())
    if u-1 not in d:
        d[u-1] = []
        d[u-1].append([v-1,w])
    else:
        d[u-1].append([v-1,w])
    if v-1 not in d:
        d[v-1] = []
        d[v-1].append([u-1,w])
    else:
        d[v-1].append([u-1,w])

ch = [False]*n
res = [-1]*n
q =[0]
res[0] = 0
ch[0] = True
while q != []:
     t = q.pop(0)
     for l in d[t]:
         if ch[l[0]] == True:
             continue
         else:
             ch[l[0]] = True
             res[l[0]] = res[t] + l[1]
             q.append(l[0])
for i in range(n):
    print(res[i]%2)

Solution (Python):
n = int(input())
d = {}
for i in range(n-1):
    u,v,w = map(int,input().split())
    if u-1 not in d:
        d[u-1] = []
        d[u-1].append([v-1,w])
    else:
        d[u-1].append([v-1,w])
    if v-1 not in d:
        d[v-1] = []
        d[v-1].append([u-1,w])
    else:
        d[v-1].append([u-1,w])

ch = [False]*n
res = [-1]*n
q =[0]
res[0] = 0
ch[0] = True
while q != []:
     t = q.pop(0)
     for l in d[t]:
         if ch[l[0]] == True:
             continue
         else:
             ch[l[0]] = True
             res[l[0]] = res[t] + l[1]
             q.append(l[0])
for i in range(n):
    print(res[i]%2)

Solution (Python):
import sys
input = sys.stdin.readline

n = int(input())
p = [list(map(int, input().split())) for _ in range(n-1)]

ans = [-1] * (n+1)
lw = [[] for i in range(n+1)]
for path in p:
    lw[path[0]].append((path[1],path[2]))
    lw[path[1]].append((path[0],path[2]))

from _collections import deque
q = deque()
ans[1] = 0
q.append(1)
while q:
    r = q.popleft()
    prev = ans[r]
    assert prev >= 0
    for xx in lw[r]:
        nx = xx[0]
        if ans[nx] >= 0:
            continue  
        q.append(nx)
        if xx[1] % 2 == 0:
            ans[xx[0]] = ans[r]
        else:
            ans[xx[0]] = 1 - ans[r]

for x in ans[1:]:
    print(x)

Solution (Python):
N = int(input())
edges = [list(map(int, input().split())) for _ in range(N - 1)]

graph = [[] for _ in range(N + 1)]
for u, v, w in edges:
    graph[u].append((v, w))
    graph[v].append((u, w))

edges.sort()
color = [-1] * (N + 1)
color[1] = 0
que = [1]

while que:
    u = que.pop()
    
    for v, w in graph[u]:
        if color[v] != -1:
            continue

        if w % 2 == 0:
            color[v] = color[u]
        else:
            color[v] = color[u] ^ 1
            
        que.append(v)
        
print(*color[1:], sep='\n')

Solution (Python):
import sys
from collections import defaultdict

sys.setrecursionlimit(100000)
input = sys.stdin.readline

N = int(input())
G = defaultdict(list)
for _ in range(N - 1):
    u, v, w = map(int, input().split())
    G[u - 1].append((v - 1, w))
    G[v - 1].append((u - 1, w))
res = [None] * N


def dfs(v, c):
    res[v] = c
    for u, w in G[v]:
        if res[u] is not None:
            continue
        if w % 2 == 0:
            dfs(u, c)
        else:
            dfs(u, 1 - c)


dfs(0, 0)
for x in res:
    print(x)

Solution (Python):
import sys
from queue import Queue


def main():
    input = sys.stdin.readline
    N = int(input())
    tree = [[] for _ in range(N)]
    for _ in range(N-1):
        u, v, w = map(int, input().split())
        tree[u-1].append([v-1, w])
        tree[v-1].append([u-1, w])

    color = [None] * N
    v_que = Queue()
    v_que.put(0)
    
    c = True
    c_que = Queue()
    c_que.put(c)

    while not v_que.empty():
        v = v_que.get()
        c = c_que.get()
        if color[v] is not None:
            continue
        color[v] = c
        for e in tree[v]:
            v_que.put(e[0])
            c_que.put(c if e[1] % 2 == 0 else not c)

    for c in color:
        print(int(c))


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

n = int(input())

es = [[] for _ in range(n)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    es[u].append((v, w))
    es[v].append((u, w))

s = 0
ans = [None] * n
ans[s] = 0

dq = deque()
dq.append(s)

while dq:
    v = dq.popleft()
    for nv, w in es[v]:
        if ans[nv] is None:
            ans[nv] = (ans[v] + w) % 2
            dq.append(nv)
print(*ans, sep='\n')

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque

N = int(input())
adj = [[] for i in range(N)]
for _ in range(N-1):
    u, v, w = map(int, input().split())
    adj[u-1].append((v-1,w))
    adj[v-1].append((u-1,w))

color = [0]*N
visited = [0]*N
visited[0] = 1
q = deque()
q.append((0,0))

while q:
    v, l = q.pop()
    visited[v] = 1
    if l % 2 == 1:
        color[v] = 1
    for u, w in adj[v]:
        if not visited[u]:
            q.append((u, l+w))

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

Solution (Python):
from collections import deque

N = int(input())
adj = [[] for i in range(N)]
for _ in range(N-1):
    u, v, w = map(int, input().split())
    adj[u-1].append((v-1,w))
    adj[v-1].append((u-1,w))

color = [0]*N
visited = [0]*N
visited[0] = 1
q = deque()
q.append((0,0))

while q:
    v, l = q.pop()
    visited[v] = 1
    if l % 2 == 1:
        color[v] = 1
    for u, w in adj[v]:
        if not visited[u]:
            q.append((u, l+w))

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

Solution (Python):
import sys
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
MAX=10**5+5
tree=[[] for _ in range(MAX)]
ans = [-1]*(MAX)
def dfs(x,weight):
    if(ans[x]>=0):
        return
    ans[x]=weight&1
    for i,w in tree[x]:
        dfs(i,weight+w)

def resolve():
    n =int(input())
    for i in range(n-1):
        u,v,w = map(int,input().split())
        tree[u].append((v,w))
        tree[v].append((u,w))
    dfs(1,0)
    
    for i in range(1,n+1):
        print(ans[i])


if __name__ == "__main__":
    resolve()

Solution (Python):
from collections import deque
N=int(input())
G=[[] for i in range(N)]
for i in range(N-1):
    u,v,w=map(int,input().split())
    u,v=u-1,v-1
    w%=2
    G[u].append([u,v,w])
    G[v].append([v,u,w])
color=[-1]*N
color[0]=0
stack=deque(G[0])
while(stack):
    u,v,w=stack.pop()
    if color[v]!=-1:
        continue
    if w==1:
        color[v]=1-color[u]
    else:
        color[v]=color[u]
    stack.extend(G[v])
[print(i) for i in color]

Solution (Python):
import sys
sys.setrecursionlimit(1000000)

n = int(input())
lis = [[] for i in range(n)]

for i in range(n-1):
    u,v,w = map(int,input().split())
    lis[u-1].append((v-1,w%2))
    lis[v-1].append((u-1,w%2))

li = [-1] * n

def calc(i,c):
    li[i] = c
    for j, w in lis[i]:
        if li[j] < 0:
            calc(j, li[i]^w)

calc(0,0)

for d in li:
    print(d)

Solution (Python):
N = int(input())
INF = float('inf')
dis = [INF]*N
dis[0] = 0
G = [[] for _ in range(N)]
for _ in range(N-1):
	u, v, w = map(int, input().split())
	u -= 1
	v -= 1
	G[u].append((v, w))
	G[v].append((u, w))

queue = [0]

while queue:
	u = queue.pop()
	for v, w in G[u]:
		if dis[v] > dis[u] + w:
			queue.append(v)
			dis[v] = dis[u] + w

for i in dis:
	print(i % 2)

Solution (Python):
import sys


sys.setrecursionlimit(100000)


def main():
    N = int(input())

    E = {i:{} for i in range(N)}
    for _ in range(N-1):
        u, v, w = map(int, input().split())
        E[u-1][v-1] = w % 2
        E[v-1][u-1] = w % 2
    
    ans = [-1] * N
    def dfs(x):
        for y in E[x]:
            del E[y][x]
            ans[y] = ans[x] if E[x][y] == 0 else 1 - ans[x]
            if len(E[y]) > 0:
                dfs(y)
    
    ans[0] = 0
    dfs(0)
    for a in ans:
        print(a)


main()

Solution (Python):
import sys
sys.setrecursionlimit(10**5)
n = int(input())
t = {i:{} for i in range(n)}
ans = [-1]*n


for _ in range(n-1):
    u ,v, w = map(int, input().split())
    t[u-1][v-1] = w
    t[v-1][u-1] = w
 
    
def dfs(x):
    for y in t[x]:
        del t[y][x]
        ans[y] = ans[x] if t[x][y] % 2 == 0 else 1 - ans[x]
        if len(t[y]) > 0:
            dfs(y)
 
 
ans[0] = 0
dfs(0)
for i in range(n):
    print(ans[i])

Solution (Python):
import sys
sys.setrecursionlimit(10**5)
n = int(input())
t = {i:{} for i in range(n)}
ans = [-1]*n
for _ in range(n-1):
    u ,v, w = map(int, input().split())
    t[u-1][v-1] = w
    t[v-1][u-1] = w


def dfs(x):
    for y in t[x]:
        del t[y][x]
        ans[y] = ans[x] if t[x][y] % 2 == 0 else 1 - ans[x]
        if len(t[y]) > 0:
            dfs(y)


ans[0] = 0
dfs(0)
for i in range(n):
    print(ans[i])

Solution (Python):
import sys
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())



from queue import Queue
def bfs(field, sy, sx, wall, dir):
    h = len(field)
    w = len(field[0])
    dp = [[-1 for _j in range(w)] for _i in range(h)]
    q = Queue()

    dp[sy][sx] = 0
    q.put((sy, sx))
    
    dy = [1, -1, 0, 0, 1, 1, -1, -1]
    dx = [0, 0, 1, -1, 1, -1, 1, -1]
    def isin(x,y): return 0 <= y and y < h and 0 <= x and x < w
    
    while not q.empty():
        y, x = q.get()
        for k in range(dir):
            ny,nx = y+dy[k],x+dx[k]
            if not isin(nx, ny) or field[ny][nx] == wall:
                continue
            elif dp[ny][nx] != -1:
                continue
            dp[ny][nx] = dp[y][x] + 1
            q.put((ny,nx))
        
    return dp
    


def ABC088_D():
    h, w = mi()
    s = [['' for _j in range(w)] for _i in range(h)]
    for i in range(h):
        s[i] = li()
    dp = bfs(s, 0, 0, '
    if dp[h - 1][w - 1] < 0:
        print(-1)
    else:
        cnt = 0
        for i in range(h):
            for j in range(w):
                cnt += s[i][j] == '.'
        print(cnt - (dp[h - 1][w - 1] + 1)) 





def ATC002_A():
    R, C = mi()
    sy, sx = mi()
    gy, gx = mi()
    s = [['' for _j in range(C)] for _i in range(R)]
    for i in range(R):
        s[i] = li()
    dp = bfs(s, sy-1, sx-1, '
    print(dp[gy-1][gx-1]) 


def ABC126_D():
    N= ii()
    G = [[] for i in range(N)]
    for i in range(N-1):
        a, b, w = mi()
        a -= 1
        b -= 1
        G[a].append((b,w))
        G[b].append((a,w))
    dist = [-1 for _ in range(N)]
    que = Queue()
    for v in range(N):
        if dist[v] != -1:
            
            continue
        dist[v] = 0
        que.put(v)
        while not que.empty():
            v = que.get()
            for nv,nw in G[v]:
                if dist[nv] == -1:
                    dist[nv] = dist[v] + nw
                    que.put(nv)
    for i in dist:
        if i % 2 == 0:
            print(0)
        else:
            print(1)




if __name__ == '__main__':
    
    
    ABC126_D()

Solution (Python):
import sys
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())



from queue import Queue
def bfs(field, sy, sx, wall, dir):
    h = len(field)
    w = len(field[0])
    dp = [[-1 for _j in range(w)] for _i in range(h)]
    q = Queue()

    dp[sy][sx] = 0
    q.put((sy, sx))
    
    dy = [1, -1, 0, 0, 1, 1, -1, -1]
    dx = [0, 0, 1, -1, 1, -1, 1, -1]
    def isin(x,y): return 0 <= y and y < h and 0 <= x and x < w
    
    while not q.empty():
        y, x = q.get()
        for k in range(dir):
            ny,nx = y+dy[k],x+dx[k]
            if not isin(nx, ny) or field[ny][nx] == wall:
                continue
            elif dp[ny][nx] != -1:
                continue
            dp[ny][nx] = dp[y][x] + 1
            q.put((ny,nx))
        
    return dp
    


def ABC088_D():
    h, w = mi()
    s = [['' for _j in range(w)] for _i in range(h)]
    for i in range(h):
        s[i] = li()
    dp = bfs(s, 0, 0, '
    if dp[h - 1][w - 1] < 0:
        print(-1)
    else:
        cnt = 0
        for i in range(h):
            for j in range(w):
                cnt += s[i][j] == '.'
        print(cnt - (dp[h - 1][w - 1] + 1)) 





def ATC002_A():
    R, C = mi()
    sy, sx = mi()
    gy, gx = mi()
    s = [['' for _j in range(C)] for _i in range(R)]
    for i in range(R):
        s[i] = li()
    dp = bfs(s, sy-1, sx-1, '
    print(dp[gy-1][gx-1]) 


def ABC126_D():
    N= ii()
    G = [[] for i in range(N)]
    for i in range(N-1):
        a, b, w = mi()
        a -= 1
        b -= 1
        G[a].append((b,w))
        G[b].append((a,w))
    is_bipartite = True
    dist = [-1 for _ in range(N)]
    que = Queue()
    for v in range(N):
        if dist[v] != -1:
            
            continue
        dist[v] = 0
        que.put(v)
        while not que.empty():
            v = que.get()
            for nv,nw in G[v]:
                if dist[nv] == -1:
                    dist[nv] = dist[v] + nw
                    que.put(nv)

    for i in dist:
        if i % 2 == 0:
            print(0)
        else:
            print(1)


if __name__ == '__main__':
    
    
    ABC126_D()