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:

class WeightedUnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        # 根への距離を管理
        self.weight = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            y = self.find(self.par[x])
            # 親への重みを追加しながら根まで走査
            self.weight[x] += self.weight[self.par[x]]
            self.par[x] = y
            return y

    # 併合
    def unite(self, x, y, w):
        rx = self.find(x)
        ry = self.find(y)
        # xの木の高さ < yの木の高さ
        if self.rank[rx] < self.rank[ry]:
            self.par[rx] = ry
            self.weight[rx] = w - self.weight[x] + self.weight[y]
        # xの木の高さ ≧ yの木の高さ
        else:
            self.par[ry] = rx
            self.weight[ry] = -w - self.weight[y] + self.weight[x]
            # 木の高さが同じだった場合の処理
            if self.rank[rx] == self.rank[ry]:
                self.rank[rx] += 1

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

    # xからyへのコスト
    def diff(self, x, y):
        return self.weight[x] - self.weight[y]

n = int(input())
wu = WeightedUnionFind(n)
for _ in range(n-1):
    u,v,w = map(int, input().split())
    u -= 1
    v -= 1
    wu.unite(u,v,w)

col = []
for i in range(n):
    w = wu.diff(0,i)
    col.append(w%2)
print(*col)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
sys.setrecursionlimit(10**9)
 
N = int(input())
uvw = [[int(x) for x in input().split()] for _ in range(N-1)]

print(uvw)
 
def bfs(uvw, status, check, now_p):
    for i in range(N-1):
        if check[i] == 1:
            continue

        if uvw[i][0] == now_p:
            check[i] = 1
            status[uvw[i][1] - 1] = status[now_p - 1] + uvw[i][2]
            bfs(uvw, status, check, uvw[i][1])
            continue
 
        if uvw[i][1] == now_p:
            check[i] = 1
            status[uvw[i][0] - 1] = status[now_p - 1] + uvw[i][2]
            bfs(uvw, status, check, uvw[i][0])
            continue

    return check, status
 
 
#from 1
status = [0] * N
check =  [0] * (N-1)
 
status = bfs(uvw, status, check, 1)[1]
for arrr in status:
    print(arrr%2)

Submission status: Wrong Answer

Solution code:
N = int(input())
color = {i:0 for i in range(1,N+1)}
odddic = {i:[] for i in range(1,N+1)}
anslist=[0,1]
for _ in range(N-1):
  u,v,w = map(int,(input().split()))
  if w%2==1:
    if u>v:
      odddic[v].append(u)
    else:
      odddic[u].append(v)
      
for i in range(1,N+1):
  for j in odddic[i]: 
    color[j] = anslist[not(color[i])]

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

Submission status: Wrong Answer

Solution code:
N = int(input())
color = [0]*N
oddlist = [0]*N
for _ in range(N-1):
  u,v,w = map(int,(input().split()))
  
  if w % 2==1:
    oddlist[u-1] = 1
    oddlist[v-1] = 1
    if oddlist[u-1]==1:
      color[u-1] = 1
      color[v-1] = 0
    elif oddlist[v-1]==1:
      color[u-1] = 0
      color[v-1] = 1
    else:
      color[u-1] = 1
      color[v-1] = 0
  else:  
    color[u-1] = 0
    color[v-1] = 0

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

Submission status: Wrong Answer

Solution code:
N = int(input())
color = [0]*N
oddlist = [0]*N
for _ in range(N-1):
  u,v,w = map(int,(input().split()))
  
  if w % 2==1 or oddlist[u-1]==1 or oddlist[v-1]==1:
    color[u-1] = 0
    color[v-1] = 0
    oddlist[u-1] = 1
    oddlist[v-1] = 1
  else:  
    color[u-1] = 1
    color[v-1] = 1

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

Submission status: Wrong Answer

Solution code:
N = int(input())
color = [0]*N
oddlist = [0]*N
for _ in range(N-1):
  u,v,w = map(int,(input().split()))
  
  if w % 2==1 or oddlist[u-1]==1 or oddlist[v-1]==1:
    color[u-1] = 0
    color[v-1] = 0
    oddlist[u-1] = 1
    oddlist[v-1] = 1
  else:  
    color[u-1] = 1
    color[v-1] = 1

print(color)

Submission status: Wrong Answer

Solution code:
import numpy as np

n = int(input())
c = np.array([[0] * n] * n, dtype=np.uint8)
for i in range(n - 1):
    s = list(map(int, input().split()))
    c[s[0] - 1, s[1] - 1] = s[2]
    c[s[1] - 1, s[0] - 1] = s[2]


def calDistance_dfs(c, d, start=0):
    d[start] = 0
    stack = []
    stack.append(start)
    while len(stack) != 0:
        current = stack.pop()
        for idx, val in enumerate(c[current, :]):
            if val != 0 and d[idx] == -1:
                stack.append(idx)
                d[idx] = d[current] + val


d = np.array([-1] * n)
calDistance_dfs(c, d, 0)

for i in d:
    print(d % 2)

Submission status: Wrong Answer

Solution code:
print(0)

Submission status: Wrong Answer

Solution code:
0

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())

glaph = [[] for _ in range(N)]
w_glaph = [[-1]*N for _ in range(N)]
ans = [0]*N

for _ in range(N-1):
    v,t,w = [int(zz) for zz in input().split()]
    w_glaph[v-1][t-1] = w
    glaph[v-1].append(t-1)
    glaph[t-1].append(v-1)

visited = [0]*N
q = deque([0])
visited[0] = 1
while q:
    now = q.popleft()
    for i in glaph[now]:
        if not visited[i]:
            q.append(i)
            visited[i] = 1
            if w_glaph[now][i]%2 == 0:
                ans[i] = ans[now]
            else:
                ans[i] = ans[now]^1

[print(i) for i in ans]

Submission status: Wrong Answer

Solution code:
n = int(input())
lst = [0] * (n + 1)
group = []
for i in range(n - 1):
	u, v, w = [int(a) for a in input().split()]
	if w % 2 == 0:
		group.append(u)
		group.append(v)        
for i in range(1, n + 1):
	if i in group:
		print(0)
	else:
		print(1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import os, sys, re, math

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

group1 = set()
group2 = set()
for (u, v, w) in uvw:
    if w % 2 != 0:
        if u in group1:
            group2.add(v)
        elif u in group2:
            group1.add(v)
        elif v in group1:
            group2.add(u)
        elif v in group2:
            group1.add(u)
        else:
            group1.add(u)
            group2.add(v)

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

Submission status: Wrong Answer

Solution code:
N = int(input())
UVW = []
for n in range(N - 1):
    uvw = [int(s) for s in input().split(' ')]
    UVW.append(uvw)
UVW.sort()
c = [-1] * (N + 1)
u1, _, _ = UVW[0]
c[u1] = 0

while UVW:
    skip_UVW = []
    for u, v, w in UVW:
        if c[u] == -1 and c[v] == -1:
            skip_UVW.append((u, v, w))
            continue
        if w % 2 == 0:
            if c[u] != -1:
                c[v] = c[u]
            else:
                c[u] = c[v]
        else:
            if c[u] == 1:
                c[v] = 0
            else:
                c[v] = 1
    UVW = skip_UVW

for ret in c[1:]:
    print(ret)

Submission status: Wrong Answer

Solution code:
n = int(input())
uvw = []
d = {}  #  key=index, val=color
for i in range(n - 1):
    u, v, w = list(map(int, input().split(' ')))
    u, v = sorted([u, v])
    uvw.append((u, v, w))
for u, v, w in sorted(uvw):
    if u not in d and v not in d:
        d[u] = 1
        if w % 2 == 0:
            d[v] = d[u]
        else:
            d[v] = not d[u]
    elif v not in d:
        if w % 2 == 0:
            d[v] = d[u]
        else:
            d[v] = not d[u]
    elif u not in d:
        if w % 2 == 0:
            d[u] = d[v]
        else:
            d[u] = not d[v]
    else:
        if w % 2 == 0 and d[u] != d[v]:
            hoge = []
            for key, val in d.items():
                if (d[u] != d[key]):
                    hoge.append(key)
            for key in hoge:
                d[key] = d[u]
for i in range(1, n+1):
    print(int(d.get(i, 0)))

Submission status: Wrong Answer

Solution code:
n = int(input())
uvw = []
d = {}  #  key=index, val=color
for i in range(n - 1):
    u, v, w = list(map(int, input().split(' ')))
    u, v = sorted([u, v])
    uvw.append((u, v, w))
for u, v, w in sorted(uvw):
    if u not in d and v not in d:
        d[u] = 1  # ここは2パターンやらないとダメかも
        if w % 2 == 0:
            d[v] = d[u]
        else:
            d[v] = not d[u]
    elif v not in d:
        if w % 2 == 0:
            d[v] = d[u]
        else:
            d[v] = not d[u]
    elif u not in d:
        if w % 2 == 0:
            d[u] = d[v]
        else:
            d[u] = not d[v]

for i in range(1, n+1):
    print(int(d.get(i, 0)))

# 違う色なら必ず距離が奇数という訳ではない？

Submission status: Wrong Answer

Solution code:
import os, sys, re, math, queue

N = int(input())
colors = [-1 for _ in range(N+1)]
nodes = {}

for _ in range(N-1):
	u,v,w = list(map(int,input().split(' ')))
	if u in nodes:
		nodes[u].append((v,w % 2))
	else:
		nodes[u] = [(v,w % 2)]

	if v in nodes:		
		nodes[v].append((u,w % 2))
	else:
		nodes[v] = [(u,w % 2)]

q = queue.Queue()
fst = list(nodes.keys())[0]
q.put((fst,0))

while not q.empty():
	pos, color = q.get()
	colors[pos] = color
	for cand in nodes[pos]:
		if colors[cand[0]] == -1:
			q.put((cand[0], color + cand[1]))

for i in range(N):
	print(colors[i+1])

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
uvw = [[]for i in range(N)]
for i in range(N-1):
    u,v,w = list(map(int,input().split()))
    uvw[u-1].append([v-1,w%2])
    uvw[v-1].append([u-1,w%2])    
color = [0 for i in range(N)]
que = deque()
que.append(0)
alr = [0]
while que:
    q = que.popleft()
    colorq = color[q]
    for i in uvw[q]:
        if i[0] not in alr:
            que.append(i[0])
            alr.append(i[0])
            if i[1] == colorq:
                color[i[0]] = colorq
            else:
                color[i[0]] = abs(colorq-1)
for i in color:
    print(i)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = [0 for i in range(N)]
flg = [0 for i in range(N)]
for i in range(N-1):
  u, v, w = map(int, input().split())
  if flg[u-1] == 1 and flg[v-1] == 0:
    L[v-1] = (w + L[u-1]) % 2
    flg[v-1] = 1
  elif flg[u-1] == 0 and flg[v-1] == 1:
    L[u-1] = (w + L[v-1]) % 2
    flg[u-1] = 1
  elif flg[u-1] == 0 and flg[v-1] == 0:
    L[v-1] = w % 2
    flg[u-1], flg[v-1] = 1, 1
for i in range(N):
  print(L[i])

Submission status: Wrong Answer

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

def calc(u, a):
  for v, w in a[u]:
    ans[v-1]=(ans[u] + w)
    calc(v-1, a)

calc(0, a)
for i in ans:
  if i % 2 == 0:
    print(0)
  else:
    print(1)

Submission status: Wrong Answer

Solution code:
def prepare(n,arr):
    nodeArr = [[] for i in range(n)]
    for ar in arr:
        nodeArr[ar[0]-1].append([ar[1]-1,ar[2]])
        nodeArr[ar[1]-1].append([ar[0]-1,ar[2]])
    return nodeArr

def dfs(parent,currentNode,distance):

    if len(nodeArr[currentNode]) == 1:
        if currentNode != 0:
            # 末点
            res[currentNode] = distance
            return

    for next in nodeArr[currentNode]:
        nextNode = next[0]
        nextNodeDistance = next[1]
        if nextNode == parent:
            continue
        # dfs(currentNode,nextNode,distance + nextNodeDistance)
        # res[currentNode] = distance


n = int(input())

arr = []

for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

nodeArr = prepare(n, arr)


if n == 1:
    print(1)
else:
    res = [-1 for i in range(n)]

    dfs(-1,0,0)

    result = [(re % 2) for re in res]

    for re in res:
        print(re % 2)

Submission status: Wrong Answer

Solution code:
def prepare(n,arr):
    nodeArr = [[] for i in range(n)]
    for ar in arr:
        nodeArr[ar[0]-1].append([ar[1]-1,ar[2]])
        nodeArr[ar[1]-1].append([ar[0]-1,ar[2]])
    return nodeArr

def dfs(parent,currentNode,distance):

    if len(nodeArr[currentNode]) == 1:
        if currentNode != 0:
            # 末点
            res[currentNode] = distance
            return
    #
    # for next in nodeArr[currentNode]:
    #     nextNode = next[0]
    #     nextNodeDistance = next[1]
    #     if nextNode == parent:
    #         continue
    #     dfs(currentNode,nextNode,distance + nextNodeDistance)
    #     res[currentNode] = distance


n = int(input())

arr = []

for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

nodeArr = prepare(n, arr)


if n == 1:
    print(1)
else:
    res = [-1 for i in range(n)]

    dfs(-1,0,0)

    result = [(re % 2) for re in res]

    for re in res:
        print(re % 2)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

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


def dfs(a):
    dist = [-1] * (n + 1)
    stack = [a]
    dist[a] = 0
    while stack:
        v = stack.pop()
        for b, w in graph[v]:
            if dist[b] == -1:
                dist[b] = (dist[v] + w) % 2
                stack.append(b)
            else:
                continue
    return dist


dist = dfs(1)
print(*dist[1:], sep="\n")

Submission status: Wrong Answer

Solution code:
n = int(input().strip())
m = [0]*n

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

for M in m:
    print(M)

Submission status: Wrong Answer

Solution code:
N=int(input())
uvw=[list(map(int, input().split())) for i in range(N-1)]
mat=[[0]*(N+1) for i in range(N+1)]
nodelink=[[] for i in range(N+1)]
for i in range(N-1):
  mat[uvw[i][0]][uvw[i][1]]=uvw[i][2]
  mat[uvw[i][1]][uvw[i][0]]=uvw[i][2]
  nodelink[uvw[i][0]].append(uvw[i][1])
  nodelink[uvw[i][1]].append(uvw[i][0])
  
color=[-1]*(N+1)

def f(nuru, iro):
  color[nuru]=iro
  tonari=nodelink[nuru]
  for nuruyo in tonari:
    if color[nuruyo]<0:
      if mat[nuru][nuruyo]==0:
        f(nuruyo, iro)
      else:
        f(nuruyo, 1-iro)
      
f(1, 0)

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

Submission status: Wrong Answer

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
#from scipy.misc import comb

N = int(input())

G = [[] for _ in range(N+1)]

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

dist = [-1]*(N+1)
dist[1] = 0

def dfs(u):
    for edge in G[u]:
        if dist[edge[0]] != -1:
            dist[edge[0]] = (dist[u]+edge[1])%2
            dfs(edge[0])

dfs(1)

for i in range(1,N+1):
    print(dist[i])

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[list(map(int,input().split())) for i in range(n-1)]
l=sorted(l)
ll=[2 for i in range(n)]
ll[0]=0
for i in range(n-1):
    if ll[l[i][0]-1]==0 or ll[l[i][0]-1]==1 or ll[l[i][1]-1]==0 or ll[l[i][1]-1]==1:
        if l[i][2]//2==l[i][2]/2:
            if ll[l[i][0]-1]==0:
                ll[l[i][1]-1]=0
            elif ll[l[i][1]-1]==0:
                ll[l[i][0]-1]=0
        else:
            if ll[l[i][0]-1]==0:
                ll[l[i][1]-1]=1
            elif ll[l[i][1]-1]==0:
                ll[l[i][0]-1]=1

for i in ll:
    print(i)

Submission status: Wrong Answer

Solution code:
n = int(input())
g = [[] for _ in range(n)]


for _ in range(n-1):
    u,v,w = map(int,input().split())
    g[u-1].append((v-1,w))
    g[v-1].append((u-1,w))
    
ans = [0] * n
for i in range(n):
    for c in g[i]:
        if c[1] % 2 == 1:
            if ans[i] == 0:
                ans[c[0]] = 1
            else:
                ans[c[0]] = 0
for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
n = int(input())
g = [[] for _ in range(n)]


for _ in range(n-1):
    u,v,w = map(int,input().split())
    g[u-1].append((v-1,w))
    g[v-1].append((u-1,w))
    
ans = [0] * n
for i in range(n):
    for c in g[i]:
        if c[1] % 2 == 0:
            ans[i] = 0
            ans[c[0]] = 0
        else:
            ans[i] = 1
            ans[c[0]] = 0
for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
n = int(input())
g = [[] for _ in range(n)]


for _ in range(n-1):
    u,v,w = map(int,input().split())
    g[u-1].append((v-1,w))
    g[v-1].append((u-1,w))
    
ans = [0] * n
for i in range(n):
    for c in g[i]:
        if c[1] % 2 == 0:
            ans[i] = 0
            ans[c[0]] = 0
        else:
            ans[i] = 0
            ans[c[0]] = 1
for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
n = int(input())
g = [[] for _ in range(n)]


for _ in range(n-1):
    u,v,w = map(int,input().split())
    g[u-1].append((v-1,w))
    g[v-1].append((u-1,w))
    
ans = [0] * n
for i in range(n):
    for c in g[i]:
        if c[1] % 2 == 0:
            if ans[i] % 2 == 0:
                ans[c[0]] = 0
            else:
                ans[c[0]] = 1
        else:
            if ans[i] % 2 == 0:
                ans[c[0]] = 1
            else:
                ans[c[0]] = 0
for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
input = sys.stdin.readline
n = int(input())
abe = [list(map(int, input().split(' '))) for i in range(n-1)]
graph = [[] for i in range(n+1)]
parent = [0 for i in range(n+1)]
edge = [0 for i in range(n+1)]
root = 1
order = []
deq = deque([root])
for a,b,e in abe:
    graph[a].append(b)
    graph[b].append(a)
while deq:
    x = deq.popleft()
    order.append(x)
    for y in graph[x]:
        if parent[x] == y:
            continue
        parent[y] = x
        deq.append(y)
for a,b,e in abe:
    if parent[a] == b:
        continue
    edge[b] = e
ans = [0 for i in range(n+1)]
for x in order:
    if x == 1:
        continue
    if edge[x]%2 == 0:
        ans[x] = ans[parent[x]]
    else:
        ans[x] = 1-ans[parent[x]]
del ans[0]
print(*ans,sep="\n")

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
input = sys.stdin.readline
n = int(input())
abe = [list(map(int, input().split(' '))) for i in range(n-1)]
graph = [[] for i in range(n+1)]
parent = [0 for i in range(n+1)]
edge = [0 for i in range(n+1)]
root = 1
order = []
deq = deque([root])
for a,b,e in abe:
    graph[a].append(b)
    graph[b].append(a)
while deq:
    x = deq.popleft()
    order.append(x)
    for y in graph[x]:
        if parent[x] == y:
            continue
        parent[y] = x
        deq.append(y)
for a,b,e in abe:
    if parent[a] == b:
        continue
    edge[b] = e
ans = [0 for i in range(n+1)]
for x in order:
    if x == 1:
        continue
    if edge[x]%2 == 0:
        ans[x] = ans[parent[x]]
    else:
        ans[x] = 1-ans[parent[x]]
print(*ans,sep="\n")

Submission status: Wrong Answer

Solution code:
N=int(input())
L=[0]*(N-1)
A=[[[0,0]] for i in range(N+1)]
B=[0]*(N+1)
for i in range(N-1):
    L[i]=list(map(int,input().split()))
L=sorted(L,key=lambda x: (x[0],x[1]))

for i in range(len(L)):
    if(L[i][2]%2==1):
        B[L[i][1]]=B[L[i][0]]+1
    else:
        B[L[i][1]]=B[L[i][0]]

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

Submission status: Wrong Answer

Solution code:
n=int(input())

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

from collections import Counter
hen={}

graph = [[] for _ in range(n+1)]
for i in range(n-1):
  a,b,c=map(int,input().split())
  graph[a].append(b)
  graph[b].append(a)
  hen[str(a)+","+str(b)]=c
  hen[str(b)+","+str(a)]=c

def dfs(v):
  color[v]=0
  dist = [-1] * (n+1)
  stack = [v]
  dist[v] = 0
  while stack:
      v = stack.pop()
      dw = dist[v] + 1
      for w in graph[v]:
          if dist[w] >= 0:
              continue
          dist[w] = dw
          #辞書からvalueを取り出す
          temp=str(v)+","+str(w)
          if hen[temp]%2:
            color[w]=1-color[v]
          else:
            color[w]=0
          stack.append(w)
  return color
a=dfs(1)
print(*a[1:],sep="\n")

#なんでこれで間違ってるのかわからんが…

Submission status: Wrong Answer

Solution code:
n=int(input())

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

from collections import Counter
hen={}

graph = [[] for _ in range(n+1)]
for i in range(n-1):
  a,b,c=map(int,input().split())
  graph[a].append(b)
  graph[b].append(a)
  hen[str(a)+","+str(b)]=c
  hen[str(b)+","+str(a)]=c

def dfs(v):
  color[v]=0
  dist = [-1] * (n+1)
  stack = [v]
  dist[v] = 0
  while stack:
      v = stack.pop()
      dw = dist[v] + 1
      for w in graph[v]:
          if dist[w] >= 0:
              continue
          dist[w] = dw
          #辞書からvalueを取り出す
          temp=str(v)+","+str(w)
          if hen[temp]%2:
            color[w]=1-color[v]
          else:
            color[w]=0
          stack.append(w)
  return color
a=dfs(1)
print(*a[1:],sep="\n")

Submission status: Wrong Answer

Solution code:
n=int(input())

color=[-1]*(n+1)
hen=[[0]*(n+1) for i in range(n+1)] #これo(n^2)かかる？

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

def dfs(v):
  color[v]=0
  dist = [-1] * (n+1)
  stack = [v]
  dist[v] = 0
  while stack:
      v = stack.pop()
      dw = dist[v] + 1
      for w in graph[v]:
          if dist[w] >= 0:
              continue
          dist[w] = dw
          if hen[v][w]%2:
            color[w]=1-color[v]
          else:
            color[w]=0
          stack.append(w)
  return color
a=dfs(1)
print(*a[1:],sep="\n")

Submission status: Wrong Answer

Solution code:
n=int(input())

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

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

for u,v,w in info:
  if color[u]==color[v]==-1:
    color[u]=0
    if w%2:
      color[v]=1-color[u]
    else:
      color[v]=color[u]
  elif color[u]!=-1:
    if w%2:
      color[v]=1-color[u]
    else:
      color[v]=color[u]
  elif color[v]!=-1:
    if w%2:
      color[u]=1-color[v]
    else:
      color[u]=color[v]

print(*color[1:],sep="\n")

Submission status: Wrong Answer

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

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

A.sort()
D=deque(A)
u,v,w=D.popleft()
if w == 0:
    if C[u] == "" and C[v] == "":
        C[u]="0"
        C[v]="0"
    elif C[u] == "0" and C[v] == "":
        C[v] = "0"
    elif C[u] == "" and C[v] == "0":
        C[u] = "0"
    elif C[u] == "1" and C[v] == "":
        C[v] = "1"
    elif C[u] == "" and C[v] == "1":
        C[u] = "1"
elif w == 1:
    if C[u] == "" and C[v] == "":
        C[u]="0"
        C[v]="1"
    elif C[u] == "0" and C[v] == "":
        C[v] = "1"
    elif C[u] == "" and C[v] == "0":
        C[u] = "0"
    elif C[u] == "1" and C[v] == "":
        C[v] = "0"
    elif C[u] == "" and C[v] == "1":
        C[u] = "0"


while D:
    u,v,w=D.popleft()
    if w==0:
        if C[u]=="" and C[v]=="":
            D.append([u,v,w])
        elif C[u]=="0" and C[v]=="":
            C[v]="0"
        elif C[u]=="" and C[v]=="0":
            C[u]="0"
        elif C[u]=="1" and C[v]=="":
            C[v]="1"
        elif C[u]=="" and C[v]=="1":
            C[u]="1"
    elif w==1:
        if C[u]=="" and C[v]=="":
            D.append([u,v,w])
        elif C[u]=="0" and C[v]=="":
            C[v]="1"
        elif C[u]=="" and C[v]=="0":
            C[u]="0"
        elif C[u]=="1" and C[v]=="":
            C[v]="0"
        elif C[u]=="" and C[v]=="1":
            C[u]="0"
for i in C:
    print(i)

Submission status: Wrong Answer

Solution code:
N=int(input())

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

A.sort()

for i in range(N-1):
    u,v,w=A[i][0],A[i][1],A[i][2]
    if w==0:
        if C[u]=="" and C[v]=="":
            C[u]="0"
            C[v]="0"
        elif C[u]=="0" and C[v]=="":
            C[v]="0"
        elif C[u]=="1" and C[v]=="":
            C[v]="1"
    elif w==1:
        if C[u]=="" and C[v]=="":
            C[u]="0"
            C[v]="1"
        elif C[u]=="0" and C[v]=="":
            C[v]="1"
        elif C[u]=="1" and C[v]=="":
            C[v]="0"
for i in C:
    print(i)

Submission status: Wrong Answer

Solution code:
N=int(input())

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

A.sort()

for i in range(N-1):
    u,v,w=A[i][0],A[i][1],A[i][2]
    if w%2==0:
        if C[u]=="" and C[v]=="":
            C[u]="0"
            C[v]="0"
        elif C[u]=="0" and C[v]=="":
            C[v]="0"
        elif C[u]=="1" and C[v]=="":
            C[v]=="1"
    elif w%2==1:
        if C[u]=="" and C[v]=="":
            C[u]="0"
            C[v]="1"
        elif C[u]=="0" and C[v]=="":
            C[v]="1"
        elif C[u]=="1" and C[v]=="":
            C[v]="0"
for i in C:
    print(i)

Submission status: Wrong Answer

Solution code:
N = int(input())
tree = [list() for _ in range(N)]
for _ in range(N-1):
    u, v, w = map(int, input().split())
    tree[u-1].append((v-1, w))
    tree[v-1].append((u-1, w))
stack = [0]
ans = [0] * N
searched = [False] * N
while stack:
    node = stack.pop()
    searched[node] = True
    for v, w in tree[node]:
        if searched[node]:
            continue
        if w % 2 != 0:
            ans[v] = ans[node] ^ 1
        else:
            ans[v] = ans[node]
        stack.append(v)
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
N = int(input())
tree = [list() for _ in range(N)]
for _ in range(N-1):
    u, v, w = map(int, input().split())
    tree[u-1].append((v-1, w))
    tree[v-1].append((u-1, w))
stack = [0]
ans = [0] * N
searched = [False] * N
while stack:
    node = stack.pop()
    for v, w in tree[node]:
        if not searched[v]:
            searched[v] = True
            if w % 2 != 0:
                ans[v] = ans[node] ^ 1
            else:
                ans[v] = ans[node]
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
N = int(input())
tree = [list() for _ in range(N)]
for _ in range(N-1):
    u, v, w = map(int, input().split())
    tree[u-1].append((v-1, w))
    tree[v-1].append((u-1, w))
ans = [0] * N
searched = [False] * N
for i in range(N):
    for t in tree[i]:
        v, w = t
        if not searched[v]:
            searched[v] = True
            if w % 2 != 0:
                ans[v] = ans[i] ^ 1
            else:
                ans[v] = ans[i]
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
N = int(input())
tree = [list() for _ in range(N)]
for _ in range(N-1):
    u, v, w = map(int, input().split())
    tree[u-1].append((v-1, w))
    tree[v-1].append((u-1, w))
ans = [0] * N
searched = [False] * N
for i in range(N):
    for t in tree[i]:
        v, w = t
        if not searched[v]:
            searched[v] = True
            if w % 2 != 0:
                ans[v] = ans[i] ^ 1
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
N = int(input())
tree = [list() for _ in range(N)]
for _ in range(N-1):
    u, v, w = map(int, input().split())
    tree[u-1].append((v-1, w))
    tree[v-1].append((u-1, w))
ans = [0] * N
searched = [False] * N
for i in range(N):
    for i, t in enumerate(tree[i]):
        v, w = t
        if not searched[v]:
            searched[v] = True
            if w % 2 != 0:
                ans[v] = ans[i] ^ 1
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(pow(10, 7))

def dfs(v, c, total):
	color[v] = c
	for x in glaph[v]:
		u, w = x[0], x[1]
		if color[u] != -1:
			continue
		total = (total+w)%2
		if total == 0:
			dfs(u, c, total)
		else:
			dfs(u, 1-c, total)
			
n = int(input())
glaph = [[]*n for _ in range(n)]
color = [-1]*n

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

dfs(v=0, c=0, total=0)
#for i in range(n):
#	print(i, glaph[i])
for i in color:
	print(i)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(pow(10, 7))

def dfs(v, c, total):
	if color[v] == -1:
		color[v] = c
	else:
		return
	for x in glaph[v]:
		u, w = x[0], x[1]
		total = (total+w)%2
		#print(v, u, total)
		if total == 0:
			dfs(u, c, total)
		else:
			dfs(u, 1-c, total)
			
n = int(input())
glaph = [[]*n for _ in range(n)]
color = [-1]*n

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

dfs(v=0, c=0, total=0)
#for i in range(n):
#	print(i, glaph[i])
for i in color:
	print(i)

Submission status: Wrong Answer

Solution code:
n = int(input())
glaph = [[-1]*n for _ in range(n)]
color = [-1]*n

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

def dfs(v, c, total):
	if color[v] != -1:
		return
	color[v] = c
	for x in range(n):
		if glaph[v][x] == -1:
			continue
		total+=glaph[v][x]
		if total%2 == 0:
			dfs(x, 0, total)
		else:
			dfs(x, 1, total)

dfs(0, 0, 0)
for i in color:
	print(i)

Submission status: Wrong Answer

Solution code:
n = int(input())
glaph = [[-1]*n for _ in range(n)]
color = [-1]*n

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

def dfs(v, c):
	if color[v] != -1:
		return
	color[v] = c
	for x in range(n):
		if glaph[v][x] == -1:
			continue
		if glaph[v][x]%2 == 0:
			dfs(x, 0)
		else:
			dfs(x, 1)

dfs(0, 0)
for i in color:
	print(i)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = [1] * n
for _ in range(n-1):
    u, v, w = map(int, input().split())
    if w % 2 != 0:
        ans[v-1] = ans[u-1] ^ 1
    else:
        ans[v-1] = ans[u-1]
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = [0] * n
for _ in range(n-1):
    u, v, w = map(int, input().split())
    if w % 2 != 0:
        ans[v-1] = ans[u-1] ^ 1
print(*ans, sep='\n')