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:
N = int(input())
u = []
v = []
w = []

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

for j in range(2):	
	for i in range(N-1):
		if w[i] % 2 == 1:
			l[v[i]-1] = str( int(not(int(l[u[i]-1]))) )
		else:
			l[v[i]-1] = str( int(l[u[i]-1]) )

s = '\n'.join(l)
print(s)

Submission status: Wrong Answer

Solution code:
N = int(input())

ans=[]

for i in range(N):
  ans.append("0")
  
for i in range(N-1):
  u,v,w = map(int, input().split())
  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())
ans = [-1 for i in range(n)]

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

#division

for i in data:
    a,b,c = i
    if c%2 == 0:
        if ans[a-1]+ans[b-1] == -2:
            ans[a-1]=0
            ans[b-1]=0
        elif ans[a-1]+ans[b-1] == -1:
            ans[a-1]=0
            ans[b-1]=0
        elif ans[a-1]+ans[b-1] == 0:
            pass
        elif ans[a-1]+ans[b-1] == 1:
            print("error")
    if c%2 != 0:
        if ans[a-1]+ans[b-1] == -2:
            ans[a-1]=0
            ans[b-1]=1
        elif ans[a-1]+ans[b-1] == -1:
            if ans[a-1]==0:
                ans[b-1]=1
            else:
                ans[b-1]=0
        elif ans[a-1]+ans[b-1] == 0:
            print("error")
        elif ans[a-1]+ans[b-1] == 1:
            pass
        else:
            print("error")

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

Submission status: Wrong Answer

Solution code:
N = int(input())
for _ in range(N):
    print(1)

Submission status: Wrong Answer

Solution code:
class UnionFind:
    def __init__(self,N):
        self.par=[i for i in range(N+1)]
        self.rank=[0 for _ in range(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 same_check(self,x,y):
        return self.find(x)==self.find(y)
    #併合
    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

n=int(input())
uf=UnionFind(n)
for i in range(n-1):
    x,y,z=map(int,input().split())
    if z%2==0:
        uf.union(x,y)
    else:
        if uf.same_check(1,x):
            uf.par[y]=-1
        elif uf.same_check(1,y):
            uf.par[x]=-1
#print(uf.par)
for i in range(1,n+1):
    if uf.same_check(1,i):
        print('0')
    else:
        print('1')

Submission status: Wrong Answer

Solution code:
n = int(input())
tree = []
root = []
for i in range(n-1):
    a, b, c = map(int, input().split())
    tree.append([a, b, c])
    root.append(i)
root.append(n)

tree.sort()
for i in range(n-1):
    if tree[i][2] % 2 == 0:
        root[tree[i][1]-1] = root[tree[i][0]-1]
for i in range(n):
    print(root[i])

Submission status: Wrong Answer

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

par = [i for i in range(N)]
dist = [0]*N
rank = [0]*N

def root(x):
    tmp = x
    while par[tmp]!=tmp:
        tmp = par[tmp]
    return tmp

def unite(x,y,z):
    rx = root(x)
    ry = root(y)
    if rx==ry:
        return
    
    if rank[rx]<rank[ry]:
        rx,ry = ry,rx
    if rank[rx] == rank[ry]:
        rank[rx] += 1
    par[ry] = rx
    dist[ry] = z

def dist_type(x):
    tmp_dist = dist[x]%2
    tmp_x = x
    while par[tmp_x] != tmp_x:
        tmp_x = par[tmp_x]
        tmp_dist += dist[tmp_x]
        tmp_dist %= 2
    return tmp_dist

for u,v,w in uvw:
    unite(u-1,v-1,w%2)

for i in range(N):
    print(dist_type(i))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

def solve(): 
    N = int(input())
    L = np.vstack((np.arange(N+1), np.zeros(N+1, dtype=int)))
    for _ in range(N-1):
        u, v, w = map(int, input().split())
        u, v = sorted([u,v])
        I = np.where(L == L[0, u])[1]
        L[0, I] = L[0, v]
        L[1, I] = L[1, I]^L[1, v]^(w%2)
    res = '\n'.join(map(str,L[1][1:]))
    return str(res)

if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
N = int(input())

lst = [0] * (N+1)

for i in range(N-1):
    u, v, w = map(int, input().split())
    if w%2 == 0:
#        print ('A')
        if lst[u] == 0 and lst [v] == 0:
            lst[u] = 1
            lst[v] = 1
        elif lst[u] != 0:
            lst[v] = lst[u]
        elif lst[v] != 0:
            lst[u] = lst[v]
    if w%2 == 1:
        lst[v] = 1
#    print (lst)

#print (lst)
for j in range(1,N+1):
    print(lst[j])

Submission status: Wrong Answer

Solution code:
import numpy as np
import copy

N = int(input())

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

for _ in range(N - 1):
    u, v, w = list(map(int, input().split()))
    roots[v - 1] += [(u-1, w)]
    roots[u - 1] += [(v-1, w)]
    
def initialize():
    color_list = np.zeros((N, 1))
    return color_list
  
color_list = initialize()
stack = []
color_list[0] = 1

def is_move(vertex, stack, roots):
    target_root = roots[vertex]
    for root in target_root:
        if root[0] not in stack:
            return True
    return False
  
def check_color(a, b):
    if a == b:
        return True
    else:
        return False
      
def splatoon(vertex, stack, roots, color_list):
    stack.append(vertex)
    
    #Recursive end
    if not is_move(vertex, stack, roots):
        
        return 0
    
    #Recursive
    target_root = roots[vertex]
    for root in target_root:
        if root[0] not in stack:
            if root[1] % 2 == 0:
                if not check_color(color_list[vertex], color_list[root[0]]):
                    color_list[root[0]] = color_list[vertex]
            splatoon(root[0], copy.copy(stack), roots, copy.copy(color_list))
            
for i in color_list:
    print(int(i))

Submission status: Wrong Answer

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

n = int(input())
uvw = []
e = {}
for i in range(n-1):
    x = input().split()
    uvw.append([int(x[0])-1, int(x[1])-1, int(x[2])%2])
    e[i] = []
e[n-1] = []

w_mat = [[0 for i in range(n)] for j in range(n)]
for u,v,w in uvw:
    if w%2:
        e[u].append(v)
        e[v].append(u)
        w_mat[u][v] = w
        w_mat[v][u] = w

res = [0]*n
used = [0]*n

def f(idx, pre_idx):
    if w_mat[idx][pre_idx]:
        res[idx] = int(not res[pre_idx])
    for i in e[idx]:
        if not used[i]:
            used[idx] = 1
            f(i, idx)

for i in range(n):
    if not used[i]:
        used[i] = 1
        f(i,i)

for x in res:
    print(x)

Submission status: Wrong Answer

Solution code:
n=int(input())
root  = {}
val = {}
for i in range(n-1):
    u,v,w=map(int,input().split())
    if u not in root:
        root[u] = set()
        root[u].add(v)
    else:
        root[u].add(v)
        
    if v not in root:
        root[v] = set()
        root[v].add(u)
    else:
        root[v].add(u)
        
    val[(u,v)]=w
    val[(v,u)]=w

wset = set()
wset.add(1)
bset = set()
visited = set()
visited.add(1)

def search(start,sset):
    for to in root[start]:
        if to not in visited:
            visited.add(to)
            if (start,to) in val:
                wait = val[(start,to)]
                #print(wait)
                if wait%2 ==0:
                    sset.add(to)
                    sset = search(to,sset)
    return sset

search(1,wset)

for i in range(1,n+1):
    if i in wset:
        print(0)
    else:
        print(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] != -1:
                self.coloring[b] = self.coloring[a]
            elif self.coloring[b] != -1:
                self.coloring[a] = self.coloring[b]
            elif self.coloring[a] == self.coloring[b] == -1:
                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())
count = 0
dict_ans = {}
for i in range(N - 1):
    u, v , w = map(int,input().split())
    if w % 2 == 0:
        if u in dict_ans:
            dict_ans.update({u:dict_ans[u],v:dict_ans[u]})
        else:
            dict_ans.update({u:count,v:count})

    else:
        if count == 0:
            count =+ 1
        else:
            count -= 1
        if u in dict_ans:
            dict_ans.update({u:dict_ans[u],v:count})
        else:
            if count == 0:
                dict_ans.update({u:count,v:count + 1})
            else:
                dict_ans.update({u:count,v:count - 1})
for i in dict_ans:
    print(dict_ans[i])

Submission status: Wrong Answer

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

a = int(input())

list = [2] * a

for i in range(a):
    if i+1 == a:
        break
    b,c,d = map(int,input().split())
    b = b -1
    c = c-1
    if i == 0:
        list[b] = 0
        list[c] = 0
    else:
        if d % 2 == 0:
            if  list[b] != 2:
                list[c] = list[b]
            elif list[c] != 2:
                list[b] = list[c]
        elif d % 2 != 0:
            if  list[b] == 0:
                list[c] = 1
            elif  list[b] == 1:
                list[c] = 0
            elif list[c] == 0:
                list[b] = 1
            elif list[c] == 1:
                list[b] = 0

for k in list:
    print(k)

Submission status: Wrong Answer

Solution code:
class UnionFind(object):

    def __init__(self, N):
        self.tree = [i for i in range(N)]
        self.rank = [0] * (N + 1)

    def find(self, x):
        if self.tree[x] == x:
            return x
        else:
            self.tree[x] = self.find(self.tree[x])  # 経路圧縮
            return self.tree[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)

        # ランクを考慮しない場合はどちらが親になっても良い
        if self.rank[x] < self.rank[y]:
            self.tree[x] = y
        else:
            self.tree[y] = x

        if self.rank[x] == self.rank[y]:
            self.rank[x] += 1

    def has_same_parent(self, x, y):
        return self.find(x) == self.find(y)


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

unionfind = UnionFind(N + 1)
parent = None
for u, v, w in UVW:
    if w % 2 == 0:
        unionfind.unite(u, v)
        parent = unionfind.find(u)

if parent is None:
    for i in range(N):
        print(i % 2)
else:
    parent_idx = unionfind.tree[1:].index(parent) + 1
    for i in range(1, N + 1):
        num = unionfind.find(i)
        if num == parent:
            print(1)
        else:
            if i % 2 == parent_idx % 2:
                print(0)
            else:
                print(1)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

distance = np.array(distance)

color = np.array(['0'] + ['1'] * (N - 1))

for i in range(1, N+1):
    dist = distance[distance[:, 0] == i]
    even_dist = dist[dist[:, 2] % 2 == 0]
    color[even_dist[:, 1] - 1] = color[i - 1]

for c in color:
    print(c)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
n = int(input())
edge = [[] for i in range(n)]
colors = [0] * n
visited = [0] * n
for i in range(n-1):
    u, v, w = [int(item) for item in input().split()] 
    u -= 1
    v -= 1
    edge[u].append([v, w%2])
    edge[v].append([u, w%2])

def dfs(frm, n):
    for tv, tw in edge[n]:
        if tv == frm or visited[tv] == 1:
            continue
        else:
            visited[tv] = 1
            if tw == 1:
                colors[tv] = (colors[frm] + 1) % 2
            else:
                colors[tv] = colors[frm]
            dfs(n, tv)
    return True

colors[0] = 0
dfs(0, 0)
for item in colors:
    print(item)

Submission status: Wrong Answer

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

UVW.sort(key=lambda x: (x[0], x[1]))
ans = [0] * (N + 1)
for uvw in UVW:
    u, v, w, z = uvw
    if w % 2 == 1:
        if ans[u] == ans[v]:
            if ans[v] == 1:
                ans[v] = 0
            else:
                ans[v] = 1

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())

lst = [0] * (N+1)

for i in range(N-1):
    u, v, w = map(int, input().split())
    if w%2 == 0:
#        print ('A')
        if lst[u] == 0 and lst [v] == 0:
            lst[u] = 1
            lst[v] = 1
        elif lst[u] != 0:
            lst[v] = lst[u]
        elif lst[v] != 0:
            lst[u] = lst[v]
    if w%2 == 1:
        lst[u] = 1
#    print (lst)

#print (lst)
for j in range(1,N+1):
    print(lst[j])

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 union(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())
u=WeightedUnionFind(n)
for i in range(n-1):
    x,y,z=map(int,input().split())
    u.union(x,y,z)

ans=[0]
for i in range(2,n+1):
    
    if u.diff(1,i)%2==0:
        ans.append(0)
    else:
        ans.append(1)
for x in ans:
    print(x)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
u_v_w = []
ans = np.full(N+1,2)
u = []
v = []
for i in range(N-1):
	u_v_w.append(list(map(int,input().split())))	
	u.append(u_v_w[i][0])
	v.append(u_v_w[i][1])

ans[1] = 0
spot = [1]

while len(u_v_w) != 0:
	num = -2
	if spot[-1] in u:
		num = u.index(spot[-1])
		pos = 0
	elif spot[-1] in v:
		num = v.index(spot[-1])
		pos = 1
	else:
		num = -1
	if num == -1:
		del spot[-1]
	else:
		if pos == 0:
			if ans[spot[-1]] == 0:
				if u_v_w[num][2] % 2 == 0:
					ans[u_v_w[num][1]] = 0
					spot.append(u_v_w[num][1])
				else:
					ans[u_v_w[num][1]] = 1
					spot.append(u_v_w[num][1])
			else:
				if u_v_w[num][2] % 2 == 0:
					ans[u_v_w[num][1]] = 1
					spot.append(u_v_w[num][1])
				else:
					ans[u_v_w[num][1]] = 0
					spot.append(u_v_w[num][1])					
		else:
			if ans[spot[-1]] == 0:
				if u_v_w[num][2] % 2 == 0:
					ans[u_v_w[num][0]] = 0
					spot.append(u_v_w[num][0])	
				else:
					ans[u_v_w[num][0]] = 1
					spot.append(u_v_w[num][0])	
			else:
				if u_v_w[num][2] % 2 == 0:
					ans[u_v_w[num][0]] = 1
					spot.append(u_v_w[num][0])	
				else:
					ans[u_v_w[num][0]] = 0
					spot.append(u_v_w[num][0])	
		del u_v_w[num]
		del u[num]
		del v[num]

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

Submission status: Wrong Answer

Solution code:
N = int(input())
data = [[int(i) for i in input().split()] for j in range(N-1)]
ans = [0 for i in range(N)]
for i in data:
    if i[2]%2 == 0:
        ans[i[0]-1] = ans[i[1]-1] = 1
for j in ans:
    print(j)

Submission status: Wrong Answer

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

color = [-1] * (n + 1)
for i in range(n - 1):
    u = uvw[i][0]
    v = uvw[i][1]
    if color[u] == -1:
        color[u] = 0
    if color[v] == -1:
        if color[u] == 0:
            color[v] = 1
        else:
            color[v] = 0

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

Submission status: Wrong Answer

Solution code:
def task_d():
    n = int(input())
    uvw = (map(int, input().split()) for _ in range(n-1))

    g = [[None] * (n + 1) for _ in range(n + 1)]
    h = [None] * (n + 1)

    for u, v, w in uvw:
        g[u][v] = w % 2

    for i in range(1, n + 1):
        if h[i] is None:
            h[i] = 0
        for j in range(i + 1, n + 1):
            if g[i][j] is not None:
                if h[j] is not None:
                    h[i] = (g[i][j] - h[j]) % 2
                else:
                    h[j] = (g[i][j] + h[i]) % 2
    for i in h[1:]:
        print(i)

                
    
task_d()

Submission status: Wrong Answer

Solution code:
n = int(input())
edges = [list(map(int, input().split())) for i in range(n-1) ]
# print("n", n)
# print("edges", edges)



# -1 -> Black, 1 -> white, 0 -> not yet
memo = [0 for i in range(n)]
memo[0] = 1

# print("memo", memo)

def paint(index, node_num):
  for edge in edges:
    if (edge[0]==node_num):
      if (edge[2] % 2 == 0):
        # print("edge", edge)
        memo[edge[1]-1] = memo[i]
        edges.remove(edge)
        paint(edge[1]-1, edge[1])
      else:
        # print("edge", edge)
        memo[edge[1]-1] = -memo[i]
        edges.remove(edge)
        paint(edge[1]-1, edge[1])
    if (edge[1]==node_num):
      if (edge[2] % 2 == 0):
        # print("edge", edge)
        memo[edge[0]-1] = memo[i]
        edges.remove(edge)
        paint(edge[0]-1, edge[1])
      else:
        # print("edge", edge)
        memo[edge[0]-1] = -memo[i]
        edges.remove(edge)
        paint(edge[0]-1, edge[1])


for i in range(n):
  node_num = i + 1
  # print("node_num", node_num)
  # print("=======================")
  paint(i, node_num)
  # print("memo", memo)
  # print("=======================")

# print("memo", memo)

for item in memo:
  if (item == 1):
    print("1")
  else:
    print("0")

Submission status: Wrong Answer

Solution code:
N = int(input())

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

answer = [0 for _ in range(N)]
odd_number = {}
for i in uvw:
  if i[2] % 2 != 0:
    if i[0] < i[1]:
      odd_number.setdefault(i[0], []).append(i[1])
    else:
      odd_number.setdefault(i[1], []).append(i[0])
    
for i in range(N):
  if odd_number.get(i+1) is None:
    pass
  else:
    for j in odd_number[i+1]:
      if answer[i] == answer[j-1]:
        if answer[j-1] == 0:
          answer[j-1] = 1
        else:
          answer[j-1] = 0
          
for i in answer:
  print(i)

Submission status: Wrong Answer

Solution code:
N = int(input())

res = []
for i in range(N-1):
    x, y, w = (int(i) for i in input().split())
    res.append((x, y, w))
res = sorted(res)

result = [-1] * N

for tup in res:
    if result[tup[0]-1] == -1:
        result[tup[0]-1] = 0
        if tup[2] % 2 == 0:
            result[tup[1]-1] = 0
        else:
            result[tup[1]-1] = 1
    elif result[tup[0]-1] == 0:
        if tup[2] % 2 == 0:
            result[tup[1]-1] = 0
        else:
            result[tup[1]-1] = 1
    elif result[tup[0]-1] == 1:
        if tup[2] % 2 == 0:
            result[tup[1]-1] = 1
        else:
            result[tup[1]-1] = 0
    
for hoge in result:
    print(hoge)

Submission status: Wrong Answer

Solution code:
n=int(input())
e_list = [[int(i) for i in input().split()] for i in range(n-1)]

even_edge=set()
odd_edge=set()

for e in e_list:
    if e[2]%2==0:
        even_edge.add(e[0])
        even_edge.add(e[1])
    else:
        odd_edge.add(e[0])
        odd_edge.add(e[1])

diff=odd_edge-even_edge

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

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
    if w%2 == 1:
        ans[u-1] = 1
        ans[v-1] = 0



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

Submission status: Wrong Answer

Solution code:
import heapq
N = int(input())
L = []
edge = [[] for i in range(N)]
for i in range(N-1):
  x,y,z = map(int,input().split())
  edge[x-1].append([x-1,y-1,z])
  edge[y-1].append([x-1,y-1,z])
ans = ['TBD']*N
ans[0] = 0
edgelist = []
for i in edge[0]:
  heapq.heappush(edgelist,i)
while len(edgelist) != 0:
  ed = heapq.heappop(edgelist)
  if ans[ed[1]] == 'TBD':
    if ed[2]%2 == 0:
      if ans[ed[0]] == 0:
        ans[ed[1]] = 0
      else:
        ans[ed[1]] = 1
    else:
      if ans[ed[0]] == 0:
        ans[ed[1]] = 1
      else:
        ans[ed[1]] = 0
    for e in edge[ed[1]]:
      if ans[e[1]] == 'TBD':
        heapq.heappush(edgelist,e)
      elif ans[e[0]] == 'TBD':
        heapq.heappush(edgelist,e)
  elif ans[ed[0]] == 'TBD':
    if ed[2]%2 == 0:
      if ans[ed[1]] == 0:
        ans[ed[0]] = 0
      else:
        ans[ed[0]] = 1
    else:
      if ans[ed[1]] == 0:
        ans[ed[0]] = 1
      else:
        ans[ed[0]] = 0
    for e in edge[ed[0]]:
      if ans[e[1]] == 'TBD':
        heapq.heappush(edgelist,e)
      elif ans[e[0]] == 'TBD':
        heapq.heappush(edgelist,e)
print(ans)

Submission status: Wrong Answer

Solution code:

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-1)
    V.append(v-1)
    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], V[i])
        iti = node_list.find(V[i])


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

Submission status: Wrong Answer

Solution code:
N=int(input())

same_dic = {}
diff_dic = {}
for i in range(N-1):
  ui,vi,wi=map(int,input().split())
  if wi%2 == 0:
    if ui not in same_dic:
      same_dic[ui] = [vi]
    else:
      same_dic[ui].append(vi)
  elif wi%2 == 1:
    if ui not in diff_dic:
      diff_dic[ui] = [vi]
    else:
      diff_dic[ui].append(vi)

paint_list = [0]*N
for ui,vlist in sorted(diff_dic.items()):
  for vi in vlist:
    paint_list[vi-1] = (paint_list[ui-1]+1)%2
for ui,vlist in sorted(same_dic.items()):
  for vi in vlist:
    paint_list[vi-1] = paint_list[ui-1]
    
for i in paint_list:
  print(i)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque

class Graph(object):

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

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

    def add_edge(self, a, b, w):
        self.graph[a].append((b, w))

class ANS(object):
    def __init__(self, graph, s):
        self.g = graph.graph
        self.dist = defaultdict(lambda: float('inf'))
        self.check = defaultdict(int)
        self.dist[s] = 0
        self.check[0] = 1

        self.Q = deque()
        self.Q.append(s)

        while self.Q:
        	X = self.Q.pop()
        	for v, w in self.g[X]:
        		if self.check[v] == 0:
        			if self.dist[X] == 0:
        				if w == 1:
        					self.dist[v] = 1
        				else:
        					self.dist[v] = 0
        			else:
        				if w == 0:
        					self.dist[v] = 0
        				else:
        					self.dist[v] = 1
        			self.check[v] = 1
        			self.Q.append(v)


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

N = int(input())
G = Graph()

for i in range(N - 1):
	u, v, w = getlist()
	w = w % 2
	G.add_edge(u - 1, v - 1, w)
	G.add_edge(v - 1, u - 1, w)

AAA = ANS(G, 0)
for i in range(N):
	print(AAA.dist[i])

Submission status: Wrong Answer

Solution code:
n = int(input())

uvw = []

for i in range(n - 1):
    uvw.append([int(x) for x in input().split()])
    
for i in range(n):
    print(0)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    Nodes = []
    for i in range(N-1):
        u, v, w = map(int, input().split())
        if u > v: Nodes.append((v, u, w))
        else: Nodes.append((u, v, w))

    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())

relation_list = []
for i in range(N-1):
    relation_list.append(list(map(int, input().split())))
    
    
answer = [0] * N

for rel in relation_list:
    if rel[2] % 2 == 1:
        tmp = answer[rel[1]-1]
        for ans in answer:
            if ans == tmp:
                ans = (ans + 1) % 2 
                
        answer[rel[0]-1] = (answer[rel[0]-1]+1) % 2

for color in answer:
    print(color)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

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


lw = [[] for i in range(n)]
for path in p:
    lw[path[0]-1].append((path[1]-1, path[2]))
    lw[path[1]-1].append((path[0]-1, path[2]))

iro_list = [0] * n
for i, each_node in enumerate(lw):
    for each_node_list in each_node:
        if each_node_list[1] % 2 != 0:
            iro_list[each_node_list[0]] = 1 - iro_list[i]

for each_iro in iro_list:
    print(each_iro)

Submission status: Wrong Answer

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

def hantei(i, oya=-1):
    if ans[i] != -1:
        return ans[i]
    for u, w in pea[i]:
        if u == oya: continue
        if hantei(u, i):
            re = ans[i] = (hantei(u, i) + w) % 2
            return re

n = int(input())
pea = [[] for _ in range(n + 1)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    w %= 2
    pea[u].append([v, w])
    pea[v].append([u, w])
ans = [-1] * (n + 1)
ans[1] = 1
for i in range(1, n + 1):
    print(hantei(i))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict as df
from collections import deque

n=int(input())
d=df(list)
for i in range(n-1):
    a,b,c=list(map(int,input().split()))
    d[a].append(b)
    d[b].append(a)
for i in range(n):
    if i%2==0:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

adj = [[] for _ in range(N+1)]
for b in branch:
  adj[b[0]].append([b[1],b[2]])
  adj[b[1]].append([b[0],b[2]])

ans = [-1] * (N+1)
deq = deque()
deq.appendleft(1)
while len(deq) > 0:
  v = deq.pop()
  for u, w in adj[v]:
    if ans[u] == -1:
      deq.appendleft(u)
    if ans[v] == -1 and ans[u] == -1:
      if w % 2 == 0:
        ans[v] = 0
        ans[u] = 0
      else:
        ans[v] = 0
        ans[u] = 1
    elif ans[v] == 0:
      if w % 2 == 0:
        ans[u] = 0
      else:
        ans[u] = 1
    else:
      if w % 2 == 0:
        ans[u] = 1
      else:
        ans[u] = 0
if N == 1:
  print(0)
else:
  print(ans[1:])

Submission status: Wrong Answer

Solution code:
N = int(input())
edge = sorted([tuple(map(int, input().split())) for _ in range(N-1)])
color = [None]*N

for u, v, w in edge:
    if w % 2 == 1:
        if color[u-1] is not None:
            color[v-1] = color[u-1] ^ 1
        elif color[v-1] is not None:
            color[u-1] = color[v-1] ^ 1
        else:
            color[u-1] = 0
            color[v-1] = 1
    else:
        if color[u-1] is not None:
            color[v-1] = color[u-1]
        elif color[v-1] is not None:
            color[u-1] = color[v-1]
        else:
            color[u-1] = 0
            color[v-1] = 0
print(*color, sep='\n')