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:
import sys
n = int(input())
mark = [[[],[]]for _ in range(n+1)]
color = [0 for _ in range(n+1)]
done = set()
for i in range(n-1):
    a,b,c = map(int,sys.stdin.readline().split())
    if c&1:
        mark[a][1].append(b)
        mark[b][1].append(a)
    else:
        mark[a][0].append(b)
        mark[b][0].append(a)
run = [1]
temp = []
while(len(run))>0:
    next = run.pop(0)
    done.add(next)
    if color[next]==0:
        for v in mark[next][1]:
            if v not in done:
                color[v]=1
                temp.append(v)
        for v in mark[next][0]:
            if v not in done:
                temp.append(v)
    else:
        for v in mark[next][0]:
            if v not in done:
                color[v]=0
                temp.append(v)
        for v in mark[next][1]:
            if v not in done:
                temp.append(v)
    if len(run)==0:
        run=run+temp
        temp=[]
for i in range(1,n+1):
    sys.stdout.write(str(color[i])+'\n')

Submission status: Wrong Answer

Solution code:
n = int(input())
mark = [[[],[]]for _ in range(n+1)]
color = [0 for _ in range(n+1)]
done = set()
for i in range(n-1):
    a,b,c = map(int,input().split())
    if c&1:
        mark[a][1].append(b)
        mark[b][1].append(a)
    else:
        mark[a][0].append(b)
        mark[b][0].append(a)
run = [1]
while(len(run))>0:
    next = run.pop(0)
    done.add(next)
    if color[next]==0:
        for v in mark[next][1]:
            if v not in done:
                color[v]=1
                run.append(v)
        for v in mark[next][0]:
            if v not in done:
                run.append(v)
    else:
        for v in mark[next][0]:
            if v not in done:
                color[v]=0
                run.append(v)
        for v in mark[next][1]:
            if v not in done:
                run.append(v)
for i in range(1,n+1):
    print(color[i])

Submission status: Wrong Answer

Solution code:
N = int(input())

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

dist = [0 for _ in range(N)]
def measure(src):
    """
    srcを根とした木に対し、根から各頂点までの距離を求める
    """
    for v, w in edges[src]:
        dist[v] = dist[src] + w
        measure(v) 
measure(0)

for i in range(N):
    if dist[i] % 2:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
n = int(input())
ulist = []
vlist = []
wlist = []
dic = {}
for i in range(n-1):
    u,v,w = map(int, input().strip().split(" "))
    ulist.append(u)
    vlist.append(v)
    wlist.append(w)
for i in range(len(ulist)):
    if ulist[i] not in dic and vlist[i] not in dic:
        if wlist[i] % 2 == 0:
            dic[vlist[i]] = 0
            dic[ulist[i]] = 0
        else:
            dic[vlist[i]] = 1
            dic[ulist[i]] = 0
    if ulist[i] in dic and vlist[i] not in dic:
        if wlist[i] % 2 == 0:
            dic[vlist[i]] = dic[ulist[i]]
        else:
            if dic[ulist[i]] == 0:
                dic[vlist[i]] = 1
            if dic[ulist[i]] == 1:
                dic[vlist[i]] = 0
    if vlist[i] in dic and ulist[i] not in dic:
        if wlist[i] % 2 == 0:
            dic[ulist[i]] = dic[vlist[i]]
        else:
            if dic[vlist[i]] == 0:
                dic[ulist[i]] = 1
            if dic[vlist[i]] == 1:
                dic[ulist[i]] = 0
for i in range(len(dic)):
    print(dic[i+1])

Submission status: Wrong Answer

Solution code:

from operator import itemgetter

N = int(input())
N_list = []
ans = []

#各行をN_listに読み込み
for i in range(N-1):
    N_list.append(list(map(int,input().split())))

#N_listをuiでソート
ui_N_list = sorted(N_list,key = itemgetter(0))

#黒が1 、最初は黒に塗る
is_Black = True
print(int(is_Black))

for i in range(len(ui_N_list)):
    #現在黒か
    if is_Black :
        #wiが奇数のとき、白に入れ替え(反転)
        if ui_N_list[i][2] % 2 != 0:
            is_Black = not(is_Black)
        print(int(is_Black))
    #白ならば wi が偶数のときに黒に入れ替え(反転)
    else :
        if ui_N_list[i][2] % 2 == 0:
            is_Black = not(is_Black)
        print(int(is_Black))

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

N = int(input())
N_list = []
ans = []

#各行をN_listに読み込み
for i in range(N-1):
    N_list.append(list(map(int,input().split())))

#N_listをuiでソート
ui_N_list = sorted(N_list,key = itemgetter(1))

#黒が1 、最初は黒に塗る
is_Black = True
print(int(is_Black))

for i in range(len(ui_N_list)):
    #現在黒か
    if is_Black :
        #wiが奇数のとき、白に入れ替え(反転)
        if ui_N_list[i][2] % 2 != 0:
            is_Black = not(is_Black)
        print(int(is_Black))
    #白ならば wi が偶数のときに黒に入れ替え(反転)
    else :
        if ui_N_list[i][2] % 2 == 0:
            is_Black = not(is_Black)
        print(int(is_Black))

Submission status: Wrong Answer

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

def dfs(c, m, a, b):
    for i in range(N):
        if i == a:
            continue
        if m[b][i]:
            if m[b][i] % 2:
                c[i] = 1 - c[b]
            dfs(c, m, b, i)

c = [0 for _ in range(N)]

m = [[0 for _ in range(N)] for _ in range(N)]
for i in range(N-1):
    m[uvw[i][0]-1][uvw[i][1]-1] = uvw[i][2]
    m[uvw[i][1]-1][uvw[i][0]-1] = uvw[i][2]

dfs(c, m, 0, 0)

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

Submission status: Wrong Answer

Solution code:

N = int(input())

ans = []

#黒が1 、最初は黒に塗る
is_Black = True

ans.append(int(is_Black))

for i in range(N-1):
    ui,vi,wi = map(int,input().split())
    #現在黒か
    if is_Black :
        #wiが奇数のとき、白に入れ替え(反転)
        if wi % 2 != 0:
            is_Black = not(is_Black)
        ans.append(int(is_Black))
    #白ならば wi が偶数のときに黒に入れ替え(反転)
    else :
        if wi % 2 == 0:
            is_Black = not(is_Black)
        ans.append(int(is_Black))

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

Submission status: Wrong Answer

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

# 色を初期化
color = [0 for _ in range(N)]

for i in range(N-1):
    if uvw[i][2] % 2:
        color[uvw[i][1]-1] = 1 - color[uvw[i][0]-1]

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

Submission status: Wrong Answer

Solution code:

N = int(input())

ans = []

#黒が1 、最初は黒に塗る
is_Black = True

ans.append(int(is_Black))

for i in range(N-1):
    ui,vi,wi = map(int,input().split())
    #wiが奇数のとき、白黒を入れ替え
    if wi % 2 != 0:
        is_Black = not(is_Black)
    
    ans.append(int(is_Black))

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

Submission status: Wrong Answer

Solution code:

N = int(input())

ans = []

#黒が1 、最初は黒に塗る
is_Black = True

for i in range(N-1):
    ui,vi,wi = map(int,input().split())
    #wiが奇数のとき、白黒を入れ替え
    if wi % 2 != 0:
        is_Black = not(is_Black)
    
    ans.append(int(is_Black))

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

Submission status: Wrong Answer

Solution code:
import math
N=int(input())
edges=[[] for i in range(N+1)]
d=[-1 for i in range(N+1)]
for i in range(1,N):
	u,v,w=[int(i) for i in input().split()]
	edges[u].append([v,w])
def kyori(u):
	for v,w in edges[u]:
		d[v]=d[u]+w
		kyori(v)
for i in range(1,N):
	if d[i]==-1:
		d[i]=0
		kyori(i)
	else:
		continue
for i in d[1:]:
	if i%2==0:
		print(1)
	else:
		print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(500000)


N = int(input())

tree_dic = {i+1:[] for i in range(N)}
bw_list = [0]*N

for i in range(1, N):
    ui, vi, wi = map(int, input().split())
    if wi % 2 == 0:
        tree_dic[ui].append((vi, "same"))
        tree_dic[vi].append((ui, "same"))
    else:
        tree_dic[ui].append((vi, "diff"))
        tree_dic[vi].append((ui, "diff"))

same_color_groap_list = []
def decide_color(same_color_groap_list, tree_dic, current_point):
    if 0 not in same_color_groap_list:
        return None
    for next_point, same_diff in tree_dic[current_point]:
        if bw_list[next_point-1] == 0:    #   まだ色が決まっていない時
            if same_diff == "same":
                bw_list[next_point-1] = bw_list[current_point-1]
                return decide_color(bw_list, tree_dic, next_point)
            else:
                bw_list[next_point-1] -= bw_list[current_point-1]
                return decide_color(bw_list, tree_dic, next_point)

bw_list[0] = 1
decide_color(same_color_groap_list, tree_dic, 1)

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

Submission status: Wrong Answer

Solution code:
# D - Even Relation
n = int(input())
data = sorted([input().split() for i in range(n-1)], key=lambda x: (x[0], x[1]))
l = ["N" for i in range(n+1)]
l[0] = "NaN"
l[1] = 0

for w in data:
    if int(w[2]) % 2 == 0:
        w[2] = "0"
    else:
        w[2] = "1"

for i in range(1, n):
    for j in range(1, n+1):
        for k in range(0, n-1):
            if type(l[i]) == int:
                if data[k] == [str(i), str(j), "1"] and l[i] == 0:
                    l[j] = 1
                elif data[k] == [str(i), str(j), "1"] and l[i] == 1:
                    l[j] = 0
                elif data[k] == [str(i), str(j), "0"] and l[i] == 0:
                    l[j] = 0
                elif data[k] == [str(i), str(j), "0"] and l[i] == 1:
                    l[j] = 1
            if type(l[j]) == int:
                if data[k] == [str(i), str(j), "1"] and l[j] == 0:
                    l[i] = 1
                elif data[k] == [str(i), str(j), "1"] and l[j] == 1:
                    l[i] = 0
                elif data[k] == [str(i), str(j), "0"] and l[j] == 0:
                    l[i] = 0
                elif data[k] == [str(i), str(j), "0"] and l[j] == 1:
                    l[i] = 1

l.remove("NaN")
print(*l, sep="\n")

Submission status: Wrong Answer

Solution code:
# D - Even Relation
n = int(input())
data = sorted([input().split() for i in range(n-1)], key=lambda x: (x[0], x[1]))
l = ["N" for i in range(n+1)]
l[0] = "NaN"
l[1] = 0

for w in data:
    if int(w[2]) % 2 == 0:
        w[2] = "0"
    else:
        w[2] = "1"

for i in range(1, n):
    for j in range(1, n+1):
        for k in range(0, n-1):
            if type(l[i]) == int or type(l[i]) == int:
                if data[k] == [str(i), str(j), "1"] and l[i] == 0:
                    l[j] = 1
                elif data[k] == [str(i), str(j), "1"] and l[i] == 1:
                    l[j] = 0
                elif data[k] == [str(i), str(j), "0"] and l[i] == 0:
                    l[j] = 0
                elif data[k] == [str(i), str(j), "0"] and l[i] == 1:
                    l[j] = 1
                elif data[k] == [str(i), str(j), "1"] and l[j] == 0:
                    l[i] = 1
                elif data[k] == [str(i), str(j), "1"] and l[j] == 1:
                    l[i] = 0
                elif data[k] == [str(i), str(j), "0"] and l[j] == 0:
                    l[i] = 0
                elif data[k] == [str(i), str(j), "0"] and l[j] == 1:
                    l[i] = 1

l.remove("NaN")
print(*l, sep="\n")

Submission status: Wrong Answer

Solution code:
# D - Even Relation
n = int(input())
data = sorted([input().split() for i in range(n-1)], key=lambda x: (x[0], x[1]))
l = ["N" for i in range(n+1)]
l[0] = "NaN"
l[1] = 0

for w in data:
    if int(w[2]) % 2 == 0:
        w[2] = "0"
    else:
        w[2] = "1"

for i in range(1, n):
    for j in range(1, n+1):
        for k in range(0, n-1):
            if data[k] == [str(i), str(j), "1"] and l[i] == 0:
                l[j] = 1
            elif data[k] == [str(i), str(j), "1"] and l[i] == 1:
                l[j] = 0
            elif data[k] == [str(i), str(j), "0"] and l[i] == 0:
                l[j] = 0
            elif data[k] == [str(i), str(j), "0"] and l[i] == 1:
                l[j] = 1
            elif data[k] == [str(i), str(j), "1"] and l[j] == 0:
                l[i] = 1
            elif data[k] == [str(i), str(j), "1"] and l[j] == 1:
                l[i] = 0
            elif data[k] == [str(i), str(j), "0"] and l[j] == 0:
                l[i] = 0
            elif data[k] == [str(i), str(j), "0"] and l[j] == 1:
                l[i] = 1

l.remove("NaN")
print(*l, sep="\n")

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = [True] * N
already = []


def solve(a, b, c):
    if c % 2 == 0:
        ans[a - 1] = ans[b - 1]
    else:
        ans[a - 1] = not ans[b - 1]
    already.append(a)


for i in range(1, N):
    u, v, w = map(int, input().split())
    if i == 1:
        already.append(u)
    if u in already:
        solve(v, u, w)
    else:
        solve(u, v, w)

for i in ans:
    if i:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon May 20 23:40:57 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,0] for _ in range(size)]

    def find(self, x):
        if self.table[x][0] < 0:
            return x
        else:
            tmp = self.table[x][0]
            self.table[x][0] = self.find(tmp)
            self.table[x][1] += self.table[tmp][1]
#            self.table[x][2] *= self.table[self.find(self.table[x][0])][3]
            return self.table[x][0]
        
    def union(self, x, y, d):
        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[s1][1] + d
#                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[s2][1] + d
#                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],c[2])
#    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%2)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon May 20 23:40:57 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,0] for _ in range(size)]

    def find(self, x):
        if self.table[x][0] < 0:
            return x
        else:
            tmp = self.table[x][0]
            self.table[x][0] = self.find(tmp)
            self.table[x][1] += self.table[tmp][1]
#            self.table[x][2] *= self.table[self.find(self.table[x][0])][3]
            return self.table[x][0]
        
    def union(self, x, y, d):
        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[s1][1] + d
#                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[s2][1] + d
#                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],c[2])
    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%2)

Submission status: Wrong Answer

Solution code:
import sys


def parse(file):
    n = int(file.readline())
    edges = []
    for _ in range(n-1):
        u, v, w = map(int, file.readline().split())
        edges.append([u,v,w])

    return n, edges

def main(n, edges):
    blacks = [1]
    neighbors = {}
    for edge in edges:
        u, v, w = edge
        if u not in neighbors:
            neighbors[u] = []
        if v not in neighbors:
            neighbors[v] = []

        neighbors[u].append([v, w % 2])
        neighbors[v].append([u, w % 2])

    visited = []
    stack = []
    stack.append(1)

    while stack:
        node = stack.pop()

        for neigh in neighbors[node]:
            v, w = neigh
            if v not in visited:
                visited.append(v)
                stack.append(v)
                if w == 0:
                    blacks.append(v)

    ans = ''
    for i in range(1, n+1):
        if i in blacks:
            ans += '0\n'
        else:
            ans += '1\n'

    return ans[:-1]

if __name__ == '__main__':
    print(main(*parse(sys.stdin)))

Submission status: Wrong Answer

Solution code:
class POTENTIAL_UNION_FIND(object):
    def __init__(self, n):
        self.parent = [-1 for i in range(n)]#非負なら親ノード，負ならグループの要素数
        self.diff_p = [0 for i in range(n)]#親ノードを基準としたポテンシャル
 
    def root(self, x): #root(x): xの根ノードを返す
        if self.parent[x] < 0:
            return x
        else:
            rx = self.root(self.parent[x]) #xをxの根rxに直接つなぎたい
            self.diff_p[x] += self.diff_p[self.parent[x]] #そのために，diff(rx,x)を計算
            self.parent[x] = rx #xをrxに直接つなげる
            return rx
 
    def size(self,x): #size(x): xのいるグループの要素数を返す
        return -self.parent[self.root(x)]

    def merge(self, x, y, dxy): #ポテンシャル差の条件p(y)-p(x)=dxyでxとyのグループをまとめる
        rx = self.root(x) #rxを新たにxと名づける
        ry = self.root(y) #ryを新たにyと名づける
        dxy += self.diff_p[x] - self.diff_p[y] #dxyをdiff(rx,ry)で置き換え
        x,y=rx,ry
        if x == y:
            return False
        if self.parent[x] > self.parent[y]: #xの要素数がyの要素数より「小さい」とき入れ替える
            x,y,dxy=y,x,-dxy
        self.parent[x] += self.parent[y] #xの要素数を更新
        self.parent[y] = x #yをxにつなぐ
        self.diff_p[y] = dxy #yの相対ポテンシャルを更新
        return True
 
    def issame(self, x, y): #issame(x,y): xとyが同じグループにあるならTrue
        return self.root(x) == self.root(y)
        
    def diff(self,x,y): #diff(x,y): xを基準としたyのポテンシャルを返す 
        if self.root(x) == self.root(y): #この時点でxの親はroot(x)
            return self.diff_p[y] - self.diff_p[x]
        else:
            return None

n=int(input())
u=POTENTIAL_UNION_FIND(n)
for i in range(n-1):
    a,b,c=map(int,input().split())
    u.merge(a-1,b-1,c)
for i in u.diff_p:
    if i%2==0:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
N = int(input())

U = []
V = []
W = []
for i in range(N-1):
    u, v, w = map(int, input().split())
    U.append(u-1)
    V.append(v-1)
    W.append(w)
    
uv = [[0 for i in range(N)] for j in range(N)]

for i in range(N-1):
    uv[U[i]][V[i]] = W[i]
    
color = [0]*N
for i in range(N):
    cnt = i + 1
    while cnt <= N-1:
        if uv[i][cnt]%2==1:
            if color[i]==0:
                color[cnt]=1
            else:
                color[cnt]=0
        cnt += 1
        
for i in range(N):
    print(color[i])

Submission status: Wrong Answer

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

color = [1 for _ in range(N+1)]
color[1] = 0
bi = []
if N > 2:
    for i in range(1,N+1):
        n1 = None
        n2 = None
        l1 = 0
        l2 = 0
        for x in weights:
            if x[0]==i:
                if n1 == None:
                    n1 = x[1]
                    l1 = x[2]
                else:
                    n2 = x[1]
                    l2 = x[2]
            if x[1]==i:
                if n1 == None:
                    n1 = x[1]
                    l1 = x[2]
                else:
                    n2 = x[1]
                    l2 = x[2]
        if n1 != None and n2 != None:
            bi.append([n1,n2,l1+l2])   
w2 = weights + bi
for _ in range(N):
    c = 0
    for x in w2:
        if x[2]%2 == 0:
            if color[x[0]] == 0 and color[x[1]] == 1 and  x[2]%2 == 0:
                color[x[1]] = color[x[0]]
                c = 1
            if color[x[1]] == 0 and color[x[0]] == 1 and  x[2]%2 == 0:
                color[x[0]] = color[x[1]]
                c = 1
    if c == 0:
        break
for x in color[1:]:
    print(x)

Submission status: Wrong Answer

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

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

# print("memo", memo)

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


for i in range(n):
  if (memo[i] != 0):
    continue
  memo[i] = 1
  node_num = i + 1
  # print("node_num", node_num)
  # print("=======================")
  paint(i, node_num)
  # print("memo", memo)
  # print("=======================")

# print("memo", memo)

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)

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


bw = [-1 for i in range(n + 1)]
bw[1] = 0

def dfs(frm):
	for ne in edges[frm]:
		if bw[ne[0]] >= 0:
			continue
		else:
			bw[ne[0]] = abs(bw[frm] - bw[ne[0]]) % 2
			dfs(ne[0])

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

Submission status: Wrong Answer

Solution code:
import numpy as np
n=[] 
while True:
    try:
        n.append(list(map(int,input().split())))
    except:
        break;

times=n[0]
a = n[1:]
ans=[3]*times[0]
for i in range(times[0]-1):
    if ans[a[i][0]-1]==3 and ans[a[i][1]-1]==3:
        if a[i][2]%2==0:
            ans[a[i][0]-1]=0;ans[a[i][1]-1]=0
        else:
	        ans[a[i][0]-1]=0;ans[a[i][1]-1]=1           
    if ans[a[i][0]-1]==3 and ans[a[i][1]-1]==0:
        if a[i][2]%2==0:
            ans[a[i][0]-1]=0
        else:
            ans[a[i][0]-1]=1
    if ans[a[i][0]-1]==0 and ans[a[i][1]-1]==3:
        if a[i][2]%2==0:
            ans[a[i][1]-1]=0
        else:
            ans[a[i][1]-1]=1  
    if ans[a[i][0]-1]==3 and ans[a[i][1]-1]==1:
        if a[i][2]%2==0:
            ans[a[i][0]-1]=0
        else:
            ans[a[i][0]-1]=0
    if ans[a[i][0]-1]==1 and ans[a[i][1]-1]==3:
        if a[i][2]%2==0:
            ans[a[i][1]-1]=0
        else:
            ans[a[i][1]-1]=0  
              
for i in range(times[0]):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
import numpy as np
n=[] 
while True:
    try:
        n.append(list(map(int,input().split())))
    except:
        break;

times=n[0]
a = n[1:]


print(a)
print(len(a))
print(times)

print([3]*times[0])
ans=[3]*times[0]
print(a)
print(a[0][0])
print(times[0]-2)

for i in range(times[0]-1):
    if ans[a[i][0]-1]==3 and ans[a[i][1]-1]==3:
        if a[i][2]%2==0:
            ans[a[i][0]-1]=0;ans[a[i][1]-1]=0
        else:
	        ans[a[i][0]-1]=0;ans[a[i][1]-1]=1           
    if ans[a[i][0]-1]==3 and ans[a[i][1]-1]==0:
        if a[i][2]%2==0:
            ans[a[i][0]-1]=0
        else:
            ans[a[i][0]-1]=1
    if ans[a[i][0]-1]==0 and ans[a[i][1]-1]==3:
        if a[i][2]%2==0:
            ans[a[i][1]-1]=0
        else:
            ans[a[i][1]-1]=1  
    if ans[a[i][0]-1]==3 and ans[a[i][1]-1]==1:
        if a[i][2]%2==0:
            ans[a[i][0]-1]=0
        else:
            ans[a[i][0]-1]=0
    if ans[a[i][0]-1]==1 and ans[a[i][1]-1]==3:
        if a[i][2]%2==0:
            ans[a[i][1]-1]=0
        else:
            ans[a[i][1]-1]=0  
              
for i in range(times[0]):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
N = int(input())
V = [[]for _ in range(N)]
V_W = [[0 for _ in range(N)] for _ in range(N)]
for _ in range(N-1):
  u,v,w = map(int,input().split())
  V[u-1].append(v-1)
  V[v-1].append(u-1)
  V_W[u-1][v-1] = w
ans = [-1 for _ in range(N)]
def f(c,w,bef):
  global V,V_W,ans
  if ans[c]!=-1:
    return
  ans[c] = bef if w%2==0 else abs(bef-1)
  for chi in V[c]:
    f(chi,V_W[c][chi],ans[c])
f(0,0,0)
for a in ans:
  print(a)

Submission status: Wrong Answer

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

def search(UF,loc):
    if UF[loc][0] == loc:
        return UF[loc]
    else:
        UF[loc][0],temp = search(UF,UF[loc][0])
        UF[loc][1] += temp
        return UF[loc]
for i in range(1,n+1):
    if search(UF,i)[1]%2 == 1:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
from queue import Queue as queue
import numpy as np
n = int(input())
e = [[] for i in range(n + 1)]
for i in range(n - 1):
    u, v, w = map(int, input().split())
    e[u].append([v, w])
    e[v].append([u, w])

q = queue()
donelist = [0 for i in range(n + 1)]

ans = [False for i in range(n + 1)]

for i in e[1]:
    q.put([i, False])
    donelist[1] = 1

while not q.empty():
    nx = q.get()
    print(nx)
    donelist[nx[0][0]] = 1
    if not nx[0][1] % 2:
        ans[nx[0][0]] = nx[1]
    else:
        ans[nx[0][0]] = not nx[1]
    for i in e[nx[0][0]]:
        if donelist[i[0]]:
            continue
        q.put([i, ans[nx[0][0]]])

del ans[0]

for i in ans:
    if i:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = [0 for i in range (n)]
for i in range(n-1):
  a,b,c = map(int,input().split())
  if c % 2 == 1:
    if ans[a-1] == ans[b-1]:
      ans[b-1] = 1-ans[b-1]
for i in range(n):
  print(ans[i])

Submission status: Wrong Answer

Solution code:
import sys
import heapq

def dijkstra(start, nodes, edges):
    dists = [sys.maxsize] * (len(nodes) + 1)
    dists[start] = 0

    queue = []
    # queueの要素：(dist, node)
    heapq.heappush(queue, (0, start))

    while len(queue) > 0:
        dist, node = heapq.heappop(queue)
        for v, e in enumerate(edges[node]):
            alt = dist + e
            if alt < dists[v]:
                dists[v] = alt
                heapq.heappush(queue, (alt, v))
    return dists


N = int(input())
edges = [[sys.maxsize] * (N + 1) for _ in range(N + 1)]

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

dists = dijkstra(1, list(range(1, N + 1)), edges)
result = ["0" if dist % 2 == 0 else "1" for dist in dists[1:]]
print("\n".join(result))

Submission status: Wrong Answer

Solution code:
N = int(input())

colors = [-1] * N
for _ in range(N-1):
    u, v, w = list(map(int, input().split()))
    u -= 1
    v -= 1
    if w % 2 == 0:
        if colors[u] == -1 and colors[v] == -1:
            colors[u] = 0
            colors[v] = 0
        elif colors[u] == -1:
            colors[u] = colors[v]
        elif colors[v] == -1:
            colors[v] = colors[u]
    else:
        if colors[u] == -1 and colors[v] == -1:
            colors[u] = 0
            colors[v] = 1
        elif colors[u] == -1:
            colors[u] = 1 - colors[v]
        elif colors[v] == -1:
            colors[v] = 1 - colors[u]
for c in colors:
    print(c)

Submission status: Wrong Answer

Solution code:
def solve():
    N = int(raw_input())
    ret = [0] * N
    changed = [False] * N
    for i in range(N - 1):
        ui,vi,wi = [int(_) for _ in raw_input().split()]
        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
    if ret[0] == 0:
        for i in range(N):
            print ret[i]
    else:
        for i in range(N):
            print 1 - ret[i]

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import math
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

# Union-Find #
def init(n):
    for i in range(N+1):
        par[i]=i
        rnk[i]=0
        num[i]=0
def find(x):
    if par[x]==x:
        return x
    else:
        res = find(par[x])
        return res
def unite(x,y,z):
    x=find(x)
    y=find(y)
    if x==y:
        return
    if (rnk[x]<rnk[y]):
        par[x]=y
        num[x]=z
    else:
        par[y]=x
        num[y]=z
        if(rnk[x]==rnk[y]):
            rnk[x]+=1
def same(x,y):
    return find(x)==find(y)
def tami(x):
    if par[x]==x:
        return 0
    else:
        return num[x] + tami(par[x])


N = I()
uvw = [IL() for i in range(N-1)]

par=[0 for i in range(N+1)]
rnk=[0 for i in range(N+1)]
num=[0 for i in range(N+1)]

init(N)

for u,v,w in uvw:
    unite(u,v,w)

ans=[0 for i in range(N+1)]
for i in range(1,N+1):
    if tami(i)==0 or tami(i)%2==0:
        ans[i]=0
    else:
        ans[i]=1

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