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:
from collections import Counter, deque

N = int(input())

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

udict ={}
uvdict = {}
lst = []
ans = [-1]*(N+1)
for u, v, w in uvw:
    if u in udict:udict[u].append(v)
    else:udict[u] =[v]
    if v in udict:udict[v].append(u)
    else:udict[v] =[u]
    lst.append(u)
    lst.append(v)
    uvdict[(u,v)] = w
cnt = Counter(lst)

cnt_max = cnt.most_common()[0][0]

nxt = deque()
nxt.append(cnt_max)
passed = [0]*(N+1)

d = [0]*(N+1)

while nxt!=deque([]):
    l1 = nxt.popleft()
    for l2 in udict[l1]:
        if passed[l2]==1:break
        d[l2] = d[l1] + uvdict[min(l1, l2), max(l1, l2)]
        nxt.append(l2)
    passed[l1]=1
for i in d[1:]:
    print(i%2)

Submission status: Wrong Answer

Solution code:
from collections import Counter, deque

N = int(input())

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

udict ={}
uvdict = {}
lst = []
ans = [-1]*(N+1)
for u, v, w in uvw:
    if u in udict:udict[u].append(v)
    else:udict[u] =[v]
    if v in udict:udict[v].append(u)
    else:udict[v] =[u]
    lst.append(u)
    lst.append(v)
    uvdict[(u,v)] = w
cnt = Counter(lst)

cnt_max = cnt.most_common()[0][0]

nxt = deque()
nxt.append(cnt_max)
passed = [0]*(N+1)
ans[cnt_max] = 1

while nxt!=deque([]):
    l1 = nxt.popleft()
    for l2 in udict[l1]:
        if passed[l2]==1:break
        if uvdict[min(l1, l2), max(l1, l2)]%2==0:ans[l2]=ans[l1]
        else:ans[l2]=(ans[l1]+1)%2
        nxt.append(l2)
    passed[l1]=1

print(*ans[1:N+1], sep='\n')

Submission status: Wrong Answer

Solution code:
N = int(input())

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

clrs = [-1]*(N+1)

for u, v, w in uvw:
    if clrs[u]!=-1 and clrs[v]==-1:
        if w%2==0:clrs[v]=clrs[u]
        else:clrs[v]=(clrs[u]+1)%2
    elif clrs[u]==-1 and clrs[v]!=-1:
        if w%2==0:clrs[u]=clrs[v]
        else:clrs[u]=(clrs[v]+1)%2
    else:
        if w%2==0:
            clrs[u]=1
            clrs[v]=1
        else:
          clrs[u]=0
          clrs[v]=1
print(*clrs[1:N+1], sep='\n')

Submission status: Wrong Answer

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

color=[0]*(n+1)
q = [(-1,0,0)]
while q:
    parent,x,l = q.pop()
    for y in tree[x]:
        if y[0] == parent:
            continue
        q.append((x,y[0],y[1]))
    if l%2==1:
        color[x+1]=1-color[parent+1]
#    print(parent,x,l)

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

Submission status: Wrong Answer

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

color=[0]*(n+1)
q = [(-1,0,0)]
while q:
    parent,x,l = q.pop()
    for y in tree[x]:
        if y[0] == parent:
            continue
        q.append((x,y[0],y[1]))
    if l%2==1:
        color[x+1]=1

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

Submission status: Wrong Answer

Solution code:
from collections import deque
N=int(input())
ten=[0]*N
G=[[] for i in range(N)]
for i in range(N-1):
    u,v,w=map(int,input().split())
    G[u-1].append([v-1,w])
    G[v-1].append([u-1,w])
print(G)
que=deque()
que.append(0)
visited=[[0] for i in range(N)]
visited[0]=1
while que:
    v=que.pop()
    for u in G[v]:
        #print(u)
        if visited[u[0]]==1:
            continue
        if u[1]%2==0:
            ten[u[0]]=ten[v]
        else:
            if ten[v]==0:
                ten[u[0]]=1
                #print("a",ten[u[0]])
            else:
                ten[u[0]]=0
        #print(ten)
        visited[u[0]]=1
        que.appendleft(u[0])
for i in range(N):
    print(ten[i])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
#https://atcoder.jp/contests/abc126/tasks/abc126_d
import sys
#import collections
from operator import itemgetter, attrgetter

n= int(input())

x=[]
for i in range(0,n+1):
	x.append(-1)

uvw=[]

for i in range(0,n-1):
	tmp = input().split()
	a,b,c = list(map(lambda a: int(a), tmp))
	if(a>b):
		hoge=a
		a=b
		b=hoge
	uvw.append([a,b,c])
tmp1=sorted(uvw, key=itemgetter(1))
tmp2=sorted(tmp1, key=itemgetter(0))
uvw=tmp2

x[uvw[0][0]]=0

for j in range(1,n-1):
	for i in range(0,n-1):
		print("---")
		print(x)
		
		if x[uvw[i][0]]!=-1:
			x[uvw[i][1]]=(x[uvw[i][0]]+uvw[i][2])%2
		elif  x[uvw[i][1]]!=-1:
			x[uvw[i][0]]=(x[uvw[i][1]]+uvw[i][2])%2

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

print(uvw)
#print(x)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
#https://atcoder.jp/contests/abc126/tasks/abc126_d
import sys
#import collections
from operator import itemgetter, attrgetter

n= int(input())

x=[]
for i in range(0,n+1):
	x.append(-1)

uvw=[]

for i in range(0,n-1):
	tmp = input().split()
	a,b,c = list(map(lambda a: int(a), tmp))
	if(a>b):
		hoge=a
		a=b
		b=hoge
	uvw.append([a,b,c])
tmp1=sorted(uvw, key=itemgetter(1))
tmp2=sorted(tmp1, key=itemgetter(0))
uvw=tmp2

x[uvw[0][0]]=0

for j in range(0,n-1):
	for i in range(0,n-1):
		if x[uvw[i][0]]!=-1:
			x[uvw[i][1]]=(uvw[i][0]+uvw[i][2])%2
		elif  x[uvw[i][1]]!=-1:
			x[uvw[i][0]]=(uvw[i][1]+uvw[i][2])%2

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

#print(uvw)
#print(x)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
#https://atcoder.jp/contests/abc126/tasks/abc126_d
import sys
#import collections
from operator import itemgetter, attrgetter

n= int(input())

x=[]
for i in range(0,n+1):
	x.append(-1)

uvw=[]

for i in range(0,n-1):
	tmp = input().split()
	a,b,c = list(map(lambda a: int(a), tmp))
	if(a>b):
		hoge=a
		a=b
		b=hoge
	uvw.append([a,b,c])
tmp1=sorted(uvw, key=itemgetter(1))
tmp2=sorted(tmp1, key=itemgetter(0))
uvw=tmp2

x[uvw[0][0]]=0

for i in range(0,n-1):
	if x[uvw[i][0]]!=-1:
		x[uvw[i][1]]=(uvw[i][0]+uvw[i][2])%2
	else:
		x[uvw[i][0]]=(uvw[i][1]+uvw[i][2])%2

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

#print(uvw)
#print(x)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
N = int(input())
d = defaultdict(list)
for i in range(N-1):
    u,v,w = map(int,input().split())
    d[u-1].append((v-1,w))
    d[v-1].append((u-1,w))

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

q = deque()
q.append((0,0))
visited = set()
while len(q) > 0:
    q2 = deque()
    while len(q) > 0:
        node1,cost1 = q.popleft()
        dist[node1] = cost1
        visited.add(node1)
        
        for node2,cost2 in d[node1]:
            if node2 in visited:
                continue
            q2.append((node2, cost1+cost2))
    q = q2
print(dist)
for i in dist:
    print(i%2)

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
#MOD=10**9+7
sys.setrecursionlimit(2147483647)
# import math
#import numpy as np
# import operator
#import bisect
# from heapq import heapify,heappop,heappush
#from math import gcd
# from fractions import gcd
#from collections import deque
#from collections import defaultdict
# from collections import Counter
#from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())


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]

def main():
	N=int(input())
	uf=WeightedUnionFind(N)
	for i in range(N-1):
		u,v,w=map(int,input().split())
		uf.union(u,v,w%2)
	
	for i in range(1,N+1):
		print(uf.diff(1,i)%2)
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
#MOD=10**9+7
sys.setrecursionlimit(2147483647)
# import math
#import numpy as np
# import operator
#import bisect
# from heapq import heapify,heappop,heappush
#from math import gcd
# from fractions import gcd
#from collections import deque
#from collections import defaultdict
# from collections import Counter
#from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())


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]

def main():
	N=int(input())
	uf=WeightedUnionFind(N)
	for i in range(N-1):
		u,v,w=map(int,input().split())
		uf.union(u,v,w%2)
	
	for i in range(1,N+1):
		if uf.diff(1,i)%2==1:
			print("1")
		else:
			print("0")
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
#MOD=10**9+7
sys.setrecursionlimit(2147483647)
# import math
#import numpy as np
# import operator
#import bisect
# from heapq import heapify,heappop,heappush
#from math import gcd
# from fractions import gcd
#from collections import deque
#from collections import defaultdict
# from collections import Counter
#from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())


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]

def main():
	n=int(input())
	uf=WeightedUnionFind(n)
	for i in range(n-1):
		u,v,w=map(int,input().split())
		uf.union(v,u,w%2)
	
	for i in range(1,n+1):
		if uf.diff(1,i)%2==1:
			print("1")
		else:
			print("0")
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
#MOD=10**9+7
# sys.setrecursionlimit(2147483647)
# import math
#import numpy as np
# import operator
#import bisect
# from heapq import heapify,heappop,heappush
#from math import gcd
# from fractions import gcd
#from collections import deque
#from collections import defaultdict
# from collections import Counter
#from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())


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]

def main():
	n=int(input())
	uf=WeightedUnionFind(n)
	for i in range(n-1):
		u,v,w=map(int,input().split())
		uf.union(v,u,w%2)
	
	for i in range(1,n+1):
		if uf.diff(1,i)%2==1:
			print("1")
		else:
			print("0")
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
#MOD=10**9+7
# sys.setrecursionlimit(2147483647)
# import math
#import numpy as np
# import operator
#import bisect
# from heapq import heapify,heappop,heappush
#from math import gcd
# from fractions import gcd
#from collections import deque
#from collections import defaultdict
# from collections import Counter
#from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())


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]

def main():
	n=int(input())
	uf=WeightedUnionFind(n)
	for i in range(n-1):
		u,v,w=map(int,input().split())
		uf.union(v,u,w)
	
	for i in range(1,n+1):
		if uf.diff(1,i)%2==1:
			print("1")
		else:
			print("0")
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
#MOD=10**9+7
# sys.setrecursionlimit(2147483647)
# import math
#import numpy as np
# import operator
#import bisect
# from heapq import heapify,heappop,heappush
#from math import gcd
# from fractions import gcd
#from collections import deque
#from collections import defaultdict
# from collections import Counter
#from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())


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]

def main():
	n=int(input())
	uf=WeightedUnionFind(n)
	for i in range(n-1):
		u,v,w=map(int,input().split())
		uf.union(v,u,w)
	print(uf.weight)
	
	for i in range(1,n+1):
		if uf.diff(1,i)%2==1:
			print("1")
		else:
			print("0")
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
N = int(input())
for i in range(N):
  print(0)

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
#MOD=10**9+7
# sys.setrecursionlimit(2147483647)
# import math
#import numpy as np
# import operator
#import bisect
# from heapq import heapify,heappop,heappush
#from math import gcd
# from fractions import gcd
#from collections import deque
#from collections import defaultdict
# from collections import Counter
#from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())


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]

def main():
	n=int(input())
	uf=WeightedUnionFind(n)
	for i in range(n-1):
		u,v,w=map(int,input().split())
		uf.union(u,v,w)
	
	for i in range(1,n+1):
		if uf.diff(1,i)%2==1:
			print("1")
		else:
			print("0")
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
#MOD=10**9+7
# sys.setrecursionlimit(2147483647)
# import math
#import numpy as np
# import operator
#import bisect
# from heapq import heapify,heappop,heappush
#from math import gcd
# from fractions import gcd
#from collections import deque
#from collections import defaultdict
# from collections import Counter
#from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())


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]

def main():
	n=int(input())
	uf=WeightedUnionFind(n)
	for i in range(n-1):
		u,v,w=map(int,input().split())
		w%=2
		uf.union(u,v,w)
	
	for i in range(1,n+1):
		if uf.diff(1,i)%2:
			print(1)
		else:
			print(0)
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
#MOD=10**9+7
# sys.setrecursionlimit(2147483647)
# import math
#import numpy as np
# import operator
#import bisect
# from heapq import heapify,heappop,heappush
#from math import gcd
# from fractions import gcd
#from collections import deque
#from collections import defaultdict
# from collections import Counter
#from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())


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]

def main():
	n=int(input())
	uf=WeightedUnionFind(n)
	for i in range(n-1):
		u,v,w=map(int,input().split())
		uf.union(u,v,w)
	
	for i in range(1,n+1):
		if abs(uf.diff(1,i)%2):
			print(1)
		else:
			print(0)
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
#MOD=10**9+7
# sys.setrecursionlimit(2147483647)
# import math
#import numpy as np
# import operator
#import bisect
# from heapq import heapify,heappop,heappush
#from math import gcd
# from fractions import gcd
#from collections import deque
#from collections import defaultdict
# from collections import Counter
#from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())


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]

def main():
	n=int(input())
	uf=WeightedUnionFind(n)
	for i in range(n-1):
		u,v,w=map(int,input().split())
		uf.union(u,v,w)
	
	for i in range(1,n+1):
		if uf.diff(1,i)%2:
			print(0)
		else:
			print(1)
	
if __name__ == "__main__":
		main()

Submission status: Wrong Answer

Solution code:
n = int(input())
vers = [[]] * n
is_ver_checked = [False] * n
ans = [0] * n

for i in range(n):
  vers[i] = []

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

# print(n)
# print(vers)

def dfs(ver_i, current_w):
  if is_ver_checked[ver_i]:
    return
  # 現在のノードまでの距離が奇数の場合、辺の色に 1 を設定
  # print('visit ver_i: {}'.format(ver_i))
  if current_w % 2 == 1:
    ans[ver_i] = 1
  
  for next_ver_i, w in vers[ver_i]:
    dfs(next_ver_i, current_w+w)
  is_ver_checked[ver_i] = True

for i in range(n):
  dfs(i, 0)
for i in range(n):
  print(ans[i])

Submission status: Wrong Answer

Solution code:
n = int(input())
vers = [[]] * n
is_ver_checked = [False] * n
ans = [0] * n

for i in range(n):
  vers[i] = []

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

# print(n)
# print(vers)

def dfs(ver_i, current_w):
  if is_ver_checked[ver_i]:
    return
  # 現在のノードまでの距離が奇数の場合、辺の色に 1 を設定
  if current_w % 2 == 1:
    ans[ver_i] = 1
  
  for next_ver_i, w in vers[ver_i]:
    dfs(next_ver_i, current_w+w)
  is_ver_checked[ver_i] = True

dfs(0, 0)
for i in range(n):
  print(ans[i])