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:
# -*- coding:utf-8 -*-

def solve():
    N = int(input())
    U, V, W = [], [], []
    group = []
    colors = [None]*N
    for i in range(N-1):
        u, v, w = list(map(int, input().split()))
        U.append(u-1)
        V.append(v-1)
        W.append(w)
    
    for i in range(N-1):
        u, v = U[i], V[i]
        if W[i]%2 == 0:
            if colors[u] is None:
                colors[u] = 0
            if colors[v] is None:
                colors[v] = 0
        else:
            # 奇数の場合
            if colors[u] == 0: colors[v] = 1
            elif colors[u] == 1: colors[v] = 0
            elif colors[u] is None:
                if colors[v] == 0:
                    colors[u] = 1
                elif colors[v] == 1:
                    colors[u] = 0
                elif colors[v] is None:
                    # 両方ともNoneの場合
                    colors[u] = 1
                    colors[v] = 0

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

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

n = int(input())
point = [[0, 0, 0], [1, 0, 0]]

for i in range(2, n+1):
    point.append([i, 0, 3])

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

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

for i in range(1, n+1):
    #print(i, point[i])
    print(point[i][2])

Submission status: Wrong Answer

Solution code:

# union-find木
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())

uf = UnionFind(N);

tree1 = -1;
tree2 = -1;

for i in range(1, N):
    u, v, w = map(int, input().split())
    w = w%2
    if(w == 0):
        uf.union(u, v)
    else:
        if(tree1 == -1):
            tree1 = uf.find(u)
            tree2 = uf.find(v)
        else:
            if(uf.find(u) == tree1 or uf.find(v) == tree2):
                uf.union(tree1, u)
                uf.union(tree2, v)
            else:
                uf.union(tree1, v)
                uf.union(tree2, u)

answer = []

for i in range(1, N+1):
    if(tree1 == uf.find(i)):
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N = int(input())
d = {}
for i in range(N - 1):
    u, v, w = map(int, input().split())
    d[(u, v)] = w

white = {1}
black = set()
for u, v in sorted(d.keys()):
    if d[(u, v)] % 2 == 0:
        if u in white:
            white.add(v)
        elif u in black:
            black.add(v)
        elif v in white:
            white.add(u)
        else:
            black.add(u)
    else:
        if u in white:
            black.add(v)
        elif u in black:
            white.add(v)
        elif v in white:
            black.add(u)
        else:
            white.add(u)
for i in range(1, N + 1):
    print('0' if i in white else '1')

Submission status: Wrong Answer

Solution code:
N = int(input())

color = [False] * N

for _ in range(N - 1):
    u, v, w = map(int, input().split())
    if w % 2 != 0:
        color[u - 1] = True
        color[v - 1] = True

for i in range(N):
    print(0 if color[i] else 1)

Submission status: Wrong Answer

Solution code:
n = int(input().strip())

distance = {}

colors = [0]*(n+1)

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

for parent in range(1, n+1):
	for option in distance:
		if option[0] == parent:
			child = option[1]
			if distance[option] % 2 == 0:
				colors[child] = colors[parent]
			else:
				colors[child] = 1-colors[parent]

print('\n'.join([str(i) for i in colors[1:]]))

Submission status: Wrong Answer

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


def main():
	N = int(input())
	a1 = 0
	a2 = 0
	for _ in range(N-1):
		n, v, w = map(int, input().split())
		if w%2 == 0:
			a1 = n
			a2 = v
	
	for i in range(N):
		if i == v or i == n:
			print(1)
		else:
			print(0)





if __name__ == '__main__':
	import sys
	sys.exit(main())

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
n=int(input())
s=[[]for i in range(n+1)]
g=[0]*(n+1)
h=[True]*(n+1)
for i in range(n-1):
    a,b,c=map(int,input().split())
    s[a].append([b,c])
    s[b].append([a,c])
def dfs(x):
    h[x]=False
    for i in s[x]:
        if h[i[0]]:
            g[i[0]]=g[x]+i[1]
            dfs(i[0])
dfs(1)
print(g)
for i in range(1,n+1):
    if g[i]%2==0:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
class UnionFind:
    def __init__(self,N):
        self.par=[i for i in range(N+1)]
        self.rank=[0 for _ in range(N+1)]
    #木の根を求める
    def find(self,x):
        if self.par[x]==x:
            return x
        else:
            self.par[x]=self.find(self.par[x])
            return self.par[x]
    
    def same_check(self,x,y):
        return self.find(x)==self.find(y)
    #併合
    def union(self,x,y):
        x=self.find(x)
        y=self.find(y)
        if self.rank[x]<self.rank[y]:
            self.par[x]=y
        else:
            self.par[y]=x
            if self.rank[x]==self.rank[y]:
                self.rank[x]+=1

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

Submission status: Wrong Answer

Solution code:
###
### 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 = []
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:
    if link[2]%2 == 1:
        ans[link[1]-1] = 1 if ans[link[0]-1] == 0 else 0

for i in ans:
    print(i)

Submission status: Wrong Answer

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

edge = sorted(edge)
ans = [0]*n
for u,v,w in edge:
  u -= 1
  v -= 1
  if w%2 == 0:
    ans[v] = 0 if ans[u] is 0 else 1
  else:
    ans[v] = 1 if ans[u] is 0 else 0

print('\n'.join(map(str, ans)))

Submission status: Wrong Answer

Solution code:
n=int(input())
uvw=[list(map(int,input().split())) for _ in range(n-1)]
uvw=[[u-1,v-1,w] for u,v,w in uvw]
uvw=sorted(uvw,key=lambda x:x[0])

res=[-1 for _ in range(n)]
j=True
for u,v,w in uvw:
    if w%2!=0:
        j=False
        res[u]=0
        res[v]=1
        break
if j:
    res[0]=0
for u,v,w in uvw:
    if w%2!=0:
        if res[u]==0:
            res[v]=1
        elif res[u]==1:
            res[v]=0
        elif res[v]==0:
            res[u]=1
        elif res[v]==1:
            res[u]=0
for u,v,w in uvw:
    if w%2==0:
        if res[u]>=0 and res[v]<0:
            res[v]=res[u]
        elif res[v]>=0:
            res[u]=res[v]
    else:
        if res[u]==0:
            res[v]=1
        elif res[u]==1:
            res[v]=0
        elif res[v]==0:
            res[u]=1
        elif res[v]==1:
            res[u]=0
for u,v,w in uvw:
    if w%2==0:
        if res[u]>=0 and res[v]<0:
            res[v]=res[u]
        elif res[v]>=0:
            res[u]=res[v]
    else:
        if res[u]==0:
            res[v]=1
        elif res[u]==1:
            res[v]=0
        elif res[v]==0:
            res[u]=1
        elif res[v]==1:
            res[u]=0
for ans in res:
    print(ans)

Submission status: Wrong Answer

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

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

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

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

N = int(input())
u = []
v = []
w = []

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

union = UnionFind(N)

for i in range(N-1):
    if w[i] % 2 == 0:
        if not union.same_check(u[i], v[i]):
            union.union(u[i], v[i])
ans = []
for i in range(1, N+1):
    if union.same_check(1, i):
        ans.append(0)
    else:
        ans.append(1)

for c in ans:
    print(c)

Submission status: Wrong Answer

Solution code:
N = int(input())
Vs = []
for _ in range(N-1):
    Vs.append([int(i) for i in input().split()])
Ws = [None for _ in range(N)]
Ws[Vs[0][0]-1] = True
for ui, vi, wi in Vs:
    print(ui, vi)
    ui, vi = ui-1, vi-1
    
    if Ws[ui] != None and Ws[vi] == None:
        if wi % 2 == 0:
            Ws[vi] = Ws[ui]
        else:
            Ws[vi] = not Ws[ui]
    elif Ws[ui] == None and Ws[vi] != None:
        if wi % 2 == 0:
            Ws[ui] = Ws[vi]
        else:
            Ws[ui] = not Ws[vi]
for w in Ws:
    if w:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
n=int(input())  #数値入力

list_v = []
for i in range(n-1):
    list_v.append([int(x) for x in input().split()])

import numpy as np
arr_v = np.array(list_v)

arr_v = arr_v[np.where(arr_v[:,2]%2==1)]
arr_e =  arr_v[:,:2]
arr_e = arr_e[np.argsort(arr_e[:,0])]

# 2 : 未定, 0 : 白, 1 : 黒
list_c = [2 for _ in range(n)]

for v, u in arr_e:
    if list_c[v-1] == 2:
        list_c[v-1] = 0
        list_c[u-1] = 1
    elif list_c[v-1] == 0:
        list_c[u-1] = 1
    elif list_c[v-1] == 1:
        list_c[u-1] = 0
list_c = [c if c <=1 else 0 for c in list_c]

for c in list_c:
    print(c)

Submission status: Wrong Answer

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

N = int(raw_input())
u = list()
v = list()
w = list()

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

_white = [u[0]]
_black = []

for i in range(N-1):
    if u[i] in _black+_white and v[i] in _black+_white:
        continue

    if u[i] in _black+_white:
        _a = u[i]
        _b = v[i]
    elif v[i] in _black+_white:
        _a = v[i]
        _b = u[i]

    if _a in _black:
        if w[i]%2:
            _white.append(_b)
        else:
            _black.append(_b)
    elif _a in _white:
        if w[i]%2:
            _black.append(_b)
        else:
            _white.append(_b)

for i in range(1,N+1):
    if i in _white:
        print "0"
    elif i in _black:
        print "1"
    else:
        print "*"

Submission status: Wrong Answer

Solution code:
import collections


class BellmanFord():
    def __init__(self):
        self.edges = []

    def add(self, u, v, d, directed=False):
        """
        u = from, v = to, d = cost
        directed = Trueのとき、有向グラフである。
        """
        if directed is False:
            self.edges.append([u, v, d])
            self.edges.append([v, u, d])
        else:
            self.edges.append([u, v, d])

    def BellmanFord_search(self, s):
        """
        次のBellmanFord_negative_boolまで考慮した時には、0-indexedであることに注意
        s =  始点
        return: d[i] 始点sから各点iまでの最短経路
        """
        d = collections.defaultdict(lambda: float('inf'))
        d[s] = 0
        numEdges = len(self.edges)
        while True:
            update = False
            for i in range(numEdges):
                e = self.edges[i]
                # e: 辺iについて [from,to,cost]
                if d[e[0]] != float("inf") and d[e[1]] > d[e[0]] + e[2]:
                    d[e[1]] = d[e[0]] + e[2]
                    update = True
            if not update:
                break
        return d

    def BellmanFord_negative_bool(self, numNodes, zero_indexed=False):
        # 負の閉路の検出, Trueなら負の閉路が存在する
        d = collections.defaultdict(lambda: float('inf'))
        d[0] = 0
        numEdges = len(self.edges)
        for i in range(numNodes):
            for j in range(numEdges):
                e = self.edges[j]
                if d[e[1]] > d[e[0]] + e[2]:
                    d[e[1]] = d[e[0]] + e[2]
                    if i == numNodes-1:
                        return True
        return False

N = int(input())
uvw = []
tree = BellmanFord()
for i in range(N-1):
    uvw.append(list(map(int,input().split())))
    tree.add(uvw[i][0],uvw[i][1],uvw[i][2])

a = tree.BellmanFord_search(1)

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

Submission status: Wrong Answer

Solution code:
N = int(input())
l = [-100] * N
for i in range(1, N):
    u, v, w = map(int, input().split())
    if (w%2==0):
        if (l[u-1]<0 and l[v-1]<0):
            l[u-1] = 1
            l[v-1] = 1
        elif (l[u-1]<0):
            l[u-1] = l[v-1]
        else:
            l[v-1] = l[u-1]

for i in range(N):
    if (l[i] < 0):
        print(0)
    else:
        print(l[i])

Submission status: Wrong Answer

Solution code:
class Edge:
    def __init__(self, v1, v2, weight):
        self.v1 = min(v1, v2)
        self.v2 = max(v1, v2)
        self.weight = weight
        self.isOdd = weight % 2 > 0
        self.isEven = not self.isOdd

    def __lt__(self, other):
        return (self.v1, self.v2) < (other.v1, other.v2)

White = True
Black = False

N = int(input())
edges = {}
colored_v = {}
uncolored_v = [x for x in range(1, N+1)]

for i in range(1, N):
    v1, v2, w = list(map(int, input().split()))
    edges[i] = Edge(v1, v2, w)


for _ in range(40):
    if len(uncolored_v) == 0:
        break
    colored_v[uncolored_v[0]] = White
    uncolored_v.remove(uncolored_v[0])
    for ek,ev in sorted(edges.items()):
        if ev.v1 not in colored_v:
            continue
        if ev.v1 in colored_v and ev.v2 in colored_v:
            continue
        if ev.isEven:
            colored_v[ev.v2] = colored_v[ev.v1]
            uncolored_v.remove(ev.v2)
        if ev.isOdd:
            colored_v[ev.v2] = not colored_v[ev.v1]
            uncolored_v.remove(ev.v2)




for v in sorted(colored_v.items()):
    print(0 if v[1] else 1)

Submission status: Wrong Answer

Solution code:
N = int(input())
colorList = [0] * N
# print(colorList)

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

Submission status: Wrong Answer

Solution code:
n = int(input())
j = 0
color = [0] * n
while n > j+1:
    u,v,k = [int(x) for x in input().split()]
    color[u-1] = 0
    color[v-1] = 1
    if(k%2 == 0):
        color[v-1] = 0
    
    j += 1
    
for i in range(n):
    print(color[i])

Submission status: Wrong Answer

Solution code:
N = int(input())
lst_ans = [0] * N


for _ in range(N-1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    if w % 2 != 0:
        if lst_ans[u] == lst_ans[v]:
            if lst_ans[v] == 0:
                lst_ans[v] = 1
            else:
                lst_ans[v] = 0

for ans in lst_ans:
    print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())


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)


uf = UnionFind(n + 1)

for _ in range(n - 1):
    u, v, w = list(map(int, input().split()))
    if w % 2 == 0:
        uf.union(u, v)

for i in range(1, n + 1):
    if uf.same_check(i, 1):
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
n = int(input())
u = [0] * n
v = [0] * n
w = [0] * n
for i in range(n-1):
    u[i], v[i], w[i] = map(int, input().split())

tyoten = [0] * n
for i in range(n):
    for j in range(i):
        if (u[j] == i or v[j] == i) and w[j] % 2 != 0:
            tyoten[i] = 1

for t in tyoten:
    print(t)

Submission status: Wrong Answer

Solution code:
class UnionFind(object):

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

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

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

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

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

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


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

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

# uniteが一度も起きていないとき
if base_idx is None:
    for i in range(N):
        if i % 2 == 0:
            print(0)
        else:
            print(1)
else:
    for num in unionfind.tree[1:]:
        if num == unionfind.tree[base_idx]:
            print(0)
        else:
            print(1)

Submission status: Wrong Answer

Solution code:
N = int(input())
import sys
graph = [dict() for _ in range(N)]

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

color = [0] * N

import sys
sys.setrecursionlimit(10 ** 9)

def dfs(i, col):
    color[i] = col
    for k, v in graph[i].items():
        if color[k] == 0:
            if v % 2 == 0:
                dfs(k, col)
            else:
                dfs(k, 2)

dfs(0, 1)

# 1 -> 0
# 2 -> 1

for c in color:
    print(c-1)

Submission status: Wrong Answer

Solution code:
N = int(input())-1
vw = [list(map(int, input().split())) for _ in range(N)]
ret = [0] * (N+1)
for i in range(N):
    if vw[i][2] % 2 == 0:
        ret[vw[i][0]-1] = 1
        ret[vw[i][1]-1] = 1

for i in ret:
    print(i)

Submission status: Wrong Answer

Solution code:
n = int(input())
oy = [0] * (n + 1)
dif = [0] * (n + 1)
hen = []
for i in range(n - 1):
    hen.append(list(map(int, input().split())))

hen.sort()

for i in range(n - 1):
    u = hen[i][0]
    v = hen[i][1]
    w = hen[i][2]
    if oy[u] == 0:
        if oy[v] == 0:
            oy[u] = v
            oy[v] = v
        else:
            oy[u] = oy[v]
        dif[u] = dif[v] + w
    else:
        if oy[v] == 0:
            oy[u] = u
            oy[v] = oy[u]
            dif[v] = dif[u] + w
        else:
            for j in range(n):
                if oy[j] == oy[u]:
                    oy[j] = oy[v]
                    dif[j] += w + dif[v]

for i in range(1, n + 1):
    print(dif[i] % 2)

Submission status: Wrong Answer

Solution code:
N=int(input())
tree=[]
diff=[]
ans=[0 for i in range(N+1)]
for i in range(N-1):
    tmp=list(map(int,input().split()))
    if tmp[2]%2==0:
        tree.append(tmp[:2])
        ans[tmp[1]]=ans[tmp[0]]
    else:
        ans[tmp[1]]=1-ans[tmp[0]]
        
for i in ans[1:]:
    print(i)

Submission status: Wrong Answer

Solution code:
n=int(input())
sides=[list(map(int,input().split())) for _ in range(n-1)]
sides=[[side[0]-1,side[1]-1,side[2]]for side in sides]
print(sides)
color=[-1 for _ in range(n)]
color[0]=0
while True:
    u,v,w=sides.pop(0)
    # print(u,v,w%2)
    if color[u]==-1 and color[v]==-1:
        sides.append([u,v,w])
    elif color[v]==-1:
        # print('u=',color[u],w%2)
        color[v]=(color[u]^(w%2))
    elif color[u]==-1:
        # print('v=',color[v],w%2)
        color[u]=(color[v]^(w%2))
    if len(sides)==0:
        break
# print(color)
for c in color:
    print(c)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [None for _ in range(n)]
for i in range(n-1):
    u, v, w = map(int,input().split())
    if w % 2 == 0:
        if a[u-1] == None and a[v-1] == None:
            a[u-1] = 1
            a[v-1] = 1
        elif a[u-1] != None:
            a[v-1] = a[u-1]
        elif a[v-1] != None:
            a[u-1] = a[v-1]
    else:
        if a[u-1] == None and a[v-1] == None:
            a[u-1] = 0
            a[v-1] = 1
        elif a[u-1] != None:
            a[v-1] = 1 - a[u-1]
        elif a[v-1] != None:
            a[u-1] = 1 - a[v-1]
            
for b in a:
    print(b)

Submission status: Wrong Answer

Solution code:
N = int(input())
T = [tuple(map(int, input().split())) for _ in range(N-1)]
V = [[] for _ in range(N+1)]
check = [0]*(N+1)
color = [0]*(N+1)
for u, v, w in T:
    V[u].append((v, w))
    V[v].append((u, w))

color[1] = 1
check[1] = 1
queue = []
for u, w in V[1]:
    queue.append((u, w))
last_color = 1
while queue:
    u, w = queue.pop()
    check[u] = 1
    if w % 2:
        if last_color:
            last_color = 0
        else:
            last_color = 1
    color[u] = last_color
    for next_u, next_w in V[u]:
        if check[next_u] == 0:
            queue.append((next_u, next_w))

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

Submission status: Wrong Answer

Solution code:
import array
import collections
import heapq

AdjacentVertex = collections.namedtuple("AdjacentVertex", "vertex cost")
INF = 2**31 - 1
NO_VERTEX = -1


# Prim法で頂点0からの最小全域木を求める
def compute_mst_prim(max_v, adj_list):
    # pred[u]は頂点uの「ひとつ前」の頂点を表す
    pred = collections.defaultdict(lambda: NO_VERTEX)
    # uとpred[u]を結ぶ辺の重みがkey[u]に入る
    key = collections.defaultdict(lambda: INF)
    key[0] = 0
    # 二分ヒープを優先度付きキューとして用いる
    pq = [(key[v], v) for v in range(max_v)]
    heapq.heapify(pq)
    # 優先度付きキューに頂点が入っているかを示す配列
    in_pq = array.array("B", (True for _ in range(max_v)))
    while pq:
        _, u = heapq.heappop(pq)
        in_pq[u] = False
        for v, v_cost in adj_list[u]:
            if in_pq[v]:
                weight = v_cost
                if weight < key[v]:
                    pred[v] = u
                    key[v] = weight
                    heapq.heappush(pq, (weight, v))
                    in_pq[v] = True
    return (pred, key)


def main():
    max_v = int(input())
    max_e = max_v - 1
    adjacency_list = collections.defaultdict(set)
    for _ in range(max_e):
        s, t, w = map(int, input().split())
        s -= 1
        t -= 1
        adjacency_list[s].add(AdjacentVertex(t, w))
        adjacency_list[t].add(AdjacentVertex(s, w))
    (_, key) = compute_mst_prim(max_v, adjacency_list)
    for i in range(max_v):
        print(key[i] % 2)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict, deque
from math import ceil, log2
stdin = sys.stdin
def ii(): return int(stdin.readline())
def li(): return map(int, stdin.readline().split())
N = ii()
seen = dict()
lr = defaultdict(list)
for i in range(N - 1):
    l, r, d= tuple(li())
    lr[l].append([r, d])
    lr[r].append([l, r])

# start from 1, color it black
queue = deque([[1, 0]])
seen[1] = 0
while queue:
    cur, color = queue.popleft()
    for nei, d in lr[cur]:
        if nei in seen: continue
        else:
            new_color = seen[cur] if ((d & 1) == 0) else (1 - seen[cur])
            seen[nei] = new_color
            queue.append([nei, new_color])
for i in range(1, N + 1):
    print(seen[i])

Submission status: Wrong Answer

Solution code:
for _ in range(5):
  print("1")

Submission status: Wrong Answer

Solution code:
n=int(input())
vertex=[i for i in range(n)]
u,v,w=[],[],[]
for i in range(n-1):
    ui,vi,wi=[int(x) for x in input().split()]
    u.append(ui)
    v.append(vi)
    w.append(wi)
for i in range(n-1):
    if w[i]%2==0:
        ui=vertex[u[i]-1]
        vi=vertex[v[i]-1]
        vertex=[x if not x==vi else ui for x in vertex ]
ans=[0]*n
for i in range(n-1):
    if w[i]%2==1:
        ui=vertex[u[i]-1]
        vi=vertex[v[i]-1]
        ans=[ans[j] if not vertex[j]==vi else 1^ans[u[j-1]-1] for j in range(n)]
for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
n =int(input())
lis = []
for i in range(n-1):
    lis.append(list(map(int,input().split())))
lis.sort()
l1 = [1 for x in range(n)]
for i in lis:
    if i[2]%2!=0:
        l1[i[1]-1] = l1[i[0]-1] * (-1)
l1 = list(map(lambda evt:(evt+1)//2,l1))
for i in l1:
    print(i)

Submission status: Wrong Answer

Solution code:

class UNION_FIND(object):
    def __init__(self,n):
        #親の番号を格納する。親だった場合は-(その集合のサイズ)
        #作るときはParentの値を全て-1にする
        #こうすると全てバラバラになる
        self.parent=[-1 for i in range(n)]
        
    def root(self,x):
        #Aがどのグループに属しているか調べる
        if self.parent[x]<0:
            return x
        else:
            self.parent[x]=self.root(self.parent[x])  #親を再帰で探知 再帰は最後に上のifにひっかかる
            return self.parent[x]
 
    def size(self,x):
        #自分のいるグループの頂点数を調べる
        return -self.parent[self.root(x)]   #親をとってきたい
        
    def union(self,x,y):
        #AとBをくっ付ける
        #AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
        #print(self.parent)
        x=self.root(x)
        y=self.root(y)
        #print(x,y)
        if x==y:   #すでにくっついてるからくっ付けない
            return False
            
        #大きい方(A)に小さいほう(B)をくっ付けたい
        #大小が逆だったらひっくり返しちゃう。
        if self.size(x)<self.size(y):
            x,y=y,x
        self.parent[x]+=self.parent[y]  #Aのサイズを更新する
        self.parent[y]=x   #Bの親をAに変更する
        return True
        
n=int(input())        
u=UNION_FIND(n)
for i in range(n-1):
    a,b,c=map(int,input().split())
    if c%2==0:
        u.union(a-1,b-1)
for i in u.parent:
    if i==-1:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

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

ans={}
for item in A:
    u=int(item[0])
    v=int(item[1])
    w=int(item[2])
    if w%2!=0:
        if (ans.get(u)==None) or (ans.get(u)==0):
            ans[v]=1
        else:
            ans[v]=0
                                  
for k in range(1,N+1):
    if ans.get(k)==None:
        print(0)
    else:
        print(ans.get(k))

Submission status: Wrong Answer

Solution code:
class WUF:
    __slots__ = ['ps', 'rs', 'ws']

    def __init__(self, n):
        self.ps = [i for i in range(n + 1)]
        self.rs = [0 for _ in range(n + 1)]
        self.ws = [0 for _ in range(n + 1)]

    def find(self, x):
        if self.ps[x] == x:
            return x
        else:
            y = self.find(self.ps[x])
            self.ws[x] += self.ws[self.ps[x]]
            self.ps[x] = y
            return y

    def union(self, x, y, w):
        rx = self.find(x)
        ry = self.find(y)
        if self.rs[rx] < self.rs[ry]:
            self.ps[rx] = ry
            self.ws[rx] = w - self.ws[x] + self.ws[y]
        else:
            self.ps[ry] = rx
            self.ws[ry] = -w - self.ws[y] + self.ws[x]
            if self.rs[rx] == self.rs[ry]:
                self.rs[rx] += 1

    def diff(self, x, y):
        rx = self.find(x)
        ry = self.find(y)
        return self.ws[x] - self.ws[y]


def task_d():
    n = int(input())
    uvw = (map(int, input().split()) for _ in range(n-1))
    
    wuf = WUF(n)
    for u, v, w in uvw:
        wuf.union(u, v, w)
    for i in range(1, n+1):
        print(wuf.diff(i, wuf.ps[i]) % 2)
task_d()

Submission status: Wrong Answer

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