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:
#!/usr/bin/env python
import bisect
from collections import defaultdict
import math

def main():
    #N = map(int, input().split())
    N = int(input())

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

    colors = [None]*N
    colors[0] = 0
    colored = [0]
    while colored:
        new_colored = []
        for n in colored:
            nodes = edges[n]
            for m, is_even in nodes:
                if colors[m] is None:
                    colors[m] = colors[n] if is_even else 1-colors[n]
                    new_colored.append(m)
        colored = new_colored

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())

dic = [[] for i in range(N)]
for i in range(N-1):
  inf = input().split()
  u = int(inf[0])
  v = int(inf[1])
  w = int(inf[2])
  dic[u-1] +=[(v-1, w)]
  dic[v-1] +=[(u-1, w)]

dist = [-1]*N
stack = []
stack.append(0)
dist[0] = 0

while stack:
  label = stack.pop(-1)
  for i, c in dic[label]:
    if dist[i] == -1:
      dist[i] = dist[label]+c
      stack += [i]

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

Submission status: Wrong Answer

Solution code:
N = int(input())

dic1 = {i:[] for i in range(1,N+1)}
dic2 = {j:[] for j in range(1,N+1)}

for i in range(N-1):
  inf = input().split()
  u = int(inf[0])
  v = int(inf[1])
  w = int(inf[2])
  dic2[u] += [v]
  dic2[v] += [u]
  if w%2==1:
    dic1[u] += [v]
    dic1[v] += [u]

a = set()
b = set()
for i in range(1,N+1):
  for k in dic1[i]:
    if k in a:
      b.add(i)
      break
  else:
    for x in a:
      if x not in dic2:
        b.add(i)
        break
    else:
      a.add(i)
    
for i in range(1,N+1):
  if i in a:
    print(0)
  else:
    print(1)

Submission status: Wrong Answer

Solution code:
N = int(input())

dic = {i:[] for i in range(1,N+1)}

for i in range(N-1):
  inf = input().split()
  u = int(inf[0])
  v = int(inf[1])
  w = int(inf[2])
  if w%2==1:
    dic[u] += [v]
    dic[v] += [u]

a = set()
b = set()
for i in range(1,N+1):
  for k in dic[i]:
    if k in a:
      b.add(i)
      break
  else:
    a.add(i)
    
for i in range(1,N+1):
  if i in a:
    print(0)
  else:
    print(1)

Submission status: Wrong Answer

Solution code:
N = int(input())

a = set()
b = set()

for i in range(N-1):
  inf = input().split()
  u = int(inf[0])
  v = int(inf[1])
  w = int(inf[2])
  if w%2==0:
    a.add(u)
    a.add(v)
  else:
    if u in a:
      b.add(v)
    elif u in b:
      a.add(v)
    else:
      a.add(u)
      b.add(v)

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

Submission status: Wrong Answer

Solution code:
n = int(input())
c = [0] * n
b = [False] * n

def color(x):
    if x == 0:
        return 1
    else:
        return 0

for _ in range(n - 1):
    u, v, w = map(int, input().split())
    if w % 2 == 0:
        if not b[u - 1] and not b[v - 1]:
            c[u - 1] = 0
            c[v - 1] = 0
        elif b[u - 1] and not b[v - 1]:
            c[v - 1] = c[u - 1]
        elif not b[u - 1] and b[v - 1]:
            c[u - 1] = c[v - 1]
    else:
        if not b[u - 1] and not b[v - 1]:
            c[u - 1] = 0
            c[v - 1] = 1
        elif b[u - 1] and not b[v - 1]:
            c[v - 1] = color(c[u - 1])
        elif not b[u - 1] and b[v - 1]:
            c[u - 1] = color(c[v - 1])
    b[u - 1] = True
    b[v - 1] = True

for i in c:
    print(i)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

dist = [None]*N
dist[0] = 0

while len(edges) > 0:
    for edge in edges:
        if dist[edge[0]] == None and dist[edge[1]] != None:
            dist[edge[0]] = edge[2]
            edges.remove(edge)
        elif dist[edge[1]] == None and dist[edge[0]] != None:
            dist[edge[1]] = edge[2]
            edges.remove(edge)

for item in dist:
    print(item%2)

Submission status: Wrong Answer

Solution code:
for i in range(5):
  print(1)

Submission status: Wrong Answer

Solution code:
import sys

N=int(input())
sys.setrecursionlimit(100000)
u = [0]*(N-1)
v = [0]*(N-1)
w = [0]*(N-1)
for i in range(N-1):
    u[i], v[i], w[i] = map(int, input().split())
    u[i] -= 1
    v[i] -= 1

class Path:
    def __init__(self, next_node, length, num):
        self.next=next_node
        self.len=length
        self.num=num

class Node:
    def __init__(self, index):
        self.index = index
        self.paths = []

    def append(self, next_node, length, num):
        self.paths.append(Path(next_node, length, num))

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

past_paths = [0]*(N-1)
paints = [-1]*N

def rec(pos, color):
    print("pos:"+str(pos)+" len:"+str(len(nodes[pos].paths)))
    if paints[pos] == -1:
        paints[pos] = color
    for path in nodes[pos].paths:
        # まだ通ってない道を通る
        if past_paths[path.num] == 0:
            past_paths[path.num] = 1
            rec(path.next, (color + path.len) % 2)
        # さっきの道を戻ってくる

rec(0, 0)

for i in paints:
    print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque


n = int(input())
es = [[] for _ in range(n)]
for _ in range(n-1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    es[u].append((v, w))
    es[v].append((u, w))
print(es)
clr = [None]*n
clr[0] = 0
dq = deque()
dq.append(0)
while dq:
    v = dq.popleft()
    for nv, w in es[v]:
        if clr[nv] is not None:
            continue
        clr[nv] = (clr[v] + w)%2
        dq.append(nv)
print(*clr, sep='\n')

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import queue


class Graph:
    def __init__(self):
        self.graph = defaultdict(list)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, src, dst, weight):
        self.graph[src].append((dst, weight))

    def get_nodes(self):
        return self.graph.keys()


N = int(input())
g = Graph()
q = queue.Queue()
ans = [-1] * N
ans[0] = 0  # 最初の色はどちらでも良いので 0 とする
q.put(0)

for i in range(N-1):
    src, dst, weight = map(int, input().split())
    src -= 1
    dst -= 1
    g.add_edge(src, dst, weight % 2)
    g.add_edge(dst, src, weight % 2)

while not q.empty():
    v = q.get()
    for i in range(len(g.graph[v])):
        u, w = g.graph[v][i]

        # 来てたならスキップ
        if ans[u] != -1:
            continue

        ans[u] = (ans[v] + w) % 2  # w is 0 or 1
        q.put(u)

for a in ans:
    print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

# 何も考えないと 2**N 通りの塗り方がある
# 長さが偶数の組は 同じ色でないといけない
# 長さが奇数の組は 違う色でなければならない

whites = set()
blacks = set()


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

    if w % 2 == 0:
        # 同じ色でないといけない
        if u in whites:
            whites.add(v)
            continue
        if u in blacks:
            blacks.add(v)
            continue
        if v in whites:
            whites.add(u)
            continue
        if v in blacks:
            blacks.add(u)
            continue
        # どちらにも含まれていない場合
        whites.add(u)
        whites.add(v)
    else:
        # 違う色でなければならない
        if u in whites:
            blacks.add(v)
            continue
        if u in blacks:
            whites.add(v)
            continue
        if v in whites:
            blacks.add(u)
            continue
        if v in blacks:
            whites.add(u)
            continue
        # どちらにも含まれていない場合
        whites.add(u)
        blacks.add(v)

for i in range(1, N+1):
    if i in whites:
        print(0)
    elif i in blacks:
        print(1)
    else:
        assert 1 == 2

Submission status: Wrong Answer

Solution code:
from sys import stdin

N = int(stdin.readline().rstrip())
tree = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(N - 1)]
ans = [1] * N

for i in range(N - 1):
    if tree[i][2] % 2 != 0:
        ans[tree[i][1] - 1] = -ans[tree[i][0] - 1] 

for k in ans:
    if k < 0:
        print(0)
    else:
        print(k)

Submission status: Wrong Answer

Solution code:
class UnionFind:
    def __init__(self,n):
        self.parent=[i for i in range(n)]
        self.rank=[0 for _ in range(n)]
        # 親への重み
        self.weight=[0 for _ in range(n)]

    def find(self,x):
        if self.parent[x]==x:
            return x
        else:
            # 重みをrootまでに変える
            self.weight[x] += self.weight[self.parent[x]]
            # rootにつなぎ替える
            y=self.find(self.parent[x])
            self.parent[x]=y
            return y

    # wはxからyまでの距離
    def union(self,x,y,w):
        rootx=self.find(x)
        rooty=self.find(y)
        if self.rank[rootx] < self.rank[rooty]:
            # yの根にxの根をつなぐ
            # xからyまでの距離 = 新たなxから根までの距離 + 根からyまでの距離
            # 新たなxから根までの距離 = もとのxの根から新たな親であるyの根までの距離 + xからもとのxの根までの距離
            # w=(self.weight[rootx](new) +self.weight[x](old)) -self.weight[y])
            self.parent[rootx] = rooty
            self.weight[rootx] = w -self.weight[x] +self.weight[y]
        else:
            # xの根にyの根をつなぐ
            # yからxまでの距離 = 新たなyから根までの距離 + 根からxまでの距離
            # 新たなyから根までの距離 = もとのyの根から新たな親であるxの根までの距離　+ yからもとのyの根までの距離
            self.parent[rooty]=rootx
            self.weight[rooty]=-w-self.weight[y]+self.weight[x]
            if self.rank[rootx]==self.rank[rooty]:
                self.rank[rootx]+=1

n=int(input())
uvw=[list(map(int,input().split())) for _ in range(n-1)]
uf=UnionFind(n)
for u,v,w in uvw:
    u,v,w=u-1,v-1,w
    uf.union(u,v,w)

# print('parent',uf.parent)
# print('weight',uf.weight)
for w in uf.weight:
    print(w%2)

Submission status: Wrong Answer

Solution code:
n = int(input())

l = [k+1 for k in range(n)]

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

l0 = list(map(lambda x: x%2, l))
for j in range(n):
    print(l0[j])

Submission status: Wrong Answer

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

adl = [[] for _ in range(N+1)]
for pi in uvw:
    adl[pi[0]].append([pi[1],pi[2]])
    adl[pi[1]].append([pi[0],pi[2]])
    
memo = [True] * (N+1)
init = uvw[0][0]
memo[init] = False
child = list(adl[init])
d = [0] * N

while child:
    c = child.pop()
    memo[c[0]] = False
    if c[1] % 2 == 0:
        d[c[0]-1] = 0
    else:
        d[c[0]-1] = 1
    for i in adl[c[0]]:
        if memo[i[0]]:child = child + [i]
        
for i in d:
    print(i)

Submission status: Wrong Answer

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

n = int(input())

connect_list = [ [] for i in range(n) ]
distance_list = [ -1 for i in range(n) ]
color_list = [ "Gray" for i in range(n) ]
distance_list[0] = 0

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

search_list = [0]

t = 0
stop = 0
while t < len(search_list):
	s = search_list[t]
	stop = 0
	for i in connect_list[s]:
		if distance_list[i[0]] == -1:
			distance_list[i[0]] = (distance_list[s] + i[1]) % 2
			search_list.append(i[0])
	t += 1
	print(search_list,t)

for i in distance_list:
	print(i)

Submission status: Wrong Answer

Solution code:
n = int(input())
uvw = [list(map(int,input().split())) for i in range(n-1)]
uvw.sort(key=lambda x:x[2]%2)
ans = [1 for i in range(n+1)]

for u,v,w in uvw:
	if w%2 == 1:
		break
	ans[u] = 0
	ans[v] = 0

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

Submission status: Wrong Answer

Solution code:
n = int(input())
uvw = [list(map(int,input().split())) for i in range(n-1)]
uvw.sort(key=lambda x:x[2]%2)
ans = [1 for i in range(n+1)]

for u,v,w in uvw:
	if w%2 == 1:
		break
	ans[u] = 0
	ans[v] = 0
print(*ans[1:])

Submission status: Wrong Answer

Solution code:
graph = []
n = 0
visited = []
ans = []

def dfs(v, ww):
    visited[v] = True
    ans[v] = ww
    for vv, w in enumerate(graph[v]):
        if w != -1 and not visited[vv]:
            dfs(vv, w+ww)


def main():
    global graph, n, visited, ans
    n = int(input())
    graph = [[-1 for i in range(n+1)] for j in range(n+1)]
    visited = [False for i in range(n+1)]
    ans = [0 for i in range(n+1)]
    for i in range(n-1):
        u, v, w = [int(j) for j in input().split()]
        graph[u][v] = w
    for i, _ in enumerate(visited):
        if visited[i] == False:
            dfs(i, 0)
    for i in range(len(ans)):
        if i == 0:
            continue
        print(ans[i]%2)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())

A = {}
COLOR = [1] * N

for _ in range(N - 1):
  u, v, w = map(int, input().strip().split(' '))
  if u not in A: A[u] = {}
  if v not in A: A[v] = {}
  A[u][v] = w
  A[v][u] = w


def test(key, total_w=0):
  if key not in A:
    return

  COLOR[key - 1] = 0 if total_w % 2 == 0 else 1

  v = A.pop(key)
  for k, w in v.items():
    test(k, w + total_w)


queue = deque()
queue.append((1, 0))
current_key = 1

print(A)
while len(queue) > 0:
  key, w = queue.popleft()
  if key not in A:
    continue

  COLOR[key - 1] = 0 if w % 2 == 0 else 1

  for child in A.pop(key).items():
    if child[0] in A:
      queue.append((child[0], w + child[1]))

for c in COLOR:
  print(c)

Submission status: Wrong Answer

Solution code:
N = int(input())

color = [None for _ in range(N)]
graph = [[] for _ in range(N)]

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

stack = [0]
color[0] = 0

while len(stack) > 0:
    n = stack.pop()
    nexts = graph[n]
    for nex in nexts:
        if color[nex[0]] is not None:
            continue
        print(nex)
        if nex[1] % 2 == 0:
            color[nex[0]] = color[n]
        else:
            color[nex[0]] = 1 - color[n]
        stack.append(nex[0])

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

Submission status: Wrong Answer

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

ans = [0]*N

for i in range(N-1):
    if (List[i][2]%2!=0)&(ans[List[i][1]-1]!=1):
        ans[List[i][0]-1]=1

for i in range(N-1):
    if (List[i][2]%2==0)&(ans[List[i][1]-1]!=ans[List[i][0]-1]):
        if ans[List[i][0]-1]==1:
             ans[List[i][1]-1]==1
        else:
            ans[List[i][0]-1]==1
    
for i in range(N):
    print(ans[i])

Submission status: Wrong Answer

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

ans = [0]*N

for i in range(N-1):
    if (List[i][2]%2!=0)&(ans[List[i][1]-1]!=1):
        ans[List[i][0]-1]=1
    
for i in range(N):
    print(ans[i])

Submission status: Wrong Answer

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

ans = [0]*N

for i in range(N-1):
    if (List[i][2]%2!=0)&(ans[List[i][1]-1]!=1):
        ans[List[i][0]]=1
    
for i in range(N):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
n = int(input())
l = []
for i in range(n-1): l.append(list(map(int,input().split())))

graph = [[i] for i in range(n)]
for i in range(n-1):
  u = l[i][0]
  v = l[i][1]
  graph[u-1].append(v-1)
  graph[v-1].append(u-1)

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

queue = [graph[0]]
reached[0] = 1
while len(queue) > 0:
  parent = queue[0][0]
  for j in range(1, len(queue[0])):
    node = queue[0][j]
    if not reached[node]:
      queue.append(graph[node])
      reached[node] = 1
      color[node] = 1*(color[parent] != 1)
    else:
      pass
  queue.pop(0)
  
for i in range(n): print(color[i])

Submission status: Wrong Answer

Solution code:
n = int(input())

brn = [[]]

for i in range(1,n):
    brn.append(list(map(int,input().split())))

#print(brn)

l = [0]*(n+1)

brn.sort()

#print(brn)

for i in range(1,n):
    if brn[i][0] == 1 or l[brn[i][0]] != 0:
        l[brn[i][1]]=l[brn[i][0]]+brn[i][2]
    else:
        l[brn[i][0]]=l[brn[i][1]]+brn[i][2]
    #print(l)

for j in range(1,n+1):

    if l[j] % 2 ==0:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
n = int(input())

### WeightedUnionFind
# 親のノード番号を格納
par = [i for i in range(n+1)]
rank = [0] * (n+1)

# 根までの総重りを格納
weight = [0] * (n+1)

def find(x):
  if par[x] == x:
    return x
  else:
    y = find(par[x])
    weight[x] += weight[par[x]]
    par[x] = y
    return y

def union(x, y, w):
  rx = find(x)
  ry = find(y)
  if rank[rx] < rank[ry]:
    par[rx] = ry
    weight[rx] = w - weight[x] + weight[y]
  else:
    par[ry] = rx
    weight[ry] = -w - weight[y] + weight[x]
    if rank[rx] == rank[ry]:
      rank[rx] += 1

def same(x, y):
  return find(x) == find(y)

# x, y間の重り
def diff(x, y):
  return weight[x] - weight[y]

### WUF end


for i in range(n-1):
  u, v, w = map(int, input().split())
  union(u, v, w)
C = [0]*n
for i in range(n):
  if diff(i+1,1) % 2 == 0:
    C[i]=0
  else:
    C[i]=1
for i in range(n):
  print(C[i])

# 72

Submission status: Wrong Answer

Solution code:
n = int(input())

### WeightedUnionFind
# 親のノード番号を格納
par = [i for i in range(n+1)]
rank = [0] * (n+1)

# 根までの総重りを格納
weight = [0] * (n+1)

def find(x):
  if par[x] == x:
    return x
  else:
    y = find(par[x])
    weight[x] += weight[par[x]]
    par[x] = y
    return y

def union(x, y, w):
  rx = find(x)
  ry = find(y)
  if rank[rx] < rank[ry]:
    par[rx] = ry
    weight[rx] = w - weight[x] + weight[y]
  else:
    par[ry] = rx
    weight[ry] = -w - weight[y] + weight[x]
    if rank[rx] == rank[ry]:
      rank[rx] += 1

def same(x, y):
  return find(x) == find(y)

# x, y間の重り
def diff(x, y):
  return weight[x] - weight[y]

### WUF end


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

for i in range(n):
  if diff(i+1,1) % 2 == 0:
    print('0')
  else:
    print('1')

# 72

Submission status: Wrong Answer

Solution code:
n = int(input())

### WeightedUnionFind改
# 親のノード番号を格納
par = [i for i in range(n+1)]
rank = [0] * (n+1)

# 根までの総重りを格納
weight = [0] * (n+1)

def find(x):
  if par[x] == x:
    return x
  else:
    y = find(par[x])
    weight[x] += weight[par[x]]
    par[x] = y
    return y

def union(x, y, w):
  rx = find(x)
  ry = find(y)
  if rank[rx] < rank[ry]:
    par[x] = y
    weight[rx] = w + weight[y]
  else:
    par[y] = x
    weight[y] = w + weight[x]
    if rank[rx] == rank[ry]:
      rank[rx] += 1

def same(x, y):
  return find(x) == find(y)

# x, y間の重り
def diff(x, y):
  return weight[x] - weight[y]

### WUF end


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

for i in range(n):
  if weight[i+1] % 2 == 0:
    print('0')
  else:
    print('1')

# 72

Submission status: Wrong Answer

Solution code:
N=int(input())
Luvw=[]
for n in range(N-1):
    Luvw.append(list(map(int,input().split())))
Luvw.sort()

ans=[0]*N

for i in range(N-1):
    if Luvw[i][2]%2==0:
        ans[Luvw[i][1]-1]=ans[Luvw[i][0]]
    else:
        ans[Luvw[i][1]-1]=1-ans[Luvw[i][0]]
        
for k in ans:
    print(k)

Submission status: Wrong Answer

Solution code:
N=int(input())
Luvw=[]
for n in range(N-1):
    Luvw.append(list(map(int,input().split())))
Luvw.sort()

ans=[0]*N

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

for k in ans:
    print(k)

Submission status: Wrong Answer

Solution code:
from collections import deque
N=int(input())
uvw=list(list(map(int,input().split())) for _ in range(N-1))

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

colors=[-1]*N
colors[0]=0
queue=deque([1])

while len(queue)>0:
    x=queue.popleft()
    if all(colors[i]!=-1 for i in range(N))==0:
        break
    for j in graph_list[x-1]:
        if colors[j[0]-1]!=2:
            queue.append(j[0])
            colors[j[0]-1]=(colors[x]+j[1])%2
for p in colors:
    print(p)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

n = int(input())
uvw = [[0] * 3 for i in range(n-1)]
for i in range(n-1):
    uvw[i][0],uvw[i][1],uvw[i][2] = map(int,input().split())
color = [-1] * (n+1)

uvw.sort()
color[1] = 0

for i in range(n-1):
    if color[uvw[i][0]] == -1 and color[uvw[i][1]] == -1:
        color[uvw[i][0]] = 0
    if color[uvw[i][0]] == 0:
        if uvw[i][2] % 2 == 0:
            color[uvw[i][1]] = 0
        else:
            color[uvw[i][1]] = 1
    elif color[uvw[i][0]] == 1:
        if uvw[i][2] % 2 == 0:
            color[uvw[i][1]] = 1
        else:
            color[uvw[i][1]] = 0
    if color[uvw[i][1]] == 0:
        if uvw[i][2] % 2 == 0:
            color[uvw[i][0]] = 0
        else:
            color[uvw[i][0]] = 1
    elif color[uvw[i][1]] == 1:
        if uvw[i][2] % 2 == 0:
            color[uvw[i][0]] = 1
        else:
            color[uvw[i][0]] = 0


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

Submission status: Wrong Answer

Solution code:
N=int(input())
n=1
m=1
k=1
Luvw=[]
Lans=[]
while n<=N:
    Lans.append(0)
    n+=1
while m<=N-1:
    Luvw.append(list(map(int, input().split())))
    m+=1

Luvw_sorted=sorted(Luvw)

while k<=N-1:
    if (Luvw_sorted[k-1][2])%2==1:
        Lans[Luvw_sorted[k-1][1]-1]=1-Lans[Luvw_sorted[k-1][0]-1]
    else:
        pass
    k+=1

for j in Lans:
    print(j)

Submission status: Wrong Answer

Solution code:
n = int(input())
tree = [[] for i in range(n+1)]

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

#print(tree)
stack=[1]
visitted=[]
ans=[0]*n

while stack:
    cur = stack.pop()
    pre=ans[cur-1]
    visitted.append(cur)
    for v, w in tree[cur]:
        if v in visitted:
            continue
        if w % 2 == 1:
            ans[v-1]=(pre+1)%2
        else:
            ans[v-1]=pre
        stack.append(v)

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

Submission status: Wrong Answer

Solution code:
from collections import deque

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

dist = [-1 for i in range(n)]
stack = deque([0])
while( len(stack) != 0 ):
    current = stack.popleft()
    current_dist = dist[current]

    for neibor_n, neibor_w in neibors[current]:
        if dist[neibor_n] != -1:
            stack.append( neibor_n )
            dist[neibor_n] = (neibor_w + current_dist) % 2  

for item in dist:
    print(item)

Submission status: Wrong Answer

Solution code:
from collections import deque


class dfs():
    
    def __init__(self, n, el):
        self.n = n
        self.el = el

        self.evenl = [0 for i in range(n)]
        #self.neibors = dict([(i,[]) for i in range(n)])
        self.neibors = [[] for i in range(n)]


    def calc(self, nf):
        used = []
        used.append( nf )
        stack = deque([ nf ])
        while( len(stack) != 0 ):
            current = stack.popleft()
            current_dist = self.evenl[current]
            if len( self.neibors[current] ) == 1:
                continue
            for neibor in self.neibors[current]:
                neibor_n, neibor_w = neibor
                if neibor_n not in used:
                    used.append( neibor_n )
                    stack.append( neibor_n )
                    self.evenl[neibor_n] = self.xor( neibor_w, current_dist )                        

    def xor(self, x1,x2):
        return (x1 + x2) % 2

    def set_neibors(self):
        for e in self.el:
            u,v,w = e
            self.neibors[u-1].append( (v-1,w%2) )
            self.neibors[v-1].append( (u-1,w%2) )


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

m = dfs(n,el)
m.set_neibors()
m.calc(0)

for item in m.evenl:
    print(item)

Submission status: Wrong Answer

Solution code:
N=int(input())
matrix=[[1]*N for i in range(N)]
for i in range(N-1):
    u,v,w=map(int,input().split())
    matrix[u-1][v-1]=w%2
    matrix[v-1][u-1]=w%2
already=[1]
def DFS(PRESENT):
    for j in range(N):
        if matrix[PRESENT][j]==0 and not j+1 in already:
            already.append(j+1)
            DFS(j)
DFS(0)
for i in range(N):
    if i+1 in already:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
n = int(input())
inf = 10 ** 9
graph = [[inf] * n for _ in range(n)]
distance = [inf] * n
distance[0] = 0
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    u, v = u - 1, v - 1
    graph[u][v] = w
    graph[v][u] = w
    if u * v == 0:
        distance[u + v] = w
    else:
        distance[u] = min(distance[v] + w, distance[u])
        distance[v] = min(distance[u] + w, distance[v])
        distance[u] = min(distance[v] + w, distance[u])
for d in distance:
    print(d % 2)

Submission status: Wrong Answer

Solution code:
n = int(input())
inf = 10 ** 9
graph = [[inf] * n for _ in range(n)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    u, v = u - 1, v - 1
    graph[u][v] = w
    graph[v][u] = w
point_list = list(range(n))
visit_list = []
distance = graph[0].copy()
distance[0] = 0
while len(set(point_list) - set(visit_list)) > 0:
    target_list = list(set(point_list) - set(visit_list))
    dis = [(distance[i], i) for i in range(n) if i in target_list]
    u = min(dis)[1]
    for target in target_list:
        if distance[u] + graph[u][target] < distance[target]:
            distance[target] = distance[u] + graph[u][target]
    visit_list.append(u)
for d in distance:
    print(d % 2)

Submission status: Wrong Answer

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

def dfs(v, p, c):
    res[v] = c
    for next in link[v]:
        if next[0] == p:
            continue
        if next[1] % 2:
            dfs(next[0], v, c)
        else:
            dfs(next[0], v, 1-c)


N = int(input())
link = [[]  for _ in range(N)]
res = [0] * N
for i in range(N-1):
    u, v, w = map(int, input().split())
    link[u-1].append([v-1, w])
    link[v-1].append([u-1, w])
    
dfs(0, -1, 1)
#print(link)
for i in range(N):
    print(res[i])

Submission status: Wrong Answer

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

def dfs(v, p, c):
    res[v] = c
    for next in link[v]:
        if next[0] == p:
            continue
        if next[1] % 2:
            dfs(next[0], v, c)
        else:
            dfs(next[0], v, 1-c)


N = int(input())
link = [[]  for _ in range(N)]
res = [0] * N
for i in range(N-1):
    u, v, w = map(int, input().split())
    link[u-1].append([v-1, w])
    link[v-1].append([u-1, w])
    
dfs(0, -1, 1)
print(link)
for i in range(N):
    print(res[i])

Submission status: Wrong Answer

Solution code:
n=int(input())
A=[0]*n
Q=[]
for _ in range(n-1):
    tmp=list(map(int, input().split()))
    Q.append(tmp)
Q.sort(key=lambda x:x[0])
for u,v,w in Q:
    if w%2==1 and A[u-1]==A[v-1]:
        A[v-1] = (A[u-1]+1)%2
for a in A:
    print(a)

Submission status: Wrong Answer

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

def dfs(v, p, c):
    res[v] = c
    for next in link[v]:
        if next[0] == p:
            continue
        if next[1] % 2:
            dfs(next[0], v, c)
        else:
            dfs(next[0], v, 1-c)


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

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

Submission status: Wrong Answer

Solution code:
n=int(input())
A=[0]*n
for _ in range(n-1):
    u,v,w=list(map(int, input().split()))
    if w%2==1 and A[u-1]==A[v-1]:
        A[v-1] = (A[u-1]+1)%2
for a in A:
    print(a)

Submission status: Wrong Answer

Solution code:
n=int(input())
A=[0]*n
for _ in range(n-1):
    u,v,w=list(map(int, input().split()))
    if w%2==1:
        A[v-1] = (A[u-1]+1)%2
for a in A:
    print(a)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)


def func(v, color):
    if visited[v] == 1:
        return
    print(color)
    visited[v] = 1
    for next_v in range(n):
        if graph[v][next_v] != 0:  # vとnext_vが繋がっている場合
            if graph[v][next_v] % 2 == 0:
                func(next_v, color)
            else:
                func(next_v, color ^ 1)


n = int(input())
graph = [[0] * n for _ in range(n)]
visited = [0] * n
for _ in range(n-1):
    u, v, w = map(int, sys.stdin.readline().split())
    graph[u-1][v-1] = w
    graph[v-1][u-1] = w


func(0, 0)

Submission status: Wrong Answer

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


def func(v, color):
    if visited[v] == 1:
        return
    print(color)
    visited[v] = 1
    for next_v in range(n):
        if graph[v][next_v] != 0:  # vとnext_vが繋がっている場合
            if graph[v][next_v] % 2 == 0:
                func(next_v, color)
            else:
                func(next_v, color ^ 1)


n = int(input())
side = [list(map(int, sys.stdin.readline().split())) for _ in range(n-1)]
graph = [[0] * n for _ in range(n)]
visited = [0] * n

for u, v, w in side:
    graph[u-1][v-1] = w
    graph[v-1][u-1] = w


func(0, 0)