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):
from collections import defaultdict
import sys

def tree_path_dict():
    def walk(i, c):
        if d[i] == -1:
            d[i] = c
            for node, dist in edge[i].items():
                walk(node, c + dist)

    sys.setrecursionlimit(100000)
    n = int(input())
    edge = defaultdict(lambda: defaultdict(lambda: float("inf")))
    for _ in range(n - 1):
        u, v, w = map(int, input().split())
        edge[u][v] = w
        edge[v][u] = w
    d = [-1] * (n + 1)
    walk(1, 0)
    for item in d[1:]:
        print(item % 2)

tree_path_dict()

Solution (Python):
import sys
def tree_path():
    
    
    
    def walk(i, c):
        if d[i] == -1:
            d[i] = c
            for node, dist in path[i]:
                walk(node, c + dist)

    sys.setrecursionlimit(100000)
    n = int(input())
    path = [[] for _ in range(n + 1)]
    for _ in range(n - 1):
        u, v, w = map(int, input().split())
        
        path[u].append([v, w])
        path[v].append([u, w])
    
    d = [-1] * (n + 1)
    walk(1, 0)
    for item in d[1:]:
        print(item % 2)


tree_path()

Solution (Python):
from collections import defaultdict
import sys

def tree_path_dict():
    sys.setrecursionlimit(100000)
    n = int(input())
    edge = defaultdict(lambda: defaultdict(lambda: float("inf")))

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

    d = [-1] * (n + 1)

    def walk(i, c):
        if d[i] == -1:
            d[i] = c
            for node, dist in edge[i].items():
                walk(node, c + dist)

    walk(1, 0)
    for i in d[1:]:
        print(i % 2)

        
tree_path_dict()

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

def tree_path():
    
    
    
    sys.setrecursionlimit(100000)
    n = int(input())
    path = [[] for _ in range(n + 1)]
    for _ in range(n - 1):
        u, v, w = map(int, input().split())
        
        path[u].append((v, w))
        path[v].append((u, w))
    
    d = [-1] * (n + 1)

    def walk(i, c):
        if d[i] == -1:
            d[i] = c
            for x, w in path[i]:
                walk(x, c + w)

    walk(1, 0)
    for i in d[1:]:
        print(i % 2)

        
tree_path()

Solution (Python):
N = int(input())
        
G = [[] for i in range(N)]
for _ in range(N - 1):
    a, b, c = map(int, input().split())
    G[a - 1].append([b - 1, c])
    G[b - 1].append([a - 1, c])
    
color = [-1] * N
color[0] = 0
Q = [0]

while len(Q) != 0:
    cur = Q.pop()
    for to, dist in G[cur]:
        if color[to] == -1:
            color[to] = (color[cur] + dist) % 2
            Q.append(to)
            
for i in range(N):
    print(color[i])

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

kyori = [-1 for k in range(N)]
kyori[T[0][0][0]] = 0
que = [T[0][0][0]]

while len(que) > 0:
    now = que.pop()
    for tsugi in T[now]:
        if kyori[tsugi[0]] == -1:
            kyori[tsugi[0]] = kyori[now] + tsugi[1]
            que.append(tsugi[0])

for a in kyori:
    print(a%2)

Solution (Python):
'''input
3
1 2 2
2 3 1
'''
import sys
sys.setrecursionlimit(100100)
N = int(input())

edges = [[] for i in range(N)]

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


color = [None]*N

color[0] = 0

def dfs(curr, prev):
	for i in edges[curr]:
		if i[0] == prev: continue

		if i[1] % 2 == 0: color[i[0]] = color[curr]
		else: color[i[0]] = 1 - color[curr]

		dfs(i[0], curr)

dfs(0, -1)

for i in color:
	print(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):
N = int(input())
graph = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    graph[u].append((v, w))
    graph[v].append((u, w))
qu = [0]
colors = [0 for _ in range(N)]
visited = set()
while len(qu) != 0:
    cur = qu.pop()
    visited.add(cur)
    for nxt, weight in graph[cur]:
        if nxt not in visited:
            colors[nxt] = (colors[cur] + weight)%2
            qu.append(nxt)
for v in colors:print(v)

Solution (Python):
import sys
sys.setrecursionlimit(10**5)
N=int(input())
uvw=[list(map(int,input().split())) for i in range(N-1)]
link=[[] for i in range(N+1)]
for u,v,w in uvw:
    link[u].append((v,w))
    link[v].append((u,w))
mark=[0]*(N+1)
color=[0]*(N+1)
def dfs(n,c):
    mark[n]=1
    color[n]=c
    for i,w in link[n]:
        if mark[i]==1:
            continue
        if w%2==0:
            dfs(i,c)
        else:
            dfs(i,1-c)
dfs(uvw[0][0],0)
for i in range(1,N+1):
    print(color[i])

Solution (Python):

class PotUnionFind:
    def __init__(self, size):
        self.parent = [-1 for i in range(size)]
        self.diff_p = [0 for i in range(size)]

    def root(self, x): 
        if self.parent[x] < 0:
            return x
        else:
            rx = self.root(self.parent[x]) 
            self.diff_p[x] += self.diff_p[self.parent[x]] 
            self.parent[x] = rx 
            return rx

    def merge(self, x, y, dxy): 
        rx = self.root(x) 
        ry = self.root(y) 
        dxy += self.diff_p[x] - self.diff_p[y] 
        x,y=rx,ry
        if x == y:
            return False
        if self.parent[x] > self.parent[y]: 
            x,y,dxy=y,x,-dxy
        self.parent[x] += self.parent[y] 
        self.parent[y] = x 
        self.diff_p[y] = dxy 
        return True

    def issame(self, x, y): 
        return self.root(x) == self.root(y)
        
    def diff(self,x,y): 
        if self.root(x) == self.root(y): 
            return self.diff_p[y] - self.diff_p[x]
        else:
            return None

    def size(self,x): 
        return -self.parent[self.root(x)]

n = int(input())

T=PotUnionFind(n)
for _ in [0]*(n-1):
    u,v,w = [int(i) for i in input().split()]
    T.merge(u-1,v-1,w)

for i in range(n):
    print(T.diff(0,i)%2)

Solution (Python):

class PotUnionFind:
    def __init__(self, size):
        self.parent = [-1 for i in range(size)]
        self.diff_p = [0 for i in range(size)]

    def root(self, x): 
        if self.parent[x] < 0:
            return x
        else:
            rx = self.root(self.parent[x]) 
            self.diff_p[x] += self.diff_p[self.parent[x]] 
            self.parent[x] = rx 
            return rx

    def weight(self, x):
        self.root(x)
        return self.diff_p[x]

    def merge(self, x, y, dxy): 
        dxy += self.weight(x) - self.weight(y) 
        x = self.root(x) 
        y = self.root(y) 
        if x == y:
            return False
        if self.parent[x] > self.parent[y]: 
            x,y,dxy=y,x,-dxy
        self.parent[x] += self.parent[y] 
        self.parent[y] = x 
        self.diff_p[y] = dxy 
        return True

    def issame(self, x, y): 
        return self.root(x) == self.root(y)
        
    def diff(self,x,y): 
        if self.root(x) == self.root(y): 
            return self.diff_p[y] - self.diff_p[x]
        else:
            return None

    def size(self,x): 
        return -self.parent[self.root(x)]

n = int(input())

T=PotUnionFind(n)
for _ in [0]*(n-1):
    u,v,w = [int(i) for i in input().split()]
    T.merge(u-1,v-1,w)

for i in range(n):
    print(T.diff(0,i)%2)

Solution (Python):


import sys                                                      
sys.setrecursionlimit(10**6)                                    
from collections import deque, Counter                          
from itertools   import accumulate, permutations, combinations  
from operator    import itemgetter                              
from bisect      import bisect_left, bisect_right, bisect       
from heapq       import heappop, heappush                       
from fractions   import gcd                                     
from math        import ceil, floor, sqrt, cos, sin, tan, pi    
from copy        import deepcopy                                
import numpy         as np                                      


def main():
    
    im = InputManager()
    
    n = im.inputSingle()
    nodes = [Node(i) for i in range(n)]
    for i in range(n-1):
        u, v, w = im.inputMultiple()
        nodes[u-1].addConnection(nodes[v-1], w)
        nodes[v-1].addConnection(nodes[u-1], w)
    
    weights = [-1 for i in range(n)]
    Node.calcWeights(nodes[0], 0, weights)
    
    for weight in weights:
        if weight % 2 == 0:
            print(0)
        else:
            print(1)
    
    return

class Node(object):
    def __init__(self, index):
        self._index = index
        self._connections = []
    
    @property
    def connections(self):
        return self._connections
    
    def addConnection(self, node, weight):
        self._connections.append({"node": node, "weight": weight})
    
    def __eq__(self, other):
        return self._index == other._index
    
    @staticmethod
    def calcWeights(node, weight, weights):
        weights[node._index] = weight
        if - 1 not in weights:
            return
        for connection in node.connections:
            if weights[connection["node"]._index] == -1:
                Node.calcWeights(connection["node"], weight+connection["weight"], weights)


class InputManager(object):
    def __init__(self):
        pass

    @staticmethod
    def inputSingle(dtype=int):
        return dtype(input())
    
    @staticmethod
    def inputMultiple(dtype=int, aslist=False):
        if aslist:
            return list(map(dtype, input().split()))
        else:
            return map(dtype, input().split())

    @staticmethod
    def input2D(h, dtype=int):
        return [list(map(dtype, input().split())) for i in range(h)]


main()

Solution (Python):





from collections import namedtuple
import sys

sys.setrecursionlimit(10**5)

DistFrom = namedtuple('DistFrom', ['f', 'dist'])

def main():
    
    n, = [int(s) for s in input().split()]
    fs = [[int(s) for s in input().split()] for _ in range(n-1)]
    
    gs = [None] * n
    for i in range(n):
        gs[i] = list()
    for x, y, d in fs:
        x -= 1
        y -= 1
        d = d%2
        gs[x].append(DistFrom(y, d))
        gs[y].append(DistFrom(x, d))
    distlist = [None] * n
    def dfs(cur, dist):
        if distlist[cur] != None:
            return
        distlist[cur] = dist
        for i, d in gs[cur]:
            dfs(i, dist^d)
    dfs(0, 0)
    
    for d in distlist:
        print(d)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
uvw = [[int(x) for x in input().split()] for _ in range(N-1)]

path = [[] for _ in range(N)]

ans = [-1]*N
ans[0] = 0

for u,v,w in uvw:
    path[u-1].append([v-1,w%2])
    path[v-1].append([u-1,w%2])
    
Q = [0]     
while Q:
    v = Q.pop(-1)
    for p,d in path[v]:
        if ans[p] == -1:
            ans[p] = (ans[v]+d)%2
            Q.append(p)
            
for a in ans:
    print(a)

Solution (Python):
import sys
 
sys.setrecursionlimit(10**5)

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

costs = [-1 for _ in range(n)]

def dfs(cur, dist):
    if costs[cur] != -1:
        return
    costs[cur] = dist
    for v, d in p[cur]:
        dfs(v, dist^d)
dfs(0, 0)

for i in range(n):
    print(costs[i])

Solution (Python):
from sys import stdin
import math
import fractions
from collections import deque

WHITE = 0
BLACK = 1

N = int(stdin.readline().rstrip())
data = [list(map(int, stdin.readline().rstrip().split())) for _ in range(N-1)]


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



dist = [None] * (N+1)
dist[data[0][0]] = 0

que = deque([])
que.append(data[0][0])
is_visited = [False] * (N+1)


while len(que) != 0:
    
    u = que.popleft()

    is_visited[u] = True

    for v, w in connect[u]:
        if not is_visited[v]:
            que.append(v)
            dist[v] = dist[u] + w


for i in range(1, N+1):
    if dist[i] % 2 == 0:
        print(WHITE)
    else:
        print(BLACK)

Solution (Python):
import sys

sys.setrecursionlimit(10**5)

n = int(input())

p = [[] for i in range(n)]

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

c = [-1 for i in range(n)]

def dfs(num, zo):
    if c[num] != -1:
        return
    else:
        c[num] = zo
    for v, w in p[num]:
        if w % 2 == 0:
            dfs(v, zo)
        else:
            dfs(v, not zo)
dfs(0, 0)

for i in range(n):
    print(1 if c[i] else 0)

Solution (Python):
from sys import stdin
import math
import fractions
from collections import deque

WHITE = 0
BLACK = 1

N = int(stdin.readline().rstrip())
data = [list(map(int, stdin.readline().rstrip().split())) for _ in range(N-1)]


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



dist = [None] * (N+1)
dist[data[0][0]] = 0

que = deque([])
que.append(data[0][0])
is_visited = [False] * (N+1)


while len(que) != 0:
    u = que.popleft()

    if is_visited[u] == True:
        continue

    is_visited[u] = True

    for v, w in connect[u]:
        que.append(v)
        if dist[v] == None:
            dist[v] = dist[u] + w


for i in range(1, N+1):
    if dist[i] % 2 == 0:
        print(WHITE)
    else:
        print(BLACK)

Solution (Python):
N = int(input())
U, V, W = [], [], []
T = [[] for _ in range(N)]

class Tree(object):
    def __init__(self, us, vs, ws):
        self.con_table = [[] for _ in range(len(us) + 1)]
        self.weights = dict()
        for (u, v, w) in zip(us, vs, ws):
            self.con_table[u-1].append(v-1)
            self.con_table[v-1].append(u-1)
            self.weights[(u-1, v-1)] = w
        self.init_search()

    def init_search(self):
        self.visited = [False for _ in range(len(self.con_table))]

dists = [None for _ in range(N)]

from collections import deque

def dfs(tree, s):
    tree.init_search()
    dists[s] = 0
    while not all(tree.visited):
        search = deque()
        tree.visited[s] = True
        search.append(s)
        while len(search) > 0:
            v = search.pop()
            for u in tree.con_table[v]:
                if not tree.visited[u]:
                    dists[u] = dists[v] + tree.weights[(min(u, v), max(u, v))]
                    tree.visited[u] = True
                    search.append(u)

for _ in range(N-1):
    u, v, w = map(int, input().split())
    U.append(u)
    V.append(v)
    W.append(w)

tree = Tree(U, V, W)

dfs(tree, 0)

print('\n'.join(('0' if d % 2 == 0 else '1') for d in dists))

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
N = int(input())
num_edges = N-1
edges = [list(map(int, input().split())) for i in range(num_edges)]

adj = [[] for i in range(N+1)]
for ed in edges:
    adj[ed[0]] += [[ed[1], ed[2]]]
    adj[ed[1]] += [[ed[0], ed[2]]]
col_list = [-1 for i in range(N+1)]
def recfun(v, col):
    col_list[v] = col 
    for ed in adj[v]: 
        if col_list[ed[0]] == -1: 
            if ed[1] % 2 == 0:
                recfun(ed[0], col) 
            else:
                recfun(ed[0], 1-col) 
recfun(1, 0)
for col in col_list[1:]:
    print(col)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
N = int(input())
num_edges = N-1
edges = [list(map(int, input().split())) for i in range(num_edges)]

adj = [[] for i in range(N+1)]
for ed in edges:
    adj[ed[0]] += [[ed[1], ed[2]]]
    adj[ed[1]] += [[ed[0], ed[2]]]
col_list = [-1 for i in range(N+1)]
def recfun(v, col):
    col_list[v] = col 
    for ed in adj[v]: 
        if col_list[ed[0]] == -1: 
            if ed[1] % 2 == 0:
                recfun(ed[0], col) 
            else:
                recfun(ed[0], 1-col) 
recfun(1, 0)
for col in col_list[1:]:
    print(col)

Solution (Python):
import numpy as np


def read():
    N = int(input().strip())  
    edges = dict()
    for i in range(N-1):
        
        u, v, w = list(map(int, input().strip().split()))
        if u - 1 in edges.keys():
            edges[u - 1].update({v - 1: w % 2})
        else:
            edges.update({u - 1: {v - 1: w % 2}})
        if v - 1 in edges.keys():
            edges[v - 1].update({u - 1: w % 2})
        else:
            edges.update({v - 1: {u - 1: w % 2}})
    return N, edges


def solve(N, edges):
    c = [-1] * N  
    stack = list()
    c[0] = 0
    stack.append(0)
    while len(stack) > 0:
        s = stack.pop()
        for t in edges[s].keys():
            if c[t] == -1:
                if edges[s][t]:
                    c[t] = 1 - c[s]
                else:
                    c[t] = c[s]
                stack.append(t)
    return c


if __name__ == '__main__':
    inputs = read()
    colors = solve(*inputs)
    for col in colors:
        print("%d" % col)

Solution (Python):
import numpy as np


def read():
    N = int(input().strip())  
    to = [[] for _ in range(N)]
    cost = [[] for _ in range(N)]
    for i in range(N-1):
        
        u, v, w = list(map(int, input().strip().split()))
        to[u-1].append(v-1)
        to[v-1].append(u-1)
        cost[u-1].append(w % 2)
        cost[v-1].append(w % 2)
    return N, to, cost


def solve(N, to, cost):
    c = [-1] * N  
    q = list()
    c[0] = 0
    q.append(0)
    while len(q) > 0:
        s = q.pop()
        for t, co in zip(to[s], cost[s]):
            if c[t] == -1:
                if co:
                    c[t] = 1 - c[s]
                else:
                    c[t] = c[s]
                q.append(t)
    return c


if __name__ == '__main__':
    inputs = read()
    colors = solve(*inputs)
    for col in colors:
        print("%d" % col)

Solution (Python):
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)

Solution (Python):



N = int(input())
T = [dict() for _ in range(N)]

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

opened = {0}
g0 = {0}
used = {0}

while len(opened) != 0:
    n = opened.pop()
    for n2 in T[n]:
        if n2 in used:
            continue
        if n in g0:
            if T[n][n2] % 2 == 0:
                g0.add(n2)
        else:
            if T[n][n2] % 2 == 1:
                g0.add(n2)
        opened.add(n2)
        used.add(n2)

for i in range(N):
    print(0 if i in g0 else 1)

Solution (Python):

import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N = read_a_int()

from collections import defaultdict
node = defaultdict(set)  
for _ in range(N-1):
    u, v, w = read_ints()
    if w % 2:
        
        w = True
    else:
        w = False
    node[v-1].add((u-1, w))
    node[u-1].add((v-1, w))



import numpy as np
ans = np.full(N, -1, dtype='int16')

visited = np.full(N, False, dtype=bool)
from collections import deque

que = deque([(0, 0)])  
ans[0] = 0
visited[0] = True
while que:
    n, c = que.popleft()
    ans[n] = c

    for nex in node[n]:
        nex_node, nex_w = nex
        nex_color = c
        if visited[nex_node]:
            continue
        
        if nex_w:
            nex_color = 1-nex_color

        que.append((nex_node, nex_color))
        visited[nex_node] = True

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

Solution (Python):



N = int(input())
T = [dict() for _ in range(N)]

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

opened = {0}
g0 = {0}
used = {0}

while len(opened) != 0:
    n = opened.pop()
    for n2 in T[n]:
        if n2 in used:
            continue
        if n in g0:
            if T[n][n2] % 2 == 0:
                g0.add(n2)
        else:
            if T[n][n2] % 2 == 1:
                g0.add(n2)
        opened.add(n2)
        used.add(n2)

for i in range(N):
    print(0 if i in g0 else 1)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)

noded = {}
edad = {}

class Node:
    def __init__(self, index):
        self.index = index
        self.color = -1
        self.eda_list = []

class Eda:
    def __init__(self, index, w, left, right):
        self.index = index
        self.w = w
        self.left = left
        self.right = right

def color_oppo_of(node):
    c = node.color
    for eda in [edad[edai] for edai in node.eda_list]:
        if eda.right == node.index:
            oppo = eda.left
        else:
            oppo = eda.right

        if noded[oppo].color == -1:
            if eda.w % 2 == 0:
                noded[oppo].color = node.color
            else:
                noded[oppo].color = (node.color + 1) % 2
            color_oppo_of(noded[oppo])

def solve():
    n = int(input())
    if n == 1:
        print("0")
    else:
        do(n)


def do(n):
    for i in range(n-1):
        U, V, W = map(int, input().split())
        u = U
        v = V
        w = W

        eda = Eda(i, w, u, v)
        edad[i] = eda

        if u not in noded.keys():
            noded[u] = Node(u)
        noded[u].eda_list.append(i)

        if v not in noded.keys():
            noded[v] = Node(v)
        noded[v].eda_list.append(i)

    noded[1].color = 0
    color_oppo_of(noded[1])

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



solve()

Solution (Python):
N=int(input())
from collections import defaultdict
branch=defaultdict(dict)
for i in range(N-1):
    u,v,w=map(int,input().split())
    branch[u-1][v-1]=w
    branch[v-1][u-1]=w
dist=[float('inf')]*N
dist[0]=0
check={0}
while len(check)>0:
    now=check.pop()
    for nex,w in branch[now].items():
        if dist[nex]>dist[now]+w:
            dist[nex]=dist[now]+w
            check|={nex}
color=[d%2 for d in dist]
for i in range(N):
    print(color[i])

Solution (Python):
import sys
import Queue

n = int(raw_input())

graph = dict()
color = [-1 for i in range(0, n)]
for i in range(0, n - 1):
    u, v, w = map(int, raw_input().split(" "))
    if (u - 1) in graph:
        graph[u - 1].append((v-1, w))
    else:
        graph[u - 1] = [(v-1, w)]

    if (v - 1) in graph:
        graph[v - 1].append((u - 1, w))
    else:
        graph[v - 1] = [(u - 1, w)]





queue = Queue.Queue()
sum = 0
next_check = 0

while True:
    if sum >= n:
        break

    for i in range(next_check, n):
        if color[i] < 0:
            queue.put(i)
            color[i] = 0
            sum += 1
            next_check = i
            break

    while True:
        if queue.empty():
            break

        current_node = queue.get()
        if current_node in graph:
            for end, weight in graph[current_node]:
                if color[end] < 0:
                    color[end] = (color[current_node] + weight % 2) % 2
                    queue.put(end)
                    sum += 1

for node in range(0, n):
    print(color[node])

Solution (Python):
from collections import deque
import math
from functools import reduce

def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

n = int(input())
ans = [-1] * n
ans[0] = 0

edges = [ [] for _ in range(n)]
for i in range(n-1):
    u,v,w = get_nums_l()
    edges[u-1].append([v-1,w%2==0])
    edges[v-1].append([u-1,w%2==0])

def aaa(x):
    

    

    for edge in edges[x]:
        to = edge[0] 
        if ans[to] != -1:
            continue
        same = edge[1]
        ans[to] = ans[x] ^ (not same)

        queue.append(to)

queue = deque()
queue.append(0)
while queue:
    aaa(queue.popleft())
for annn in ans:
    print(annn)

Solution (Python):
N = int(input())
to = [[] for _ in range(N)]
cost = [[] for _ in range(N)]

for _ in range(N - 1):
  a, b, w = map(int, input().split())
  a -= 1
  b -= 1
  to[a].append(b)
  to[b].append(a)
  cost[a].append(w)
  cost[b].append(w)

ans = [-1 for _ in range(N)]
queue = []
queue.append(0)
while len(queue) > 0:
  v = queue.pop(0)
  for i in range(len(to[v])):
    u = to[v][i]
    w = cost[v][i]
    if ans[u] != -1:
      continue
    ans[u] = (ans[v] + w) % 2
    queue.append(u)

for a in ans:
  print(a)

Solution (Python):
def d_even_relation(N, Nodes):
    from collections import deque
    graph = [{} for _ in range(N)]
    for u, v, w in Nodes:
        graph[u - 1][v - 1] = w
        graph[v - 1][u - 1] = w

    distance_from_root = [0] * N  
    queue = deque([(-1, 0, 0)])  
    while queue:
        parent, child, distance = queue.pop()
        if parent != -1:  
            distance += (graph[parent][child] % 2)  
            distance_from_root[child] = distance
        for k in graph[child].keys():
            if parent != k:  
                queue.appendleft((child, k, distance))
    ans = (1 if d % 2 == 0 else 0 for d in distance_from_root)
    return '\n'.join(map(str, ans))

N = int(input())
Nodes = [[int(i) for i in input().split()] for j in range(N - 1)]
print(d_even_relation(N, Nodes))

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

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

ad = [[] for _ in range(N)]

for u, v, w in edge:
    u -= 1; v -= 1
    ad[u].append((v,w))
    ad[v].append((u,w))

ans = [0]*N

def dfs(v=0,pv=-1,d=0):
    if d % 2 == 0:
        ans[v] = 1
    for nv,w in ad[v]:
        if nv != pv:
            dfs(nv,v,d+w)

dfs()

for a in ans:
    print(a)

Solution (Python):
import sys
sys.setrecursionlimit(100000)
nnn = int(input())

a = [-2 for i in range(nnn)]
a[0] = 0

def m():
	return map(int,input().split())


def oon(x,n,nn,k):
	global a
	for i in range(len(x[n])):
		if x[n][i][0] != nn and a[nn] != -2:
			p = (k + x[n][i][1]) % 2
			if p == 0:
				a[x[n][i][0]] = 0
			else:
				a[x[n][i][0]] = 1
			oon(x,x[n][i][0],n,p)



b = [[] for i in range(nnn)]

for i in range(nnn-1):
	z,x,c = m();
	b[z-1].append([x-1,c])
	b[x-1].append([z-1,c])

oon(b,0,0,0)

for i in range(nnn):
	print(a[i])

Solution (Python):
from collections import deque


class Node:
    def __init__(self, id, w):
        self.id = id
        self.w = w


N = int(input())
uvw = [list(map(int, input().split())) for _ in range(N - 1)]
pairs = [[] for _ in range(N + 1)]
for u, v, w in uvw:
    pairs[u].append([v, w])
    pairs[v].append([u, w])

nodes = {1: Node(1, 0)}
q = deque([nodes[1]])
while len(q) > 0:
    node = q.pop()
    for v, w in pairs[node.id]:
        if v not in nodes:
            nodes[v] = Node(v, node.w + w)
            q.append(nodes[v])

for i in range(1, N + 1):
    print(nodes[i].w % 2)

Solution (Python):
from sys import setrecursionlimit, getrecursionlimit
setrecursionlimit(getrecursionlimit()*10000)
from collections import defaultdict
g = defaultdict(list)
visited = defaultdict(bool)
N = int(input())
ans = [0] * N

def dfs(v, d=0):
    if visited[v]:
        return
    visited[v] = True
    if d % 2:
        ans[v-1] = 1
    for c,w in g[v]:
        dfs(c, d+w)

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

dfs(1)

for a in ans:
    print(a)

Solution (Python):
import sys
from collections import deque

def solve(tree, u, v):

    colors = {}
    colors[u] = 0
    stack = deque([u])
    while stack:
        u = stack.pop()
        for v in tree[u]:
            if v in colors:
                continue

            stack.append(v)
            if tree[u][v] % 2 == 0:
                colors[v] = colors[u]
            else:
                colors[v] = 0 if colors[u] == 1 else 1

    sorted(colors)
    for c in colors:
        print colors[c]

if __name__ == '__main__':

    tree = {}
    n = sys.stdin.readline().strip()
    for _ in xrange(int(n)-1):
        u, v ,w = map(int, sys.stdin.readline().strip().split(" "))
        if u not in tree:
            tree[u] = {}
        if v not in tree:
            tree[v] = {}
        tree[u][v] = w
        tree[v][u] = w

    solve(tree, u, v)

Solution (Python):
import sys
sys.setrecursionlimit(100000)
n = int(input())

d = [set() for i in range(n)]
cost = {}
for i in range(n-1):
    u,v,w = map(int,input().split())
    
    d[u-1].add(v-1)
    d[v-1].add(u-1)
    cost[(u-1,v-1)] = w%2
    cost[(v-1,u-1)] = w%2

visited = [False for i in range(n)]
colors = [0 for i in range(n)]

stack = []
def dfs(v,c):
    visited[v] = True
    colors[v] = c%2
    for i in d[v]:
        if not visited[i]:
            dfs(i,c+cost[(v,i)])

dfs(0,0)

for i in range(n):
    print(colors[i])

Solution (Python):
N = int(input())
W = [ [] for _ in range(N) ]
dist = [-1] * N
dist[0] = 0

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

lst = [ ( 0 ,0, -1)]
idx = 0
while len(lst) > idx:
    cur, d, prev = lst[idx]
    idx += 1
    for v, w in W[cur]:
        if v == prev:
            continue
        dist[v] = d + w
        lst.append( (v, dist[v], cur))

for ans in dist:
    print(ans % 2)