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



Sample input:
3
1 2 2
2 3 1


Sample output:
0
0
1

Submission status: Wrong Answer

Solution code:
# def gcd(a_gcd, b_gcd):
#     while b_gcd != 0:
#         a_gcd, b_gcd = b_gcd, a_gcd % b_gcd
#     return a_gcd
#
#
# def lcm(a_lcm, b_lcm):
#     return a_lcm * b // gcd(a_lcm, b_lcm)
#
#
# def max_sum(N_max_sum, a_max_sum):
#     dp = [0] * (N_max_sum + 1)
#     for i in range(N_max_sum):
#         dp[i + 1] = max(dp[i], dp[i] + a_max_sum[i])
#     return dp[N]
#
#
# def knapsack(N_knapsack, W, weight, value):
#     inf = float("inf")
#     dp = [[-inf for _ in range(W+1)] for j in range(N_knapsack + 1)]
#     for i in range(W+1):
#         dp[0][i] = 0
#
#     for i in range(N_knapsack):
#         for w in range(W+1):
#             if weight[i] <= w:
#                 dp[i+1][w] = max(dp[i][w-weight[i]]+value[i], dp[i][w])
#             else:
#                 dp[i+1][w] = dp[i][w]
#     return dp[N_knapsack][W]


# import math
#
# # n = int(input())
# N, K = map(int, input().split())
#
# sum = [0 for _ in range(math.ceil(math.log(K, 2)) + 1)]
#
#
# for i in range(N, 0, -1):
#     for j in range(len(sum)):
#         if K <= i * pow(2, j):
#             sum[j] += 1
#             break
#
# ans = 0
#
# for i in range(len(sum)):
#     # print(pow(2, i))
#     # print(sum[i])
#     ans += sum[i] / (pow(2, i) * N)
#
# print(ans)


class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


N = int(input())
U = []
V = []
W = []

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


node_list = UnionFind(N)
iti = 0

for i in range(N - 1):
    if (W[i] % 2) == 0:
        node_list.union(U[i]-1, V[i]-1)
        iti = node_list.find(V[i] - 1)



for i in range(N):
    if node_list.find(i) == iti:
        print('1')
    else:
        print('0')

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

import sys
sys.setrecursionlimit(10000)

def paint(k, same, diff, visited, csame, cdiff):
    if k in visited:
        return
    if k in same:
        return
    csame.add(k)
    visited.add(k)
    for e in diff:
        if e in visited:
            continue
        paint(e, same, diff, visited, cdiff, csame)
    for e in same:
        if e in visited:
            continue
        paint(e, same, diff, visited, csame, cdiff)

def main():
    n = int(input())
    same = {}
    diff = {}
    for _ in range(n-1):
        u, v, w = list(map(int, input().split()))
        if w % 2 == 0:
            same.setdefault(u, [])
            same.setdefault(v, [])
            same[u].append(v)
            same[v].append(u)
        else:
            diff.setdefault(u, [])
            diff.setdefault(v, [])
            diff[u].append(v)
            diff[v].append(u)
    white = set()
    black = set()
    visited = set()
    for k in diff:
        paint(k, same, diff, visited, white, black)
    ret = []
    for i in range(n):
        if i+1 in white:
            ret.append("0")
        else:
            ret.append("1")
    print("\n".join(ret))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
bw = [-1 for i in range(N+1)]
ng = 0
for i in range(N-1):
    u, v, w = map(int, input().split())
    w = w % 2
    if i == 0:
        bw[u] = ng
        bw[v] = ng
        ng += 1
    elif bw[u] == -1 and bw[v] == -1:
        bw[u] = ng
        bw[v] = ng
        ng += 1
    elif w % 2 == 0:
        if bw[u] == -1 or bw[v] == -1:
            maxg = max(bw[u], bw[v])
            bw[u] = maxg
            bw[v] = maxg
        elif bw[u] != bw[v]:
            ming = min(bw[u], bw[v])
            bw[u] = ming
            bw[v] = ming
    else:
        if bw[u] == -1:
            bw[u] = ng
            ng += 1
        elif bw[v] == -1:
            bw[v] = ng
            ng += 1
for x in bw[1:]:
    if x == 0:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
input = sys.stdin.readline
 
# bisect_left(lists, 3)
# bisect_right(lists, 3)
 
def main():
	#N, K = map(int, input().split(" "))
	N = int(input())
	#S = input()
	#S = S[:-1]
	#S = list(S)

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

	UF = UnionFind(N)
	UF2 = UnionFind(N)
	for i, val in enumerate(U):
		a, b, c = val
		a -= 1
		b -= 1
		if c % 2 == 0:
			#print("+{} {}".format(a, b))
			UF.union(a, b)
		else:
			UF2.union(a, b)

	ans = [-1 for _ in range(N)]
	for i in range(N):
		if i == 0:
			ans[0] = 0

		for j in range(N):
			if i >= j:
				continue

			if ans[i] == -1:
				break

			#print(j)
			flg = UF.is_same(i, j)
			if flg == True:
				#print(j)
				ans[j] = ans[i]
	
	for i in ans:
		if i == 0:
			print(i)
		else:
			print(1)

class UnionFind(object):
	def __init__(self, n=1):
		self.par = [i for i in range(n)]
		self.rank = [0 for _ in range(n)]
		self.size = [1 for _ in range(n)]
		
	# x が属するグループを探索
	def find(self, x):
		if self.par[x] == x:
			return x
		else:
			self.par[x] = self.find(self.par[x])
			return self.par[x]
		
	# x と y のグループを結合
	def union(self, x, y):
		x = self.find(x)
		y = self.find(y)
		if x != y:
			# メンバ数の更新
			sum = self.size[x] + self.size[y]
			self.size[x] = sum
			self.size[y] = sum
			# 短いリストを長いリストのルートに繋ぎ直す
			if self.rank[x] < self.rank[y]:
				x, y = y, x
			if self.rank[x] == self.rank[y]:
				self.rank[x] += 1
			self.par[y] = x
	
	# x と y が同じグループがどうか
	def is_same(self, x, y):
		return self.find(x) == self.find(y)
		
	# x が属するグループのメンバ数を返却
	def getSize(self, x):
		par_x = self.find(x)
		return self.size[par_x]

if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import sys


def input():
    return sys.stdin.readline()[:-1]


N = int(input())
A = [list(map(int, input().split())) for i in range(N-1)]
A = [(e[0] - 1, e[1] - 1, e[2]) if e[0] <= e[1] else (e[1] - 1, e[0] - 1, e[2]) for e in A]

B = {i: [] for i in range(N)}  # かく頂点がどの頂点と繋がっているか
C = {(e[0], e[1]): e[2] for e in A}  # へんのながさ
# print(C)
for e in A:
    B[e[0]].append(e[1])
    B[e[1]].append(e[0])
# print(B)
not_visited = [1 for i in range(N)]
not_visited[0] = 0
color = [-1 for i in range(N)]
color[0] = 0  # node 0は0色
beam = [0]
while True:
    now = beam[-1]
    for b in B[now]:
        if not_visited[b]:
            edge = (now, b) if now <= b else (b, now)
            print(edge)
            if C[edge] %2 ==0:
                color[b] = color[now]
            else:
                color[b] = 1 - color[now]
            not_visited[b] = 0
            beam.insert(0, b)
    beam.pop()
    if sum(not_visited) == 0:
        break

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

Submission status: Wrong Answer

Solution code:
def convert(inX):
  if inX == 1:
    return 0
  else:
    return 1

n = int(input())
data = [2]*(n+1)
for i in range(n-1):
  tmp = input().split()
  if int(tmp[2])%2==0:
    if data[int(tmp[0])]!=2 and data[int(tmp[1])]==2: 
      data[int(tmp[1])] = data[int(tmp[0])]
    elif data[int(tmp[0])]==2 and data[int(tmp[1])]!=2:
      data[int(tmp[0])] = data[int(tmp[1])]
    elif data[int(tmp[0])]==2 and data[int(tmp[1])]==2:
      data[int(tmp[0])] = 1
      data[int(tmp[1])] = 1
  else:
    if data[int(tmp[0])]!=2 and data[int(tmp[1])]==2: 
      data[int(tmp[1])] = convert(data[int(tmp[0])])
    elif data[int(tmp[0])]==2 and data[int(tmp[1])]!=2:
      data[int(tmp[0])] = convert(data[int(tmp[1])])
    elif data[int(tmp[0])]== 2 and data[int(tmp[1])]==2:
      data[int(tmp[0])] = 1
      data[int(tmp[1])] = 0

for i in range(1, len(data)):
  print(data[i])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
N = int(raw_input())
l = []
for i in range(N-1):
    l.append(raw_input().split())

ans = [0]*N
for i in range(N-1):
    u = int(l[i][0])
    v = int(l[i][1])
    w = int(l[i][2])

    if w%2 == 0:
        ans[u-1] = 1
        ans[v-1] = 1

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

Submission status: Wrong Answer

Solution code:
n = int(input())
edge = [0] *n
for _ in range(n-1):
    u, v, w = map(int, input().split())
    if w%2==0:
        edge[u-1] = 1
        edge[v-1] = 1
    else:
        pass
for i in range(len(edge)):
    print(edge[i])

Submission status: Wrong Answer

Solution code:
import random
n = int(input())
listA = []
listB = []
listN=[]
stockL=[]
index=0
while index < n-1:
    u, v, w = map(int, input().split())
    if w == 1:
        listA.append(u)
        listB.append(v)
        listN.append(u)
        listN.append(v)
    else:
        stockL.append([u, v])
    index+=1

flag=1
while flag:
    flag=0
    index=0
    while index < len(stockL):
        u = stockL[index][0]
        v = stockL[index][1]
        if u in listA:
            if v in listB:
                flag=1
                vI = listB.index(v)
                uI = listA.index(u)
                if random.random() > 0.5:
                    listA[vI], listB[vI] = listB[vI], listA[vI]
                else:
                    listA[uI], listB[uI] = listB[uI], listA[uI]
            else:
                if v in listA:
                    pass
                else:
                    listA.append(v)
        elif u in listB:
            if v in listA:
                flag=1
                vI = listA.index(v)
                uI = listB.index(u)
                if random.random() > 0.5:
                    listA[vI], listB[vI] = listB[vI], listA[vI]
                else:
                    listA[uI], listB[uI] = listB[uI], listA[uI]
            else:
                if v in listB:
                    pass
                else:
                    listB.append(v)
        else:
            if v in listB:
                listB.append(u)
                listB.append(v)
            elif v in listA:
                listA.append(v)
                listA.append(u)
            else:
                pass
        index+=1
index2=1
while index2 < n+1:
    if index2 in listA:
        print(0)
    elif index2 in listB:
        print(1)
    else:
        print(0)
    index2+=1

Submission status: Wrong Answer

Solution code:
import copy
import sys

sys.setrecursionlimit(10000000)
N=int(input())
data=[[int(i) for i in input().split()] for _ in range(N-1)]
state = [-1 for _ in range(N+1)]
def search(depth,copyed_state):
    if depth==len(data):
        # print('end')
        return copyed_state
    kl = data[depth]
    anss=[]
    # print('depth:{} state:{}'.format(depth,copyed_state))
    if kl[2]%2 ==0:
        if copyed_state[kl[0]]==-1 and copyed_state[kl[1]]==-1:
            new_lizeros = copy.copy(copyed_state)
            new_lizeros[kl[0]] = 0
            new_lizeros[kl[1]] = 0
            anss.append(search(depth+1,new_lizeros))
            new_liones = copy.copy(copyed_state)
            new_liones[kl[0]] = 1
            new_liones[kl[1]] = 1
            anss.append(search(depth+1,new_liones))
        elif copyed_state[kl[0]]==0 and copyed_state[kl[1]]==-1:
            one_lizeros = copy.copy(copyed_state)
            one_lizeros[kl[1]] = 0
            anss.append(search(depth+1,one_lizeros))
        elif copyed_state[kl[0]]==1 and copyed_state[kl[1]]==-1:
            one_lizeros = copy.copy(copyed_state)
            one_lizeros[kl[1]] = 1
            anss.append(search(depth+1,one_lizeros))
        elif copyed_state[kl[0]]==-1 and copyed_state[kl[1]]==0:
            one_lizeros = copy.copy(copyed_state)
            one_lizeros[kl[0]] = 0
            anss.append(search(depth+1,one_lizeros))
        elif copyed_state[kl[0]]==-1 and copyed_state[kl[1]]==1:
            one_lizeros = copy.copy(copyed_state)
            one_lizeros[kl[0]] = 1
            anss.append(search(depth+1,one_lizeros))
    if copyed_state[kl[0]] == -1 and copyed_state[kl[1]] == -1:
        new_lizeros2 = copy.copy(copyed_state)
        new_lizeros2[kl[0]] = 0
        new_lizeros2[kl[1]] = 1
        anss.append(search(depth + 1, new_lizeros2))
        new_liones2 = copy.copy(copyed_state)
        new_liones2[kl[0]] = 1
        new_liones2[kl[1]] = 0
        anss.append(search(depth + 1, new_liones2))
    elif copyed_state[kl[0]] == 0 and copyed_state[kl[1]] == -1:
        one_lizeros2 = copy.copy(copyed_state)
        one_lizeros2[kl[1]] = 1
        anss.append(search(depth + 1, one_lizeros2))
    elif copyed_state[kl[0]] == 1 and copyed_state[kl[1]] == -1:
        one_lizeros2 = copy.copy(copyed_state)
        one_lizeros2[kl[1]] = 0
        anss.append(search(depth + 1, one_lizeros2))
    elif copyed_state[kl[0]] == -1 and copyed_state[kl[1]] == 0:
        one_lizeros2 = copy.copy(copyed_state)
        one_lizeros2[kl[0]] = 1
        anss.append(search(depth + 1, one_lizeros2))
    elif copyed_state[kl[0]] == -1 and copyed_state[kl[1]] == 1:
        one_lizeros2 = copy.copy(copyed_state)
        one_lizeros2[kl[0]] = 0
        anss.append(search(depth + 1, one_lizeros2))
    else: # dottimo haittetara
        sktyu = copy.copy(copyed_state)
        anss.append(search(depth + 1, sktyu))

    for i in anss:
        if len(i)!=0:
            return i
ans=search(0,state)
for i in range(1,N+1):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
# 入力が10**5とかになったときに100ms程度早い
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(lambda: set())  # キーで指定したノードが隣接するノードを記録しておく。
for _ in range(N-1):
    u, v, w = read_ints()
    if w % 2:
        # wじゃ奇数だったらTrue
        w = True
    else:
        w = False
    node[v-1].add((u-1, w))
    node[u-1].add((v-1, w))


# 0番目から探索していく
import numpy as np
ans = np.full(N, -1, dtype='int16')
# -1は未探索、1は黒、0は白、奇数の重みに当たるたびに反転
visited = np.full(N, False, dtype=bool)
from collections import deque
# 幅優先探索で白黒を記録していく
que = deque([(0, 0)])  # (nodeidx, 現在の色)
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
        if visited[nex_node]:
            continue
        # 重みが奇数なら色を反転して探索に追加
        if nex_w:
            if c == 0:
                c = 1
            else:
                c = 0

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
u = [0]*(n-1)
v = [0]*(n-1)
w = [0]*(n-1)
ans = [-1]*n
for i in range(n-1):
    u_temp,v_temp,w[i] = map(int,input().split())
    u[i] = u_temp-1
    v[i] = v_temp-1
    
for i in range(n-1):
    if w[i] % 2 == 0:
        if ans[u[i]] == -1 and ans[v[i]] == -1:
            ans[u[i]] = 0
            ans[v[i]] = 0
        elif ans[u[i]] == 0 and ans[v[i]] == -1:
            ans[v[i]] = 0
        elif ans[u[i]] == -1 and ans[v[i]] == 0:
            ans[u[i]] = 0
        elif ans[u[i]] == 1 and ans[v[i]] == -1:
            ans[v[i]] = 1
        elif ans[u[i]] == -1 and ans[v[i]] == 1:
            ans[u[i]] = 1
            
    if w[i] % 2 == 1:
        if ans[u[i]] == -1 and ans[v[i]] == -1:
            ans[u[i]] = 0
            ans[v[i]] = 1
        elif ans[u[i]] == 0 and ans[v[i]] == -1:
            ans[v[i]] = 1
        elif ans[u[i]] == -1 and ans[v[i]] == 0:
            ans[u[i]] = 1
        elif ans[u[i]] == 1 and ans[v[i]] == -1:
            ans[v[i]] = 0
        elif ans[u[i]] == -1 and ans[v[i]] == 1:
            ans[u[i]] = 0
                       
for i in range(n):
    if ans[i] == -1:
        ans[i] = 0
    print(ans[i])

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

i  = lambda: int(input())
mi = lambda: map(int, input().split())

N = i()
white = []
black = []

for _ in range(N-1):
    u, v, w = mi()
    if w % 2 == 0:
        if u in white or v in white:
            white += [u,v]
        else:
            black += [u,v]
    else:
        if u in white:
            black += [v]
        elif u in black:
            white += [v]
        elif v in white:
            black += [u]
        elif v in black:
            white += [u]
        else:
            white += [u]
            black += [v]

for i in range(1, N+1):
    if i in black:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
N = int(input())
uvw = []
out = [ 0 for i in range(N)]
bk, wh = [],[]
for i in range(N-1):
  uvw = list(map(int,input().split()))
  if uvw[2]%2 == 0:
    if (uvw[0]-1 in bk):
      bk.append(uvw[1]-1)
    elif (uvw[1]-1 in bk):
      bk.append(uvw[0]-1)
    elif (uvw[0]-1 in wh):
      wh.append(uvw[1]-1)
    elif (uvw[1]-1 in wh):
      wh.append(uvw[0]-1)
    else:
      bk.append(uvw[0]-1)
      bk.append(uvw[1]-1)
  else:
    if (uvw[0]-1 in bk):
      wh.append(uvw[1]-1)
    elif (uvw[1]-1 in bk):
      wh.append(uvw[0]-1)
    elif (uvw[0]-1 in wh):
      bk.append(uvw[1]-1)
    elif (uvw[1]-1 in wh):
      bk.append(uvw[0]-1)
    else:
      bk.append(uvw[0]-1)
      wh.append(uvw[1]-1)
  bk = list(set(bk))
  wh = list(set(wh))

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

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    Nodes = [tuple(map(int, input().split())) for i in range(N-1)]
    Nodes = sorted(Nodes, key=lambda x: x[0])
    bw = [True for x in range(N+1)]

    for n in Nodes:
        if (n[2] % 2 != 0) and (bw[n[0]] == bw[n[1]]):
                bw[n[1]] = not bw[n[0]]
    for x in bw[1:]:
        print(int(x))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
node = [0 for i in range(n)] 
for ni in range(n-1):
    u, v, w = [int(i) for i in input().split()]
    if w%2 == 0:
        node[v-1] = node[u-1]
    else:
        node[v-1] = (node[u-1] + 1)%2
print(*node,sep="\n")

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())

passed = [[False for _ in range(N)] for _ in range(N)]
path = [[] for _ in range(N)]
ans = [0] * N

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

def follow_tree(f, td_lst):
    for td in td_lst:
        t, d = td

        if ans[f] == 0 and d % 2 != 0:
            ans[t] = 1
        elif ans[f] == 1 and d % 2 == 0:
            ans[t] = 1

        if passed[f][t] == False:
            passed[f][t] = True
            passed[t][f] = True
            follow_tree(t, path[t])

follow_tree(start, path[start])

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input().strip())

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

edges = []

for i in range(n-1):
	u, v, w = map(int, input().strip().split())
	edges.append((u, v, w))

edges.sort()
for edge in edges:
	u, v, w = edge
	if w % 2:
		# if odd, different color is mandatory
		if color[u] == -1 and color[v] == -1:
			color[u] = 0
			color[v] = 1
		elif color[u] == -1:
			color[u] = 1-color[v]
		elif color[v] == -1:
			color[v] = 1-color[u]
		else:
			if color[u] == color[v]:
				color[u] = 1-color[v]

print('\n'.join(['0' if i == -1 else str(i) for i in color[1:]]))

Submission status: Wrong Answer

Solution code:
import sys

class Graph():
    def __init__(self, n):
        self.even_edges = []
        self.odd_edges = []
        self.n = n
        self.coloring = [-1] * n

    def add_edge(self, a, b, cost):
        a -= 1
        b -= 1
        
        if cost % 2 != 0:
            self.odd_edges.append((a,b))
        else:
            self.even_edges.append((a,b))

    def paint(self):

        # paint odd-edge holders
        while self.odd_edges:
            a, b = self.odd_edges.pop()

            if self.coloring[a] != -1:
                    self.coloring[b] = 1-a
            else:
                if self.coloring[b] != -1:
                    self.coloring[a] = 1-b
                else:
                    self.coloring[a] = 0
                    self.coloring[b] = 1

        # paint even-edges
        while self.even_edges:
            a, b = self.even_edges.pop()

            if self.coloring[a] != 0:
                self.coloring[b] = self.coloring[a]
            elif self.coloring[b] != 0:
                self.coloring[a] = self.coloring[b]
            else:
                self.coloring[a] = self.coloring[b] = 0

        # paint isolated vertices
        self._paint_isolated()

    def _paint_isolated(self):
        for i in range(self.n):
            if self.coloring[i] == -1:
                self.coloring[i] = 0


input = sys.stdin.readline

N = int(input())
g = Graph(N)

while True:
    s = input().rstrip()

    if s == '':
        break

    a,b,c = map(int, s.split())
    g.add_edge(a,b,c)

g.paint()

for x in g.coloring:
    print(x)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

arr = [-1] * (N+1)
for i in range(1, N+1):
    if arr[i] == -1:
        arr[i] = 0
        if i in dict:
            v = dict[i]
            for j in v:
                arr[j] = 1

for i in arr[1:]:
    print(i)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = set([1])
b = set([])
l = []
for i in range(N - 1):
	l.append(list(map(int, input().split())))	
l = sorted(l)
for i in range(N - 1):
	u, v, w = l[i][0], l[i][1], l[i][2]
	if u not in a and u not in b and v not in a and v not in b:
		a.add(u)
	elif u not in a and u not in b:
		u, v = v, u
	if u in a and w % 2 == 0:
		a.add(v)
	elif u in a:
		b.add(v)
	elif u in b and w % 2 == 0:
		b.add(v)
	elif u in b:
		a.add(v)
for i in range(1, N + 1, 1):
	if i in a:
		print('0')
	else:
		print('1')

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
uvw = sorted([[int(i) for i in input().split()] for i in range(n-1)])

res = [0]*n
for u,v,w in uvw:
    if w%2:
        res[v-1] = int(not res[u-1])

print(res)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import operator

N = int(input())
uvw = []
for i in range(N - 1):
    (u, v, w) = tuple(map(int, input().split(' ')))
    if u < v:
        uvw.append((u - 1, v - 1, w))
    else:
        uvw.append((v - 1, u - 1, w))

a = [-1] * N
for (u, v, w) in sorted(uvw, key=operator.itemgetter(0)):
    if w % 2 == 1:
        if a[u] == -1 and a[v] == -1:
            a[u] = 0
            a[v] = 1
        elif a[u] == -1:
            a[u] = 1 - a[v]
        elif a[v] == -1:
            a[v] = 1 - a[u]

for i in a:
    if i < 0:
        print(0)
    else:
        print(i)

Submission status: Wrong Answer

Solution code:
n = int(input())
u_list = []
v_list = []
w_list = []
colors = [2] * (n + 1)

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

u_sorted_key = sorted(range(len(u_list)), key=lambda k: u_list[k])

for key in u_sorted_key:
    u = u_list[key]
    v = v_list[key]
    w = w_list[key]

    if w % 2 == 1:
        if colors[u] == 0:
            colors[v] = 1
        elif colors[u] == 1:
            colors[v] = 0
        else:
            colors[u] = 1
            colors[v] = 0

# 残った2を適当に処理

for c in colors:
    if c == 2:
        print(1)
    else:
        print(c)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
datalist = []
for i in range(N-1):
    data = tuple(map(int,input().split()))
    datalist.append(data)

colorlist = np.array([True for _ in range(N+1)])

for data in datalist:
    if data[2]%2 != 0:
        colorlist[data[1]] = not colorlist[data[0]]

for a in colorlist.astype(np.int)[1:]:
    print(a)

Submission status: Wrong Answer

Solution code:
N=int(input())
UV,W=[],[]
for _ in range(N-1):
    u, v, w=map(int, input().split())
    uv=sorted([u,v])
    UV.append(uv)
    W.append(w)
color=[0 for _ in range(N)]
for i in range(N-1):
    if W[i]%2==0 and color[UV[i][0]-1]==color[UV[i][1]-1]:
        pass
    elif W[i]%2!=0 and color[UV[i][0]-1]==color[UV[i][1]-1]:
        if color[UV[i][0]-1]==0:
            color[UV[i][1]-1]=1
        else: color[UV[i][1]-1]=0
for i in range(N):
    print(color[i])

Submission status: Wrong Answer

Solution code:
n = int(input())
N = ['0'] * n
for i in range(n-1):
    u, v, w = map(int, input().split())
    if w % 2 == 0:
        N[v-1] = N[u-1]
    else:
        if N[u-1] == '0':
            N[v-1] = '1'
        elif N[u-1] == '1':
            N[v-1] = '0'
for a in N:
    print(a)

Submission status: Wrong Answer

Solution code:
n=int(input())

ans=[2]*(n+1)

ki=[]
for i in range(n-1):
  u,v,w = map(int, input().split())
  ki.append([u,v,w])
ki.sort()
    
#
ans[1]=0
#print(ans)
for i in range(n-1):
  u,v,w = ki[i][0],ki[i][1],ki[i][2]
  #print(u,v,w)
  if (not ans[u]==2) and ans[v]==2:
    if ans[u]==0:
      if w%2==0:
        ans[v]=0
      else:
        ans[v]=1
    elif ans[u]==1:
      if w%2==0:
        ans[v]=1
      else:
        ans[v]=0
  elif ans[u]==2 and (not ans[v]==2):    
    if ans[v]==0:
      if w%2==0:
        ans[u]=0
      else:
        ans[u]=1
    elif ans[v]==1:
      if w%2==0:
        ans[u]=1
      else:
        ans[u]=0
        
for i in range(n-1):
  u,v,w = ki[i][0],ki[i][1],ki[i][2]
  #print(u,v,w)
  if (not ans[u]==2) and ans[v]==2:
    if ans[u]==0:
      if w%2==0:
        ans[v]=0
      else:
        ans[v]=1
    elif ans[u]==1:
      if w%2==0:
        ans[v]=1
      else:
        ans[v]=0
  elif ans[u]==2 and (not ans[v]==2):    
    if ans[v]==0:
      if w%2==0:
        ans[u]=0
      else:
        ans[u]=1
    elif ans[v]==1:
      if w%2==0:
        ans[u]=1
      else:
        ans[u]=0

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

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
tree = {}
bw = [-1] * n
for i in range(n-1):
    u, v, w = map(int, input().split())
    if u-1 not in tree:
        tree[u-1] = [(u-1, v-1, w)]
    else:
        tree[u-1].append((u-1, v-1, w))
    if v-1 not in tree:
        tree[v-1] = [(v-1, u-1, w)]
    else:
        tree[v-1].append((v-1, u-1, w))

q = deque()
for nei in tree[0]:
    q.append(nei)
bw[tree[0][0][0]] = 0

while q:
    node = q.popleft()
    print(node)
    if bw[node[1]] != -1:
        if node[2] % 2 == 0:
            bw[node[1]] = bw[node[0]]
    else:
        if node[2] % 2 == 1:
            bw[node[1]] = (bw[node[0]] + 1) % 2
        else:
            bw[node[1]] = bw[node[0]]

    if node[1] in tree:
        for nei in tree[node[1]]:
            if bw[nei[1]] != 0:
                q.append(nei)

for c in bw:
    print(c)

Submission status: Wrong Answer

Solution code:
from copy import deepcopy

N = int(input())
L = [[0]*N for _ in range(N)]
for i in range(N-1):
    a,b,n = map(int,input().split())
    L[a-1][b-1] = n
    L[b-1][a-1] = n
team = [[_] for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i == j:
            continue
        if L[i][j] > 0:
            team[i].append(j)
ans = deepcopy(team[0])
for i in range(1,N):
    for t in team[i][1:]:
        if t in ans and L[t][i]%2 == 0:
            ans.append(t)
ans = set(ans)
for i in range(N):
    if i in ans:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N = int(input())
l = list("k"*(N+1))
for  i in range(N-1):
        a,b,c=map(int, input().split())
        if c%2==1:
                l[a-1]="s"
                l[b-1]="s"
m = 0
for i in range(N):
        if l[i] == "s":
                l[i]=m%2
                if l[i+1]=="s":
                        m+=1
                        
t=0
for i in range(N):
        if l[i]!="k":
                t = l[i]
                break
        
for i in range(N):
        if l[i]=="k":
                l[i]=t
        else:
                t=l[i]
for i in range(N):
        print(l[i])

Submission status: Wrong Answer

Solution code:
def convert(inX):
  if inX == 1:
    return 0
  else:
    return 1

n = int(input())
data = [2]*(n+1)
for i in range(n-1):
  tmp = input().split()
  if int(tmp[2])%2==0:
    if data[int(tmp[0])]!=2 and data[int(tmp[1])]==2: 
      data[int(tmp[1])] = data[int(tmp[0])]
    elif data[int(tmp[0])]==2 and data[int(tmp[1])]!=2:
      data[int(tmp[0])] = data[int(tmp[1])]
    elif data[int(tmp[0])]==2 and data[int(tmp[1])]==2:
      data[int(tmp[0])] = 1
      data[int(tmp[1])] = 1
  else:
    if data[int(tmp[0])]!=2 and data[int(tmp[1])]==2: 
      data[int(tmp[1])] = convert(data[int(tmp[0])])
    elif data[int(tmp[0])]=='2' and data[int(tmp[1])]!='2':
      data[int(tmp[0])] = convert(data[int(tmp[1])])
    elif data[int(tmp[0])]=='2' and data[int(tmp[1])]=='2':
      data[int(tmp[0])] = '1'
      data[int(tmp[1])] = '0'

for i in range(1, len(data)):
  print(data[i])