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
input = sys.stdin.readline

sys.setrecursionlimit(1000000)

def main(): 

    N=int(input())
    to=[[] for _ in range(N+1)]
    ans=[0]*(N+1)

    for i in range(N-1):
        u,v,w=map(int,input().split())
        to[u].append([v,w])
        to[v].append([u,w])
    
    def dfs(chil, par):
        for v,l in to[chil]:
            if v == par: continue
            if (ans[chil] == 1 and l%2==0) or (ans[chil] == 0 and l%2==1):
                ans[v]=1
            dfs(v,chil)
    
    
    dfs(1,-1)
    for i in ans[1:]:
        print(i)
if __name__ == "__main__":
    main()

Solution (Python):
def main():
    import sys
    from collections import defaultdict,deque
    input = sys.stdin.readline
    N = int(input())
    Tree = defaultdict(list)
    for _ in range(N-1):
        u,v,w = map(int, input().split())
        Tree[u].append([v,w])
        Tree[v].append([u,w])
        
    visited = {}
    answer = [0]*N
    que = deque()
    que.append([1,0])
    while que != deque():
        tmp = que.pop()
        visited[tmp[0]] = 1
        for i in Tree[tmp[0]]:
            if not i[0] in visited:
                if (tmp[1] + i[1])%2 == 1:
                    answer[i[0]-1] = 1
                que.append([i[0],tmp[1] + i[1]])
    print(*answer,sep="\n")
    
main()

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)


def dfs(v, p, c):
    colors[v] = c
    for next_v, d in g[v]:
        if next_v == p:
            continue
        if d % 2 == 0:
            dfs(next_v, v, c)
        else:
            dfs(next_v, v, 1 - c)


N = int(input())

g = [[] for _ in range(N)]
colors = [[] for _ in range(N)]
for i 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])

dfs(0, -1, 0)
print(*colors, sep='\n')

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

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



def dfs(u,v):
  for e,w in edge[u]:
    if e != v:
      ans[e] = (w + ans[u]) % 2
      
      dfs(e,u)

ans = [0] + [-1] * (N-1)
dfs(0,-1)
print(*ans)

Solution (Python):
from collections import deque
from collections import defaultdict
def main():
    n = int(input())
    
    dc = defaultdict(list)
    for _ in range(n-1):
        a,b,c = map(int,input().split())
        dc[a].append([b,c])
        dc[b].append([a,c])


    que = deque([1])
    
    visited = [0]*(n+1)
    visited[0] = 1
    visited[1] = 1

    ans = [1]*(n+1)
    while len(que)>0:
        now = que.popleft()
        for ls in dc[now]:
            next_,_ = ls
            if visited[ls[0]]==0:
                if ls[1]%2==0:
                    ans[next_] = ans[now]
                else:
                    ans[next_] = -ans[now]
                
                visited[ls[0]]=1
                que.append(ls[0])
            else:
                pass
        
                    
    for i in range(1,n+1):
        if ans[i]==1:
            print(1)
        else:
            print(0)
if __name__ == "__main__":
    main()

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

def survey(now, mlist, clist):
    for ml in mlist[now]:
        if clist[ml[0]]==2:
            clist[ml[0]]=(clist[now]+ml[1])%2
            _, _=survey(ml[0], mlist, clist)
    return mlist, clist

n=int(input())
mlist=[]
clist=[]
for i in range(n):
    mlist.append([])
    clist.append(2)

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

clist[0]=0
now=0
mlist, clist = survey(now, mlist, clist)
for cl in clist:
    print(cl)

Solution (Python):
n = int(input())
data = [[] for _ in range(n)]
for _ in range(n-1):
    a,b,c = map(int,input().split())
    data[a-1].append([b-1,c % 2])
    data[b-1].append([a-1,c % 2])

que = []
color = [-1 for _ in range(n)]
def dfs(a,data):
    
    
    if color[a] == -1:
        color[a] = 0
        que.append(a)
    else:
        return 0
    while len(que):
        a = que.pop()
        for i in data[a]:
            
            
            if color[i[0]] != -1:continue
        
            if i[1] == 0:
                color[i[0]] = color[a]
            else:
                color[i[0]] = 1 - color[a]  
            que.append(i[0])

for i in range(n):
    dfs(i,data)
for i in range(n):
    print(color[i])

Solution (Python):
from collections import defaultdict as dd
from collections import deque as dq
N = int(input())
e = dd(list)
for _ in range(N - 1):
  u, v, w = map(int, input().split())
  e[u].append((v, w))
  e[v].append((u, w))

res = [0] * (N + 1)
visited = [0] * (N + 1)
Q = dq([])
Q.append((1, 0))
visited[1] = 1
while len(Q):
  q = Q.pop()
  for n in e[q[0]]:
    if visited[n[0]] == 1:
      continue
    if n[1] % 2 == 0:
      res[n[0]] = res[q[0]]
    else:
      res[n[0]] = (res[q[0]] + 1) % 2
    Q.append(n)
    visited[n[0]] = 1
  
for i in range(1, N + 1):
  print(res[i])

Solution (Python):
from collections import defaultdict
from collections import deque
N = int(input())

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

ans = [-1] * (N)
ans[0] = 0
q = deque([])
q.append(0)

while len(q) > 0:
  u = q.pop()
  for v, w in D[u]:
    if ans[v] != -1:
      continue
    else:
      ans[v] = (ans[u] + w) % 2
    q.append(v)

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

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

N=int(input())

G = [[] for j in range(N)]

color = [0]*N

for _ in range(N-1):
    e,f,g = list(map(int, input().split()))
    heapq.heappush(G[e-1], [f-1,g%2])
    heapq.heappush(G[f-1], [e-1,g%2])

def dfs(v,p,c):
    color[v]=c
    for k in range(len(G[v])):
        i,j = heapq.heappop(G[v])
        if i==p:
            continue
        elif j ==0 :
            dfs(i,v,c)
        elif j ==1 :
            dfs(i,v,1-c)
        
dfs(0,-1,0)

for k in color:
    print(k)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
 
def dfs(now, prev):
    
    for next, w in g[now]:
        if ans[next] == -1:
            ans[next]=0
            if  w % 2 != 0:
                if ans[now]==0:
                    ans[next]=1
                    dfs(next,now)
                else:
                    ans[next]=0
                    dfs(next,now)
            else:
                ans[next]=ans[now]
                dfs(next,now)
    
 

n=int(input())
g = [[] for i in range(n)]
for i 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])

ans=[-1]*n
ans[0]=0
dfs(0,0)
for i in range(n):
    print(ans[i])

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

N = int(input())
color = [-1] * N
G = [[] for _ 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))

def dfs(v, cur):
  color[v] = cur
  for next_v, w in G[v]:
    if color[next_v] != -1:
      continue
    if w % 2 == 0:
      dfs(next_v, cur)
    else:
      dfs(next_v, 1 - cur)
      
dfs(0, 0)
for i in range(N):
  print(color[i])

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

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

def dfs(v, cur):
  color[v] = cur
  for next_v, w in zip(G[v], W[v]):
    if color[next_v] != -1:
      continue
    if w % 2 == 0:
      dfs(next_v, cur)
    else:
      dfs(next_v, 1 - cur)
      
for i in range(N):
  if color[i] != -1:
    continue
  dfs(i, 0)
for i in range(N):
  print(color[i])

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

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

neighbor = [[] for _ in range(n+1)]
for edge in lst:
    neighbor[edge[0]].append((edge[1], edge[2]))
    neighbor[edge[1]].append((edge[0], edge[2]))

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


def dfs(start, bw):
    color[start] = bw
    for pair in neighbor[start]:
        if color[pair[0]] != -1:
            continue
        elif pair[1] % 2 == 0:
            dfs(pair[0], bw)
        else:
            dfs(pair[0], (bw + 1) % 2)

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

Solution (Python):
from collections import defaultdict
import queue

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

ans = [-1] * N
ans[0] = 0
q = queue.Queue()
q.put(0)
while not q.empty():
    u = q.get()
    for v, w in to[u]:
        if ans[v] != -1:
            continue
        ans[v] = (ans[u] + w) % 2
        q.put(v)

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

Solution (Python):
from collections import defaultdict
import queue

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

ans = [-1] * N
ans[0] = 0
q = queue.Queue()
q.put(0)
while not q.empty():
    u = q.get()
    for v, w in to[u]:
        if ans[v] != -1:
            continue
        ans[v] = (ans[u] + w) % 2
        q.put(v)

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

Solution (Python):



import sys
sys.setrecursionlimit(100000)

def dfs(i):
  for v,w in V.get(i, []):
    if (w % 2) == 0:
      if color[v] == -1:
        color[v] = color[i]
      elif color[v] == color[i]:
        continue
      elif color[v] != color[i]:
        return False
    else:
      if color[v] == -1:
        color[v] = 0 if color[i] else 1
      elif color[v] == color[i]:
        return False
      elif color[v] != color[i]:
        continue
    dfs(v)
  return True

if __name__ == '__main__':
  n = int(input())
  V = {} 
  color = {} 
  for i in range(1, n):
    u,v,w = map(int, input().split())
    if not V.get(u, False):
      V[u] = [[v,w]]
    else:
      V[u].append([v,w])

    if not V.get(v, False):
      V[v] = [[u,w]]
    else:
      V[v].append([u,w])
    
  for i in range(1, n+1):
    color[i] = -1

  while True:
    i = 0
    for j in range(1, n+1):
      if color[j] == -1:
        i = j
        color[i] = 0
        break
    if i == 0:
      for j in range(1, n+1):
        print(color[j])
      break
    else:
      if not dfs(i):
        print('No')
        break

Solution (Python):
import sys

sys.setrecursionlimit(1000000)


n=int(input())


class Node:
    def __init__(self,name):
        self.name=name
        self.near=[]
        self.length=[]
    def coloring(self,color):
        self.color=color



nodelist=[]
for i in range(n):
    nodelist.append(Node(i))
   
    

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

color=[None for i in range(n)]
color[0]=0



def paint(node):
    nodecolor=node.color
    for j in range(len(node.near)):
        nearnode=node.near[j]
        if color[nearnode.name]!=None:
            continue
        if node.length[j]==0:
            nearnode.coloring(nodecolor)
        else:
            nearnode.coloring((nodecolor+1)%2)
        color[nearnode.name]=0
        paint(nearnode)

nodelist[0].coloring(0)
paint(nodelist[0])


for i in nodelist:
    print(i.color)

Solution (Python):
n = int(input())
L = [[] for _ in range(n+1)]
R = [[] for _ in range(n+1)]
for _ in range(n-1):
    u, v, w = map(int, input().split())
    w %= 2
    L[u] += [[u, v, w]]
    R[v] += [[v, u, w]]
P = [[] for _ in range(2*n+2)]
P[1] += (L[1]+R[1])
L[1] = R[1] = []
for i in range(1, 2*n):
    for _, v, w in P[i]:
        P[i+2-w] += (L[v]+R[v])
        L[v] = R[v] = []
A = []
for p in P[1::2]: A += p
B = set([u for u, *_ in A])
for i in range(1, n+1): print(1 if i in B else 0)

Solution (Python):
n = int(input())
L = [[] for _ in range(n+1)]
R = [[] for _ in range(n+1)]
for _ in range(n-1):
    u, v, w = map(int, input().split())
    w %= 2
    L[u] += [[u, v, w]]
    R[v] += [[v, u, w]]
P = [[] for _ in range(2*n+2)]
P[1] += (L[1]+R[1])
L[1] = R[1] = []
for i in range(1, 2*n):
    for _, v, w in P[i]:
        P[i+2-w] += (L[v]+R[v])
        L[v] = R[v] = []
A = []
for p in P[1::2]: A += p
B = set([u for u, *_ in A])
for i in range(1, n+1): print(1 if i in B else 0)

Solution (Python):

INF = float("inf")
MOD = int(1e9 + 7)


def main():
    N = int(input())
    g = [[] for _ in range(N)]
    for n in range(N - 1):
        u, v, w = map(int, input().split())
        u, v, w = u - 1, v - 1, w % 2
        g[u].append((v, w))
        g[v].append((u, w))
    rs, visited, que = [0] * N, [False] * N, [0]
    while que:
        c = que.pop(0)
        if visited[c]: continue
        visited[c] = True
        for n, w in g[c]:
            if visited[n]: continue
            rs[n] = (rs[c] + w) % 2
            que.append(n)
    for r in rs:
        print(r)

main()

Solution (Python):
n=int(input())
l=[[] for _ in range(n+1)]
q=[(1,0)]
ans=[-1]*n
while n>1:
    u,v,w=map(int,input().split())
    l[u]+=[(v,w)]
    l[v]+=[(u,w)]
    n-=1
while q:
    u,w=q.pop()
    ans[u-1]=w%2
    for v,d in l[u]:
        q+=[(v,w+d)]*(ans[v-1]<0)
print(*ans)

Solution (Python):
N = int(input())
import sys
sys.setrecursionlimit(90000)

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

color = [-1 for _ in range(N)]
def dfs(p, c, color):
    color[p] = c
    cinv = 0 if c == 1 else 1
    for q, d in graph[p]:
        if color[q] != -1:
            continue
        if d % 2 == 1:
            color = dfs(q, cinv, color)
        else:
            color = dfs(q, c, color)
    return color

A = dfs(0, 0, color)
for a in A:
    print(a)

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

n = int(input())
seen = [-1]*(n+1)
path = defaultdict(dict)

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


def dfs(seen, pos, color):
    if seen[pos] != -1:
        return
    else:
        seen[pos] = color
        change_color = 1 - color
        for nx in path[pos].keys():
            check = path[pos][nx]
            if check % 2 == 1:
                dfs(seen, nx, change_color)
            dfs(seen, nx, color)


dfs(seen, 1, 0)
for i in range(1, n+1):
    print(seen[i])

Solution (Python):

import collections
import itertools

def getint(): return int(input())
def getints(): return list(map(int, input().split()))



m,lst,nxt,to=0,[],[],[]

def init_graph(node_count, edge_count):
    global lst,nxt,to
    lst = [-1 for _ in range(node_count)]
    nxt = [0  for _ in range(edge_count)]
    to  = [0  for _ in range(edge_count)]


def add_edge(u, v):
    global m
    nxt[m], lst[u], to[m], m = lst[u], m, v, m + 1
    nxt[m], lst[v], to[m], m = lst[v], m, u, m + 1


def add_edge(u, v, w):
    global m
    nxt[m], lst[u], to[m], m = lst[u], m, (v, w), m + 1
    nxt[m], lst[v], to[m], m = lst[v], m, (u, w), m + 1

def traverse(u):
    m = lst[u]
    while m != -1:
        yield m
        m = nxt[m]


n = getint()
init_graph(n, 2 * n)

for _ in range(n - 1):
    u, v, w = getints()
    add_edge(u - 1, v - 1, w)

def get_distance(u, dist):
    dist[u] = 0
    stk = [(u, 0)]
    while stk:
        v, d = stk[-1]
        stk.pop()
        for m in traverse(v):
            vv, w = to[m]
            if dist[vv] >= 0:
                continue
            dist[vv] = d + w
            stk.append((vv, d + w))


dist = [-1]*n
get_distance(0, dist)




for i in range(n):
    print(dist[i] % 2)

Solution (Python):
from collections import deque

N = int(input())
E = [[] for _ in range(N)]
for _ in range(N-1):
    u, v, w = map(int, input().split())
    E[u-1].append((v-1, w))
    E[v-1].append((u-1, w))
ans = [-1]*N
ans[0] = 0

q = deque([0])
while q:
    next = q.popleft()
    for (u, w) in E[next]:
        if ans[u] < 0:
            q.append(u)
            ans[u] = int(ans[next]) if w % 2 == 0 else int(not ans[next])
print(*ans)

Solution (Python):
import sys
sys.setrecursionlimit(1000000)
 
def dfs(l,d,k):
  for i in l[k]:
    if d[i[0]]==-1:
      d[i[0]]=d[k]+i[1]
      dfs(l,d,i[0])

n=int(input())
l=[list(map(int,input().split())) for i in range(n-1)]
connection=[[] for i in range(n)]
for i in range(n-1):
  connection[l[i][0]-1].append([l[i][1]-1,l[i][2]])
  connection[l[i][1]-1].append([l[i][0]-1,l[i][2]])
distance=[-1 for i in range(n)]
x=-1
for i in range(n-1):
  if len(connection[i])==1:
    x=i
    break
distance[x]=0
dfs(connection,distance,x)
for i in range(n):
  print(distance[i]%2)

Solution (Python):
import sys
sys.setrecursionlimit(1000000)
 
def dfs(l,d,k):
  for i in l[k]:
    if d[i[0]]==-1:
      d[i[0]]=d[k]+i[1]
      dfs(l,d,i[0])

n=int(input())
l=[list(map(int,input().split())) for i in range(n-1)]
connection=[[] for i in range(n)]
for i in range(n-1):
  connection[l[i][0]-1].append([l[i][1]-1,l[i][2]])
  connection[l[i][1]-1].append([l[i][0]-1,l[i][2]])
distance=[-1 for i in range(n)]
x=-1
for i in range(n-1):
  if len(connection[i])==1:
    x=i
    break
distance[x]=0
dfs(connection,distance,x)
for i in range(n):
  print(distance[i]%2)

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
  g[u].append([v, w])
  g[v].append([u, w])

c = [-1]*n
c[0] = 0

queue = deque([0])

while queue:
  x = queue.popleft()
  for x_, w_ in g[x]:
    if c[x_] == -1:
      if w_%2 == 0:
        c[x_] = c[x]
      else:
        c[x_] = 1-c[x]
      queue.append(x_)
    else:
      continue

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

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
  g[u].append([v, w])
  g[v].append([u, w])

c = [-1]*n
c[0] = 0

queue = deque([0])

while queue:
  x = queue.popleft()
  for x_, w_ in g[x]:
    if c[x_] == -1:
      if w_%2 == 0:
        c[x_] = c[x]
      else:
        c[x_] = 1-c[x]
      queue.append(x_)
    else:
      continue
for x in c:
  print(x)

Solution (Python):

N = int(input())

uvw = []
Nlis = [-1] * N
Ndic = {}

for i in range(N-1):

    u,v,w = map(int,input().split())
    
    if u not in Ndic:
        Ndic[u] = [[v,w]]

    else:
        Ndic[u].append([v,w])

    if v not in Ndic:
        Ndic[v] = [[u,w]]

    else:
        Ndic[v].append([u,w])


ends = set([1])
Q = [1]
Q_cnt = 0
Nlis[0] = 0

while len(Q) > Q_cnt and N != 1:

    now = Q[Q_cnt]
    Q_cnt += 1

    for nw in Ndic[now]:

        if nw[0] not in ends:

            Q.append(nw[0])
            ends.add(nw[0])

            if nw[1] % 2 == 0:
                Nlis[nw[0]-1] =  Nlis[now-1]

            else:
                Nlis[nw[0]-1] = (Nlis[now-1] + 1) % 2

            

for i in Nlis:
    print(i)

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

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


visited = [0] * n
ans = [0] * n
def dfs(u, d):
    for i in range(len(edge[u])):
        v, w = edge[u][i]
        if visited[v] == 0:
            ans[v] = (d + w) % 2
            visited[v] = 1
            dfs(v, d + w)


visited[0] = 1
dfs(0, 0)
for a in ans:
    print(a)

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

n = int(input())
roots = [[] for _ in range(n)]

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

dis = [-1] * n
dis[0] = 0
stack = [0]

while stack:
    label = stack.pop(-1)
    for i,w in roots[label]:
        if dis[i] == -1:
            dis[i] = dis[label] + w
            stack.append(i)

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

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

n = int(input())
graph = [[] for _ in range(n)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    if w % 2 == 0:
        graph[u].append((v, 1))
        graph[v].append((u, 1))
    else:
        graph[u].append((v, -1))
        graph[v].append((u, -1))
            
color = [None for i in range(n)]
def set_color(v, c):
    color[v] = c
    for next_v, d in graph[v]:
        if color[next_v] is None:
            set_color(next_v, d * c)

set_color(0, 1)
for c in color:
    if c != 1:
        print(0)
    else:
        print(1)

Solution (Python):
N = int(input())
uvw = [list(map(int,input().split())) for _ in range(N-1)]
import sys
sys.setrecursionlimit(10000000)







        


ans = [0] * N
adj_list = [[] for _ in range(N+1)]

for u,v,w in uvw:
    adj_list[u-1].append([v-1,w])
    adj_list[v-1].append([u-1,w])
    
memo = [True] * N
def dfs(s,cnt): 
    memo[s] = False
    if cnt % 2 == 1:
        ans[s] = 1
    nxt = adj_list[s]
    for v,w in nxt:
        if memo[v]:
            dfs(v,cnt+w)
    return
    
dfs(0,0)
for a in ans:
    print(a)

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


N = int(input())
G = [[] for i in range(N)]

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

color = [None for i in range(N)]


def set_color(v, c):
    color[v] = c
    for next_v, d in G[v]:
        if color[next_v] is None:
            set_color(next_v, d*c)


set_color(0, 1)

for c in color:
    if c == 1:
        print(1)
    else:
        print(0)

Solution (Python):
N = int(input())
import sys
sys.setrecursionlimit(10 ** 6)


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

ans = [-1] * (N + 1)
def dfs(x): 
    
    
    for i in lst[x]: 
        
        if ans[i[0]] == -1: 
            if i[1] == 0: 
                ans[i[0]] = ans[x] 
                dfs(i[0])
            else: 
                ans[i[0]] = (ans[x] + 1) % 2
                dfs(i[0])
    return 

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

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

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

ans = [-1] * (N + 1)
def dfs(x): 
    
    
    for i in lst[x]: 
        
        if ans[i[0]] == -1: 
            if i[1] == 0: 
                ans[i[0]] = ans[x] 
                dfs(i[0])
            else: 
                ans[i[0]] = (ans[x] + 1) % 2
                dfs(i[0])
    return 

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

Solution (Python):
from collections import deque
N = int(input())
table = [[] for i in range(N)]
for i in range(N-1):
  u,v,w = map(int, input().split())
  u -= 1
  v -= 1
  table[u].append((v,w))
  table[v].append((u,w))
dist = [-1]*N
dist[0] = 0
H = deque()
H.append(0)
while H:
  u = H.popleft()
  for v,w in table[u]:
    if dist[v] == -1:
      dist[v] = dist[u] if w%2 == 0 else 1 - dist[u]
      H.append(v)
for i in range(N):
  print(dist[i])

Solution (Python):
import sys

N = int(input())
nodes = [set() for _ in range(N)]
for line in sys.stdin:
    u, v, w = map(int, line.split())
    u, v = (u - 1, v - 1)
    nodes[u].add((v, w))
    nodes[v].add((u, w))


ans = [-1 for _ in range(N)]
q = [(0, 0, -1)]

while q:
    v, dist, parent = q.pop()

    if dist % 2 == 0:
        ans[v] = 0
    else:
        ans[v] = 1

    for u, w in nodes[v]:
        if u == parent:
            continue

        q.append((u, dist + w, v))

print("\n".join(map(str, ans)))

Solution (Python):
N = int(input())
nodes = [set() for _ in range(N)]
for _ in range(N - 1):
    u, v, w = map(int, input().split())
    u, v = (u - 1, v - 1)
    nodes[u].add((v, w))
    nodes[v].add((u, w))


ans = [-1] * N
q = [(0, 0, -1)]

while q:
    v, dist, parent = q.pop()

    if dist % 2 == 0:
        ans[v] = 0
    else:
        ans[v] = 1

    for u, w in nodes[v]:
        if u == parent:
            continue

        q.append((u, dist + w, v))

print("\n".join(map(str, ans)))

Solution (Python):
N = int(input())
nodes = [set() for _ in range(N)]
for _ in range(N - 1):
    u, v, w = map(int, input().split())
    u, v = (u - 1, v - 1)
    nodes[u].add((v, w))
    nodes[v].add((u, w))


ans = [-1 for _ in range(N)]
q = [(0, 0, -1)]

while q:
    v, dist, parent = q.pop()

    if dist % 2 == 0:
        ans[v] = 0
    else:
        ans[v] = 1

    for u, w in nodes[v]:
        if u == parent:
            continue

        q.append((u, dist + w, v))

print("\n".join(map(str, ans)))

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
N = int(input())
a = [[] for _ in range(N)]
b = [[] for _ in range(N)]
for i in range(N-1):
    x, y, z = map(int, input().split())
    x, y, z = x-1, y-1, z % 2
    a[x].append([y, z])
    b[y].append([x, z])
dist = [0] * N
visited = [0] * N
print("")

def dfs(v, prev, dist, predist, visited):
    if visited[v]:
        return dist
    dist[v] = dist[prev] ^ predist
    visited[v] = 1
    for i in a[v]:
        dist = dfs(i[0], v, dist, i[1], visited)
    for i in b[v]:
        dist = dfs(i[0], v, dist, i[1], visited)
    return dist

dist = dfs(0, 0, dist, 0, visited)
for i in dist:
    print(i)

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

N = int(input())

C = [-1] * N  
E = [[] for _ in range(N)]  
swap = {0: 1, 1: 0}


def coloring(v, c):
    C[v] = c
    for e in E[v]:
        if C[e[0]] == -1:
            coloring(e[0], c if e[1] % 2 == 0 else swap[c])


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

coloring(0, 0)

for c in C:
    print(c)

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

N = int(input())

C = [-1] * N  
E = [[] for _ in range(N)]  
swap = {0: 1, 1: 0}


def coloring(v, c):
    C[v] = c
    for e in E[v]:
        if C[e[0]] == -1:
            coloring(e[0], c if e[1] % 2 == 0 else swap[c])


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

coloring(0, 0)

for c in C:
    print(c)

Solution (Python):
import sys
from collections import defaultdict
N = int(input())


sys.setrecursionlimit(1000000)

uvw = [[int(item) for item in input().split()] for _ in range(N-1)]

d = defaultdict(lambda: {})
for u, v, w in uvw:
    d[u][v] = w
    d[v][u] = w


dist = {1: 0}


def calc_dist(i, up):
    if not(i in dist):
        dist[i] = dist[up]+d[up][i]
        [calc_dist(k, i) for k in list(d[i].keys())]


[calc_dist(k, 1) for k in list(d[1].keys())]

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

Solution (Python):
from collections import deque

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])
Node = [None]*N
Node[0] = True
queue = deque([0])
while queue != deque([]):
    p = queue.popleft()
    for u,w in S[p]:
        if Node[u] == None:
            queue.append(u)
            Node[u] = (Node[p])^(w%2==1)
for i in range(N):
    print([0,1][Node[i]])

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

import logging

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)


def resolve():
    N = [int(x) for x in sys.stdin.readline().split()][0]
    edge_list = [[] for _ in range(N)]

    for _ in range(N-1):
        u, v, w = [int(x) for x in sys.stdin.readline().split()]
        edge_list[u-1].append((v-1, w))
        edge_list[v-1].append((u-1, w))

    color_list = [0] * N
    visited_set = set()
    def dfs(i_rec, c_rec):
        visited_set.add(i_rec)
        color_list[i_rec] = c_rec
        for i_next, weight in edge_list[i_rec]:
            if i_next in visited_set:
                continue
            if weight % 2 == 0:
                dfs(i_next, c_rec)
            else:
                dfs(i_next, 1 - c_rec)

    dfs(0, 0)

    for c in color_list:
        print(c)


if __name__ == "__main__":
    resolve()





import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """3
1 2 2
2 3 1"""
        output = """0
0
1"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """5
2 5 2
2 3 10
1 3 8
3 4 2"""
        output = """1
0
1
0
1"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """1"""
        output = """0"""
        self.assertIO(input, output)

Solution (Python):
n = int(input())
ar = [[] for i in range(n)]
for i in range(n-1):
    ipt = list(map(int,input().split()))
    ar[ipt[0]-1].append((ipt[1]-1,ipt[2]))
    ar[ipt[1]-1].append((ipt[0]-1,ipt[2]))

ans = [-1 for i in range(n)]
u = 0
ans[u] = 0
queue = [u] 
l = 1
while True:
    if l == 0:
        break
    u = queue.pop(0)
    l -= 1
    for v in ar[u]:
        if ans[v[0]] < 0:
            ans[v[0]] = ans[u] + v[1]
            queue.append(v[0])
            l+=1

for i in range(n):
    print(ans[i]%2)