Problem:


Score : 400 points

Problem Statement

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

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

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

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq u_i < v_i \leq N

1 \leq w_i \leq 10^9

Input

Input is given from Standard Input in the following format:

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

Output

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

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

Sample Input 1

3
1 2 2
2 3 1

Sample Output 1

0
0
1

Sample Input 2

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

Sample Output 2

1
0
1
0
1



Sample input:
3
1 2 2
2 3 1


Sample output:
0
0
1

Submission status: Wrong Answer

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

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

    # 併合
    def 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(1,i)%2==0:
        ans.append(0)
    else:
        ans.append(1)
for x in ans:
    print(x)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

import queue

n = int(input())

class tree:
    def __init__(self, node_number):
        self.nodes = [{} for _ in range(node_number)]

    def add_connects(self, node_num, connect_node, path_len):
        self.nodes[node_num-1][connect_node-1] = path_len

t = tree(n)
checked_oddlen = False
start_node = 0
for _ in range(n-1):
    u, v, w = map(int, input().split())
    t.add_connects(u, v, w)
    t.add_connects(v, u, w)
    if not(checked_oddlen) and w%2 == 0:
        start_node = u


# 木構造を全探索。偶数長のパスを持つノードから長さが偶数長であるノードナンバーを保存    
def dfs(node_number, total_path_len, odds, prev_node):
    connect_nodes = list(t.nodes[node_number].keys())
    for node_id in connect_nodes:
        if (total_path_len + t.nodes[node_number][node_id]) % 2 == 0:
            odds.append(node_id)
        if node_id == prev_node:
            dfs(node_id, total_path_len+t.nodes[node_number][node_id], odds, node_number)

odds = [start_node]
dfs(start_node, 0, odds, start_node)

for i in range(n):
    if i in odds:
        print("1")
    else:
        print("0")

Submission status: Wrong Answer

Solution code:
N = int(input())
U, V, W = [], [], []
T = [[] for _ in range(N)]
S = set()
for _ in range(N-1):
    u, v, w = map(int, input().split())
    if w % 2 == 1:
        T[u-1].append(v)
        T[v-1].append(u)
        S.add(u)
        S.add(v)
    # U.append(u)
    # V.append(v)
    # W.append(w)

ans = [None for _ in range(N)]

while len(S) > 0:
    s = S.pop()
    search = set()
    search.add(s)
    ans[s-1] = '0'
    while len(search) > 0:
        p = search.pop()
        if p in S:
            S.remove(p)
        for t in T[p-1]:
            if t in S:
                search.add(t)
                ans[t-1] = '0' if ans[p-1] == '1' else '1'

for i in range(N):
    if ans[i] is None:
        ans[i] = '0'
    

print(' '.join(ans))

Submission status: Wrong Answer

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

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

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

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

for c in bw:
    print(c)

Submission status: Wrong Answer

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

u, v, w = [], [], []
ans = [0] * N

for i in range(N-1):
    _u, _v, _w = map(int, input().split())
    u.append(_u)
    v.append(_v)
    w.append(_w)
# print(u)
# print(v)
# print(w)

flg = w[0] % 2
ans[v[0]-1] ^= flg
q = deque([(u[0], v[0], w[0], ans[v[0]-1])])
# print(q)

s = set()
s.add((u[0], v[0], w[0]))
# print(s)

while q:
    u_, v_, w_, ans_ = q.popleft()
    for i in range(1, N-1):
        if u[i] == v_ and ((u[i], v[i], w[i]) not in s):
            flg = w[i] % 2
            s.add((u[i], v[i], w[i]))
            q.append((u[i], v[i], w[i], ans_ ^ flg))
            ans[v[i]-1] = ans_ ^ flg
        if v[i] == v_ and ((u[i], v[i], w[i]) not in s):
            flg = w[i] % 2
            s.add((u[i], v[i], w[i]))
            q.append((u[i], v[i], w[i], ans_ ^ flg))
            ans[u[i]-1] = ans_ ^ flg

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

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
ans = np.array([-1]*N).astype(int)
for n in range(N-1):
    u,v,w=map(int, input().split())
    u -= 1
    v -= 1
    if ans[u] < 0 and ans[v] < 0:
        ans[u] = 0
        if w%2 == 0:
            ans[v] = 0
        else:
            ans[v] = 1
    elif ans[u] < 0:
        if w%2 == 0:
            ans[u] = ans[v]
        else:
            if ans[v] == 0:
                ans[u] = 1
            else:
                ans[u] = 0
    elif ans[v] < 0:
        if w%2 == 0:
            ans[v] = ans[u]
        else:
            if ans[u] == 0:
                ans[v] = 1
            else:
                ans[v] = 0
    elif ans[u] == ans[v]:
        if w%2 == 0:
            pass
        else:
            if ans[u] == 0:
                t = 1
            else:
                t = 0
            ans[np.where(ans==ans[v])] = t
    elif ans[u] != ans[v]:
        if w%2 != 0:
            pass
        else:
            ans[np.where(ans==ans[u])] = ans[v]
            
for p in ans:
    print(p)

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:
      ans[a[i][1]-1]=ans[a[i][0]-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:
# coding: UTF-8
import sys
sys.setrecursionlimit(10**5)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

readl= lambda: list(map(int, sys.stdin.readline().split()))
readt= lambda: tuple(map(int, sys.stdin.readline().split()))
read = lambda: sys.stdin.readline().rstrip()
readi = lambda: int(read())
readmi = lambda: map(int, sys.stdin.readline().split())
readms = lambda: map(str, sys.stdin.readline().split())

n = readi()
tree = defaultdict(lambda: defaultdict(int))
ans = [0] * (n + 1)
for i in range(n - 1):
    a, b, c= readmi()
    tree[a][b] = c
    tree[b][a] = c


def dfs(x, flg):
    if x >= n:
        return
    v = tree[x]
    for i in v:
        if i <= x:
            continue
        else:
            if i % 2 == 0:
                if flg == True:
                    ans[i] = 0
                    dfs(i, True)
                else:
                    ans[i] = 1
                    dfs(i, False)
            else:
                if flg == True:
                    ans[i] = 1
                    dfs(i, False)
                else:
                    ans[i] = 0
                    dfs(i, True)
dfs(1, True)
for i in range(1, n + 1):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict


def main():
    num = int(input())


    data_dic = defaultdict(set)

    for i in range(num - 1):
        u, v, w = map(int, input().split())
        if w % 2 == 1:
            max_data, min_data = max(u, v), min(u, v)
            data_dic[min_data].add(max_data)


    color_dic = [-1 for i in range(num + 1)]
    change_dic = {0:1, 1:0}

    for i in range(1, num + 1):
        my_color = 1
        aite_color = 0
        if color_dic[i] != -1:
            my_color = color_dic[i]
            aite_color = change_dic[my_color]
        else:
            for ele in data_dic[i]:
                if color_dic[ele] != -1:
                    aite_color = color_dic[ele]
                    my_color = change_dic[aite_color]
                    break
        color_dic[i] = my_color
        for ele in data_dic[i]:
            color_dic[ele] = aite_color


    for i in range(1, num + 1):
        print(color_dic[i])






if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())

s1 = set()
s2 = set()

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

    if u in s1:
        if w % 2 == 0:
            s1.add(v)
        else:
            s2.add(v)
    elif v in s1:
        if w % 2 == 0:
            s1.add(u)
        else:
            s2.add(u)
    elif u in s2:
        if w % 2 == 0:
            s2.add(v)
        else:
            s1.add(v)
    elif v in s2:
        if w % 2 == 0:
            s2.add(u)
        else:
            s1.add(u)
    else:
        if w%2 == 0:
            s1.add(u)
            s1.add(v)
        else:
            s1.add(u)
            s2.add(v)


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

Submission status: Wrong Answer

Solution code:
N = int(input())
wset = set()
bset = set()
exo = []
exe = []

for i in range(N-1):
    u,v,w = map(int,input().split())
    if w%2 != 0:
        if u in wset:   bset.add(v)
        elif u in bset: wset.add(v)
        elif v in wset: bset.add(u)
        elif v in bset: wset.add(u)
        else:   exo += [[u,v]]
    else:
        if u in wset:   wset.add(v)
        elif u in bset: bset.add(v)
        elif v in wset: wset.add(u)
        elif v in bset: bset.add(u)
        else:   exe += [[u,v]]

for i in exo:
    if i[0] in wset:   bset.add(i[1])
    elif i[0] in bset: wset.add(i[1])
    elif i[1] in wset: bset.add(i[0])
    elif i[1] in bset: wset.add(i[0])
    else:
        wset.add(i[0])
        bset.add(i[1])
for i in exe:
    if i[0] in wset:   wset.add(i[1])
    elif i[0] in bset: bset.add(i[1])
    elif i[1] in wset: wset.add(i[0])
    elif i[1] in bset: bset.add(i[0])
    else:
        wset.add(i[0])
        wset.add(i[1])

for i in range(1,N+1):
    if i in wset:   print(0)
    elif i in bset: print(1)
    else:   print(0)

Submission status: Wrong Answer

Solution code:
N = int(input())
dic = {}
for i in range(N):
    dic[i+1] = 4
for i in range(N-1):
    u, v, w = map(int, input().split())
    if w%2 == 0:
        if dic[u] == 4:
            dic[u] = 1
        if dic[v] == 4:
            dic[v] = 1
    else:
        if dic[u] == 4 and dic[v] == 4:
            dic[u] = 1
            dic[v] = 0
        elif dic[u] == 1:
            dic[v] = 0
              
c = dic.values()
list(c)
d = ""
for j in c:
    if j == 3:
        j =1
    d += str(j) + "\n"
print(d)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = []
for i in range(N-1):
    u,v,w = map(int, input().split())
    a.append([u,v,w%2])
a.sort(key=lambda x:x[0])
par = [0]*(N+1)
tmp = 0
par[1] = 0
for u,v,w in a:
    if w:
        par[v] = tmp^1
        tmp = tmp^1
for i in range(1,N+1):
    print(par[i])

Submission status: Wrong Answer

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

branch = [0]*n

dist.sort()

for i in range(n-1):
    if dist[i][2] % 2 == 1:
        if branch[dist[i][0]-1] == 0:
            branch[dist[i][1]-1] == 1
        if branch[dist[i][0]-1] == 1:
            branch[dist[i][1]-1] == 0

for j in range(n-1):
    print(branch[j])

Submission status: Wrong Answer

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

    def find(self, x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self.find(self.table[x])
        return self.table[x]

    def union(self, x, y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 != s2:
            if self.table[s1] > self.table[s2]:
                self.table[s2] = s1
                self.table[s1] -= 1
                self.size[s1] += self.size[s2]
                self.size[s2] = 0
            else:
                self.table[s1] = s2
                self.table[s2] -= 1
                self.size[s2] += self.size[s1]
                self.size[s1] = 0
        return

n = int(input())
u = [0] * (n-1)
v = [0] * (n-1)
w = [0] * (n-1)
uf = UnionFind(n)

for i in range(n-1):
    u[i], v[i], w[i] = map(int, input().split())
    u[i] -= 1
    v[i] -= 1
    w[i] = w[i] % 2
    # 同じなら0、違ったら1
    if w[i] == 0:
        uf.union(u[i], v[i])

group1 = []
group2 = []

for i in range(n-1):
    if w[i] == 1:
        if uf.find(u[i]) in group1:
            group2.append(v[i])
        elif uf.find(u[i]) in group2:
            group1.append(v[i])
        elif uf.find(v[i]) in group1:
            group2.append(u[i])
        elif uf.find(v[i]) in group2:
            group1.append(u[i])
        else:
            group1.append(v[i])
            group2.append(u[i])

for i in range(n):
    if uf.find(i) in group1:
        print(0)
    else:
        print(1)

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 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:
N = int(input())
Li = [list(map(int, input().split())) for _ in range(N-1)]
Li = sorted(Li, key=lambda x: x[0])
ans = [0 for _ in range(N+1)]

for i in Li:
    u = i[0]
    v = i[1]
    if i[2] % 2 == 1:
        if ans[u] == 0 and ans[v] == 0:
            ans[u] = 1
            ans[v] = -1
        elif ans[u] == 0 and ans[v] != 0:
            ans[u] = -ans[v]
        elif ans[u] != 0 and ans[v] == 0:
            ans[v] = -ans[u]
    else:
        if ans[u] == 0 and ans[v] == 0:
            ans[u] = 1
            ans[v] = 1
        elif ans[u] == 0 and ans[v] != 0:
            ans[u] = ans[v]
        elif ans[u] != 0 and ans[v] == 0:
            ans[v] = ans[u]
            
for i in ans:
    if i == -1:
        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
 
    elif ans[uwd[1]-1]!=-1:
        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
    else:
        ans[uwd[0]-1] = 0
        ans[uwd[1]-1] = 0
for i in range(len(ans)):
    print(ans[i])

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:
# -*- 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):
    ans = uf.table[i][1] if uf.table[i][1] < 0 else 0
    print(ans)

Submission status: Wrong Answer

Solution code:
n=input()
br=[]
wr=[]
n=int(n)

i=1
while i!=n:
    i=i+1
    u,v,w=input().split(" ")

    u=int(u)
    v=int(v)
    w=int(w)

    if w%2==0:
        if u in wr==False or v in wr==False:
            br.append(u)
            br.append(v)
        else:
            wr.append(u)
            wr.append(v)
    else:
        if u in wr == False:
            br.append(u)
            wr.append(v)
        else:
            wr.append(u)
            br.append(v)


i=0

try:
    while i!=n:
        i = i + 1
        try:
            br.index(i)

            print(1)
        except:
            print(0)




except:
    pass

Submission status: Wrong Answer

Solution code:
###
### atcorder test program
###

import sys
import math

### math class
# class math:
#     ### pi
#     pi = 3.14159265358979323846264338

#     ### GCD
#     def gcd(self, a, b):
#         if b == 0:
#             return a
#         return self.gcd(b, a%b)

#     ### LCM
#     def lcm(self, a, b):
#         return (a*b)//self.gcd(a,b)

# math = math()

### output class
class output:
    ### list
    def list(self, l):
        l = list(l)
        print(" ", end="")
        for i, num in enumerate(l):
            print(num, end="")
            if i != len(l)-1:
                print(" ", end="")
        print()

output = output()

### input sample
#i = input()
#A, B, C = [x for x in input().split()]
#N, K = [int(x) for x in input().split()]
#inlist = [int(w) for w in input().split()]
#R = float(input())
#A = [int(x) for x in input().split()]
#for line in sys.stdin.readlines():
#    x, y = [int(temp) for temp in line.split()]

### output sample
#print("{0} {1} {2:.5f}".format(A//B, A%B, A/B))
#print("{0:.6f} {1:.6f}".format(R*R*math.pi,R*2*math.pi))
#print(" {}".format(i), end="")

N = int(input())
data = []
nodenum = {str(i): 0 for i in range(N)}
ans  = [0 for i in range(N)]

for i in range(N-1):
    data.append([int(x) for x in input().split()])

data.sort()

for link in data:
    nodenum[str(link[0]-1)] += 1
    nodenum[str(link[1]-1)] += 1

nodenum = sorted(nodenum.items(), key=lambda x: -x[1])

for i in nodenum:
    temp = nodenum.pop()
    temp = list(temp)
    temp = int(temp[0])-1
    if data[temp][2]%2 == 1:
        ans[data[temp][1]-1] = 1 if ans[data[temp][0]-1] == 0 else 0

for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())
wset = set()
bset = set()
exo = []
exe = []

for i in range(N-1):
    u,v,w = map(int,input().split())
    if w%2 != 0:
        if u in wset:   bset.add(v)
        elif u in bset: wset.add(v)
        elif v in wset: bset.add(u)
        elif v in bset: wset.add(u)
        else:   exo += [[u,v,w]]
    else:
        if u in wset:   wset.add(v)
        elif u in bset: bset.add(v)
        elif v in wset: wset.add(u)
        elif v in bset: bset.add(u)
        else:   exe += [[u,v,w]]

for i in exo:
    if u in wset:   bset.add(v)
    elif u in bset: wset.add(v)
    elif v in wset: bset.add(u)
    elif v in bset: wset.add(u)
    else:
        wset.add(u)
        bset.add(v)
for i in exe:
    if u in wset:   wset.add(v)
    elif u in bset: bset.add(v)
    elif v in wset: wset.add(u)
    elif v in bset: bset.add(u)
    else:
        wset.add(u)
        wset.add(v)

for i in range(1,N+1):
    if i in wset:   print(0)
    elif i in bset: print(1)
    else:   print(0)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

for e in edge:
    if e[0] > e[1]:
        e[0], e[1] = e[1], e[0]

for n in range(1, N+1):
    for e in edge:
        if e[0] == n:
            if e[2]%2 == 1:
                node[e[1]-1] = node[e[0]-1] ^1

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

Submission status: Wrong Answer

Solution code:
N = int(input())
tree = [list(map(int, input().split())) for _ in range(N-1)]
answer = [-1] * N
for i in range(N - 1):
    if(tree[i][2] % 2 == 0):
        if(answer[0] == -1 or answer[1] == -1):
            answer[0] = 0
            answer[1] = 0
        elif(answer[0] == 0):
            answer[1] = 0
        elif (answer[1] == 0):
            answer[0] = 0
        elif (answer[0] == 1):
            answer[1] = 1
        elif (answer[1] == 1):
            answer[0] = 1
    else:
        if (answer[0] == -1 or answer[1] == -1):
            answer[0] = 0
            answer[1] = 1
        elif (answer[0] == 0):
            answer[1] = 1
        elif (answer[1] == 0):
            answer[0] = 1
        elif (answer[0] == 1):
            answer[1] = 0
        elif (answer[1] == 1):
            answer[0] = 0
for i in range(N):
    print(answer[i])

Submission status: Wrong Answer

Solution code:
def solve():
    N = int(raw_input())
    # u = []
    # v = []
    # w = []
    ret = [0] * N
    changed = [False] * N
    for i in range(N - 1):
        ui,vi,wi = [int(_) for _ in raw_input().split()]
        # u.append(ui)
        # v.append(vi)
        # w.append(wi)
        # if wi % 2 == 0:
        #     ret[vi - 1] = ret[ui - 1]
        # else:
        #     ret[vi - 1] = (1 - ret[ui - 1])
        if (i == 0):
            changed[ui - 1] = True
        if not changed[vi - 1]:
            if wi % 2 == 0:
                ret[vi - 1] = ret[ui - 1]
            else:
                ret[vi - 1] = (1 - ret[ui - 1])
            changed[vi - 1] = True
        else:
            if wi % 2 == 0:
                ret[ui - 1] = ret[vi - 1]
            else:
                ret[ui - 1] = (1 - ret[vi - 1])
            changed[ui - 1] = True
    for i in range(N):
        print ret[i]

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = [None] * N
A = [[int(i) for i in input().split()] for j in range(N-1)]
A= sorted(A, key=lambda a: (a[-1] % 2))
for u,v,w in A:
    if w % 2 == 0:
        if ans[u-1] is None and ans[v-1] is None:
            ans[u-1] = ans[v-1] = 1
        elif ans[u-1] is not None:
            ans[v-1] = ans[u-1]
        elif ans[v-1] is not None:
            ans[u-1] = ans[v-1]
    else:
        if ans[u-1] is None and ans[v-1] is None:
            ans[u-1] = 0
            ans[v-1] = 1
        elif ans[u-1] is not None:
            ans[v-1] = (ans[u-1] + 1) % 2
        elif ans[v-1] is not None:
            ans[u-1] = (ans[v-1] + 1) % 2
        else:
            if ans[v-1] == ans[u-1]:
                ans[u-1] = (ans[v-1] + 1) % 2
for a in ans:
    print(a)

Submission status: Wrong Answer

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

    def find(self, x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self.find(self.table[x])
        return self.table[x]

    def union(self, x, y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 != s2:
            if self.table[s1] > self.table[s2]:
                self.table[s2] = s1
                self.table[s1] -= 1
                self.size[s1] += self.size[s2]
                self.size[s2] = 0
            else:
                self.table[s1] = s2
                self.table[s2] -= 1
                self.size[s2] += self.size[s1]
                self.size[s1] = 0
        return

n = int(input())

u = [0] * (n-1)
v = [0] * (n-1)
w = [0] * (n-1)
uf = UnionFind(n)

for i in range(n-1):
    u[i], v[i], w[i] = map(int, input().split())
    u[i] -= 1
    v[i] -= 1
    w[i] = w[i] % 2
    # 同じなら0、違ったら1
    if w[i] == 0:
        uf.union(u[i], v[i])

group1 = []
group2 = []

for i in range(n-1):
    if w[i] == 1:
        if uf.find(u[i]) in group1:
            group2.append(v[i])
        elif uf.find(u[i]) in group2:
            group1.append(v[i])
        elif uf.find(v[i]) in group1:
            group2.append(u[i])
        elif uf.find(v[i]) in group2:
            group1.append(u[i])
        else:
            group1.append(v[i])
            group2.append(u[i])

for i in range(n):
    if uf.find(i) in group1:
        print(0)
    else:
        print(1)