Problem:


Score : 400 points

Problem Statement

We have a tree with N vertices numbered 1 to N.
The i-th edge in the tree connects Vertex u_i and Vertex v_i, and its length is w_i.
Your objective is to paint each vertex in the tree white or black (it is fine to paint all vertices the same color) so that the following condition is satisfied:

For any two vertices painted in the same color, the distance between them is an even number.

Find a coloring of the vertices that satisfies the condition and print it. It can be proved that at least one such coloring exists under the constraints of this problem.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq u_i < v_i \leq N

1 \leq w_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
u_1 v_1 w_1
u_2 v_2 w_2
.
.
.
u_{N - 1} v_{N - 1} w_{N - 1}

Output

Print a coloring of the vertices that satisfies the condition, in N lines.
The i-th line should contain 0 if Vertex i is painted white and 1 if it is painted black.

If there are multiple colorings that satisfy the condition, any of them will be accepted.

Sample Input 1

3
1 2 2
2 3 1

Sample Output 1

0
0
1

Sample Input 2

5
2 5 2
2 3 10
1 3 8
3 4 2

Sample Output 2

1
0
1
0
1



Sample input:
3
1 2 2
2 3 1


Sample output:
0
0
1

Submission status: Wrong Answer

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


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


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

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

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

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


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

for _ in range(N-1):
    u, v, w = map(int, input().split())
    U.append(u-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('0')
    else:
        print('1')

Submission status: Wrong Answer

Solution code:
n = int(input())
e = [[int(i) for i in input().split()] for i in range(n-1)]
colors = {i:True for i in range(1,n+1)}
for u,v,w in e:
    if w%2 == 1:
        colors[v] = not colors[u]
    if w%2 == 0 and colors[v] != colors[u]:
        colors[v] = colors[u]
for cont in colors.values():
    if cont is True:
        print(0)
    else:
        print(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 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)

'''
print(u.par)
print(u.rank)
print(u.weight)    
'''

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

Submission status: Wrong Answer

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

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 sset:
            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:
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(N):	
	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())

INF = 10000000000

d = [[INF for _ in range(N)] for _ in range(N)]
for i in range(N):
    d[i][i] = 0

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

for i in range(N):
    for j in range(N):
        for k in range(N):
            d[i][j] = min(d[i][j], d[i][k] + d[k][j])

ans = []
for i in range(N):
    ans.append(d[0][i] % 2)

for a in ans:
    print(a)

Submission status: Wrong Answer

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

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

    def find(self, x):
        if self.par[x] == x or self.par[x] == -1 * 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 separate(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = -1 * x
        else:
            self.par[y] = -1 * y
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

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

tree = UnionFind(n)
for i in range(n-1):
    u = a[i][0]
    v = a[i][1]
    w = a[i][2]
    if w % 2 == 0:
        tree.union(u, v)
    else:
        tree.separate(u, v)

counter = []
for i in range(1, n+1):
    counter.append(tree.find(i))

counter1 = list(set(counter))
for i in range(n):
    if counter[i] == counter1[0]:
        print('0')
    else:
        print('1')

Submission status: Wrong Answer

Solution code:
n=int(input())

ans=[2]*(n+1)

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

for i in range(n-1):
  u,v,w = ki[i][0],ki[i][1],ki[i][2]
  if ans[u]==2 and ans[v]==2:
    ans[v]=0
        
for i in range(n-1):
  u,v,w = ki[i][0],ki[i][1],ki[i][2]
  #print(u,v,w)
  if (not ans[u]==2) and ans[v]==2:
    if ans[u]==0:
      if w%2==0:
        ans[v]=0
      else:
        ans[v]=1
    elif ans[u]==1:
      if w%2==0:
        ans[v]=1
      else:
        ans[v]=0
  elif ans[u]==2 and (not ans[v]==2):    
    if ans[v]==0:
      if w%2==0:
        ans[u]=0
      else:
        ans[u]=1
    elif ans[v]==1:
      if w%2==0:
        ans[u]=1
      else:
        ans[u]=0
        
for i in range(1,n+1):
  print(ans[i])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun May 19 21:04:38 2019

@author: Yamazaki Kenichi
"""

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

#import sys
#sys.setrecursionlimit(10**6)

class UnionFind(object):
    def __init__(self, size):
        self.table = [[-1,1,1] for _ in range(size)]

    def find(self, x):
        if self.table[x][0] < 0:
            return x
        else:
            self.table[x][0] = self.find(self.table[x][0])
            self.table[x][1] *= self.table[self.find(self.table[x][0])][2]
#            self.table[x][2] *= self.table[self.find(self.table[x][0])][3]
            return self.table[x][0]
        
    def union(self, x, y, flg):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 != s2:
            if self.table[s1][0] <= self.table[s2][0]:
                self.table[s1][0] += self.table[s2][0]
                self.table[s2][0] = s1
                self.table[s2][1] = self.table[x][1] * (1 if flg else -1)
                self.table[s2][2] = self.table[x][1] * (1 if flg else -1)
            else:
                self.table[s2][0] += self.table[s1][0]
                self.table[s1][0] = s2
                self.table[s1][1] = self.table[y][1] * (1 if flg else -1)
                self.table[s1][2] = self.table[y][1] * (1 if flg else -1)
            return True
        else:
            return False

uf = UnionFind(N+1)
for c in uvw:
    flg = (c[2] % 2 == 0) 
    uf.union(c[0],c[1],flg)
#    print(uf.table)
for i in range(1,N+1):
    uf.find(i)

for i in range(1,N+1):
    print(uf.table[i][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 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)

'''
print(u.par)
print(u.rank)
print(u.weight)    
'''

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

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())
edges = [list(map(int, input().split())) for _ in range(N-1)]
vertices = [0]*(N+1)
visited = [0]*(N+1)

queue = deque([1])

while queue:
    s = queue.popleft()

    for i in edges:
        if i[0] == s:
            if visited[s] == 0:
                visited[s] = 1
                queue.append(s+1)
            if i[2] % 2 == 1:
                if vertices[i[0]] == 0:
                    vertices[i[1]] = 1
                else:
                    vertices[i[1]] = 0

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

Submission status: Wrong Answer

Solution code:
n = int(input())

Li = [0] * (n + 1)


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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    edges = [[-1]*N for n in range(N)]
    colors = [-1]*N
    for i in range(N-1):
        u, v, w = [int(n) for n in input().split()]
        edges[u-1][v-1] = w
        edges[v-1][u-1] = w
    colors[0] = 0
    bfs(edges, 0, colors)
    for i in colors:
        print(i)
            
def bfs(edges, current, colors):
    ed = edges[current]
    for i in range(len(ed)):
        if ed[i] == -1:
            continue
        if colors[i] != -1:
            continue
        
        if ed[i] % 2 == 0:
            colors[i] = colors[current]
        else:
            colors[i] = colors[current] ^ 1
        bfs(edges, i, colors)
    for i in colors:
        print(i, end=" ")
    print()

        

        
main()

Submission status: Wrong Answer

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

list_ans = []
list_ans.append(0)
buf_0 = 0
buf_1 = 1
print(uvw)
for i in range(1,N-1):
    if list_ans[i-1] == 0:
        buf_0 = 0
        buf_1 = 1
    else:
        buf_0 = 1
        buf_1 = 0
    print(i)
    print(uvw[i])
    if (uvw[i][2] % 2) == 0:
        list_ans.append(buf_0)
    else:
        list_ans.append(buf_1)

Submission status: Wrong Answer

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


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


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

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

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

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


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

for _ in range(N-1):
    u, v, w = map(int, input().split())
    U.append(u-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())
ev = [False for _ in range(n)]
od = [False for _ in range(n)]
for i in range(n-1):
    u, v, w = map(int,input().split())
    if w % 2 == 0:
        ev[u-1] = True
        ev[v-1] = True
    else:
        od[u-1] = True
        od[v-1] = True

t = False
now = 0
for e, o in zip(ev, od):
    if e and o:
        t = True
    if t:
        print(now)
        t = False
    elif e:
        print(now)
    elif o:
        now = 1 - now
        print(now)

Submission status: Wrong Answer

Solution code:
N = int(input())
ansList = [-1] * N #塗られてないところを-1で初期化する

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

string_list.sort()  #第一引数の順にソート
ansList[0] = 0 #一番目は0で固定


defaultCol = 0
for str in string_list:
    if str[2] % 2 == 1: #奇数だった場合、色を変える
        if defaultCol == 0:
            defaultCol = 1
        else:
            defaultCol = 0
    ansList[str[1] - 1] = defaultCol #決めた色で塗る

for i in range(N - 1):
    if ansList[i] == -1:
        ansList[i] = ansList[i - 1]

for ans in ansList:
    print(ans)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
result = [None]*n
for i in range(n - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    if w % 2 == 0:
        if result[u] == None and result[v] == None:
            result[u] = 0
            result[v] = 0
        elif result[u] == None:
            color = result[v]
            result[u] = color
        else:
            color = result[u]
            result[v] = color
    else:
        if result[u] == None and result[v] == None:
            result[u] = 0
            result[v] = 1
        elif result[u] == None:
            color = (result[v] + 1) % 2
            result[u] = color
        else:
            color = (result[u] + 1) % 2
            result[v] = color
for i in range(n):
    print(result[i])

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[list(map(int,input().split())) for i in range(n-1)]
a.sort()
ans=[0]*n
for i in range(n-1):
  if a[i][2]%2==0:
    if a[i][1]==1:
      ans[a[i][0]-1]=1
  else:
    if a[i][1]==1:
      ans[a[i][0]-1]=0
    else:
      ans[a[i][1]-1]=(ans[a[i][0]-1]+1)%2
for ii in range(n):
  print(ans[ii])

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 i, ans in enumerate(answer):
            if ans == tmp and i != rel[0]-1:
                answer[i] = (ans + 1) % 2 

for color in answer:
    print(color)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N = INT()
uvw = [LIST() for _ in range(N-1)]
uvw.sort(key=lambda x:(x[0], x[1]))
# print(uvw)
color = [0]*N  # -1で白 1で黒を表す
searched = [0]*N  # 探索した場所に1をいれる
color[0] = -1  # 0番目のノードを白で初期化
searched[0] = 1  # 0 番目のノードは探索済み

dic = {}
for u, v, w in uvw:
	if searched[v-1] == 0 and searched[u-1] == 0:  # どっちも見てないとき，反転かどうかだけ覚えておく
		if w % 2 == 0:
			dic[u-1] = (v-1, 1)
			dic[v-1] = (u-1, 1)
		else:
			dic[u-1] = (v-1, -1)
			dic[v-1] = (u-1, -1)
	else:
		if searched[v-1] == 1:
			u, v = v, u
		if w % 2 == 0:
			color[v-1] = color[u-1]
			searched[v-1] = 1
		else:
			color[v-1] = color[u-1] * (-1)
			searched[v-1] = 1
for i in range(N-1, -1, -1):
	if searched[i] == 0:
		color[i] = color[dic[i][0]] * dic[i][1]
		searched[i] = 1
# print(color)
for i in color:
	if i == -1:
		print('0')
	else:
		print('1')

Submission status: Wrong Answer

Solution code:


def main():
    N = int(input())
    a = [0 for _ in range(N+1)]
    s = []
    for i in range(N-1):
        s.append(list(map(int, input().split())))
    s = sorted(s)
    # print(s)
    for y in s:
        u, v, w = tuple(y)
        a[v] = w
    for A in a[1:]:
        if A % 2 == 0:
            print(0)
        else:
            print(1)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
tree = {}
for n in range(N-1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1

    if u not in tree:
        tree[u] = {}
    tree[u][v] = True if w % 2 else False

    if v not in tree:
        tree[v] = {}
    tree[v][u] = True if w % 2 else False

answer = {}
answer[0] = 0
choten = 0
queue = [0]
counter = 0
while len(answer) != N:
    for key, value in tree[choten].items():
        if key not in answer:
            answer[key] = answer[choten] if value else abs(answer[choten] - 1)
            queue.append(key)
    counter += 1
    choten = queue[counter]
for i in range(N):
    print(answer[i])

Submission status: Wrong Answer

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

# -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):
  if (memo[i] != 0):
    continue
  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=[]

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

m=[0]*N

uvw.sort()

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def is_even(x):
    if (x//2)*2 == x:
        return 1
    else:
        return 0

if __name__ == "__main__":
    N = int(input())
    E = [[]]*N
    for i in range(N-1):
        u,v,w = map(int,input().split())
        E[u-1].append([v-1,w])
        E[v-1].append([u-1,w])
    A = list(range(N))
    B = [0]*N
    C = list(range(1,N))
    while(C):
        now = A.pop(0)
        for a in E[now]:
            try:
                C.remove(a[0])
            except:
                continue
            if not is_even(a[1]):
                B[a[0]] = 1-B[now]
    for b in B:
        print(b)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict as df
from collections import deque
def bfs(k):
    s=deque()
    s.append(k)
    while(len(s)>0):
        #print(s)
        f=s.popleft()
        for j in d[f]:
            
            if vis[j]==0:
                if vis[f]=='R':
                    vis[j]='B'
                    s.append(j)
                else:
                    vis[j]='R'
                    s.append(j)
            elif vis[j]=='R' and vis[f]=='B':
                pass
            elif vis[j]=='B'and vis[f]=='R':
                pass
            
    return 1
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)
vis=[0]*(n+1)
vis[1]='R'
res=bfs(1)
if res==1:
    for j in range(1,n+1):
        if vis[j]=='R':
            print('0')
        else:
            print('1')

Submission status: Wrong Answer

Solution code:
n=int(input())
data=[[int(i) for i in input().split()] for i in range(n-1)]
first=data[0]
data=data[1:]
ans=[-1]*n
ans[first[0]-1] = 0
ans[first[1]-1] = 0 if first[2]%2==0 else 1
for uwd in data:
    if ans[uwd[0]-1]!=-1:
        if ans[uwd[0]-1]==0:
            ans[uwd[1]-1]=0 if uwd[2]%2==0 else 1
        else:
            ans[uwd[1]-1]=1 if uwd[2]%2==0 else 0

    else:
        if ans[uwd[0]-1]==0:
            ans[uwd[0]-1]=0 if uwd[1]%2==0 else 1
        else:
            ans[uwd[0]-1]=1 if uwd[1]%2==0 else 0
for i in range(len(ans)):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
n = int(input())
l = [list(map(int,input().split())) for i in range(n-1)]
b = l[0][0]
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)
a = Unionfind(n)
for i in l:
    if i[2] % 2 == 0 :
        a.union(i[0], i[1])
for i in range(1, n+1):
    if a.same_check(b,i) :
        print(1)
    else:
        print(0)