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: Runtime Error

Solution code:
class Node:
    # def __init__(self, val):
    def __init__(self):
        self.val = None
        self.color = None
        self.a = []

n = int(input())
nodes = []

for i in range(n):
    nodes.append(Node())
    # nodes.append(Node(i+1))

for i in range(n-1):
    u, v, w = map(int, input().split())
    node1 = nodes[u-1]
    node2 = nodes[v-1]
    node1.val = u
    node2.val = v
    node1.a.append([node2, w])
    node2.a.append([node1, w])

# for i in range(n):
#     node = nodes[i]
#     for pair in node.a:
#         print(pair[0])
#         print(pair[1])

root = nodes[0]
root.color = True
nodeSet = set()

def traverse(node, distance):
    if node in nodeSet:
        return
    else:
        nodeSet.add(node)
    # print('# traverse: ' + str(node.val))
    for pair in node.a:
        adjNode = pair[0]
        dis = pair[1]
        if (distance + dis) % 2 == 0:
            adjNode.color = root.color
        else:
            adjNode.color = not root.color
        traverse(adjNode, distance + dis)

traverse(root, 0)

for i in range(n):
    node = nodes[i]
    if node.color:
        print(1)
    else:
        print(0)

Submission status: Runtime Error

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

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

ls = []
tmp = []

for i in range(N-1):
    if w[i] % 2 == 0:
        ls.append(u[i])
        ls.append(v[i])
ls_unique = list(set(ls))
#print(ls_unique)
result = [0] * N
for i in range(N-1):
    if u[i] in ls_unique != True and w[i] % 2 != 0:
        result[v[i]] = 1
for i in range(len(ls_unique)):
    result[ls_unique.pop()-1] = 1
for i in range(N):
    print(result[i])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(50000)

n=int(input())
visitedlist=[-1 for  i in range(n)]
kilist=[[] for i in range(n)]
anslist=[0 for i in range(n)]
searchedlist=[-1 for i in range(n)]
for i in range(n-1):
        a,b,c=map(int,input().split())
        kilist[a-1].append((b,c))
        kilist[b-1].append((a,c))
def search(i):
        searchedlist[i-1]=1
        visitedlist[i-1]=1
        flag=0
        for j in kilist[i-1]:
            if visitedlist[j[0]-1]<0:
                visitedlist[j[0]-1]=1
                flag+=1
                anslist[j[0]-1]=anslist[i-1]+j[1]
        else:
            pass

        if flag>0:    
            for k in kilist[i-1]:
                if searchedlist[k[0]-1]==-1:
                    search(k[0])
search(2)
for i in anslist:
    print(i%2)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000)

n=int(input())
visitedlist=[-1 for  i in range(n)]
kilist=[[] for i in range(n)]
anslist=[0 for i in range(n)]
searchedlist=[-1 for i in range(n)]
for i in range(n-1):
        a,b,c=map(int,input().split())
        kilist[a-1].append((b,c))
        kilist[b-1].append((a,c))
def search(i):
        searchedlist[i-1]=1
        visitedlist[i-1]=1
        flag=0
        for j in kilist[i-1]:
            if visitedlist[j[0]-1]<0:
                visitedlist[j[0]-1]=1
                flag+=1
                anslist[j[0]-1]=anslist[i-1]+j[1]
        else:
            pass

        if flag>0:    
            for k in kilist[i-1]:
                if searchedlist[k[0]-1]==-1:
                    search(k[0])
search(2)
for i in anslist:
    print(i%2)

Submission status: Runtime Error

Solution code:
n=int(input())
visitedlist=[-1 for  i in range(n)]
kilist=[[] for i in range(n)]
anslist=[0 for i in range(n)]
searchedlist=[-1 for i in range(n)]

for i in range(n-1):
        a,b,c=map(int,input().split())
        kilist[a-1].append((b,c))
        kilist[b-1].append((a,c))
def search(i):
        searchedlist[i-1]=1
        visitedlist[i-1]=1
        flag=0
        for j in kilist[i-1]:
            if visitedlist[j[0]-1]<0:
                visitedlist[j[0]-1]=1
                flag+=1
                anslist[j[0]-1]=anslist[i-1]+j[1]
        else:
            pass

        if flag>0:    
            for k in kilist[i-1]:
                if searchedlist[k[0]-1]==-1:
                    search(k[0])
search(2)
for i in anslist:
	print(i%2)

Submission status: Runtime Error

Solution code:
#d-even relation
n=int(input())
visitedlist=[-1 for  i in range(n)]
kilist=[[] for i in range(n)]
anslist=[0 for i in range(n)]
searchedlist=[-1 for i in range(n)]

for i in range(n-1):
        a,b,c=map(int,input().split())
        kilist[a-1].append((b,c))
        kilist[b-1].append((a,c))
def search(i):
        searchedlist[i-1]=1
        visitedlist[i-1]=1
        flag=0
        for j in kilist[i-1]:
            if visitedlist[j[0]-1]<0:
                visitedlist[j[0]-1]=1
                flag+=1
                anslist[j[0]-1]=(anslist[i-1]+j[1])
        if flag>0:    
            for k in kilist[i-1]:
                if searchedlist[k[0]-1]==-1:

                    search(k[0])
                else:
                    return
                   
        else:
            return 

search(1)
for i in anslist:
    print(i%2)

Submission status: Runtime Error

Solution code:
#d-even relation
n=int(input())
visitedlist=[-1 for  i in range(n)]
kilist=[[] for i in range(n)]
for i in range(n-1):
    a,b,c=map(int,input().split())
    kilist[a-1].append([b,c])
    kilist[b-1].append([a,c])   
anslist=[0 for i in range(n)]
searchedlist=[0 for i in range(n)]
def search(i):
        searchedlist[i-1]=1
        visitedlist[i-1]=1
        flag=0
        for j in kilist[i-1]:
            if visitedlist[j[0]-1]<0:
                visitedlist[j[0]-1]=1
                flag+=1
                anslist[j[0]-1]=(anslist[i-1]+j[1])%2
        if flag>0:    
            for k in kilist[i-1]:
                if searchedlist[k[0]-1]==0:
                    search(k[0])
        else:
            return 

search(1)
for i in anslist:
    print(i%2)

Submission status: Runtime Error

Solution code:
#d-even relation
n=int(input())
visitedlist=[-1 for  i in range(n)]
lists=[tuple(map(int,input().split())) for i in range(n-1)]
kilist=[[] for i in range(n)]
for i in lists:
    kilist[i[0]-1].append([i[1],i[2]])
    kilist[i[1]-1].append([i[0],i[2]])   
anslist=[0 for i in range(n)]
searchedlist=[0 for i in range(n)]
def search(i):
        searchedlist[i-1]=1
        visitedlist[i-1]=1
        flag=0
        for j in kilist[i-1]:
            if visitedlist[j[0]-1]<0:
                visitedlist[j[0]-1]=1
                flag+=1
                anslist[j[0]-1]=(anslist[i-1]+j[1])%2
        if flag>0:    
            for k in kilist[i-1]:
                if searchedlist[k[0]-1]==0:
                    search(k[0])
        else:
            return 

search(1)
for i in anslist:
    print(i%2)

Submission status: Runtime Error

Solution code:
#d-even relation
n=int(input())
visitedlist=[-1 for  i in range(n)]
lists=[tuple(map(int,input().split())) for i in range(n-1)]
kilist=[[] for i in range(n)]
for i in lists:
    kilist[i[0]-1].append([i[1],i[2]])
    kilist[i[1]-1].append([i[0],i[2]])   
anslist=[0 for i in range(n)]
#開始地点1のところは0を基準としている
#1には初めに訪れる
searchedlist=[0 for i in range(n)]
def search(i):
        searchedlist[i-1]=1
        visitedlist[i-1]=1
        flag=0
        for j in kilist[i-1]:
            if visitedlist[j[0]-1]<0:
                visitedlist[j[0]-1]=1
                flag+=1
                anslist[j[0]-1]=anslist[i-1]+j[1]
        if flag>0:    
            for k in kilist[i-1]:
                if searchedlist[k[0]-1]==0:
                    search(k[0])
                else:
                    pass
        else:
            pass

search(1)
for i in anslist:
    print(i%2)

Submission status: Runtime Error

Solution code:
#d-even relation
#ふかさ優先探索の実装
#再起関数の実装を行う
n=int(input())
visitedlist=[-1 for  i in range(n)]
lists=[tuple(map(int,input().split())) for i in range(n-1)]
kilist=[[] for i in range(n)]
for i in lists:
    kilist[i[0]-1].append([i[1],i[2]])
    kilist[i[1]-1].append([i[0],i[2]])   
anslist=[0 for i in range(n)]
#開始地点1のところは0を基準としている
#1には初めに訪れる
def search(i):
        visitedlist[i-1]=1
        flag=0
        for j in kilist[i-1]:
            if visitedlist[j[0]-1]<0:
                visitedlist[j[0]-1]=1
                flag+=1
                anslist[j[0]-1]=anslist[i-1]+j[1]
        if flag>0:    
            for k in kilist[i-1]:
              search(k[0])   	
        else:
            pass

search(3)
for i in anslist:
  print(i%2)

Submission status: Runtime Error

Solution code:
#d-even relation
#ふかさ優先探索の実装
#再起関数の実装を行う
n=int(input())
visitedlist=[-1 for  i in range(n)]
lists=[tuple(map(int,input().split())) for i in range(n-1)]
kilist=[[] for i in range(n)]
for i in lists:
    kilist[i[0]-1].append(i[1:])
    kilist[i[1]-1].append(i[0:3:2])
visitedlist[0]=1
anslist=[0 for i in range(n)]
#開始地点1のところは0を基準としている
#1には初めに訪れる
def search(i):
        flag=0
        for j in kilist[i-1]:
            if visitedlist[j[0]-1]<0:
                visitedlist[j[0]-1]=1
                flag+=1
                anslist[j[0]-1]=anslist[i-1]+j[1]
        if flag>0:    
            for k in kilist[i-1]:
                return search(k[0])           
search(1)
print(anslist)

Submission status: Runtime Error

Solution code:
#d-even relation
#ふかさ優先探索の実装
#再起関数の実装を行う
n=int(input())
visitedlist=[-1 for  i in range(n)]
lists=[tuple(map(int,input().split())) for i in range(n-1)]
kilist=[[] for i in range(n)]
for i in lists:
    kilist[i[0]-1].append(i[1:])
    kilist[i[1]-1].append(i[0:3:2])
visitedlist[0]=1
anslist=[0 for i in range(n)]
#開始地点1のところは0を基準としている
#1には初めに訪れる
def search(i):
        flag=0
        for j in kilist[i-1]:
            if visitedlist[j[0]-1]<0:
                visitedlist[j[0]-1]=1
                flag+=1
                
                if j[1]%2==0:
                    anslist[j[0]-1]=anslist[i-1]
                elif j[1]%2!=0:
                    anslist[j[0]-1]=1-anslist[i-1]      
        if flag>0:    
            for j in kilist[i-1]:
                return search(j[0])
        else:
            pass
            
search(1)
for k in anslist:
    print(k)

Submission status: Runtime Error

Solution code:
n=int(input())

lists=[tuple(map(int,input().split())) for i in range(n-1)]
visitedlist=[-1 for  i in range(n)]
kilist=[[] for i in range(n)]
for i in lists:
    kilist[i[0]-1].append(i[1:])
    kilist[i[1]-1].append(i[0:3:2])
visitedlist[0]=1
anslist=[0 for i in range(n)]
#開始地点1のところは0を基準としている
#1には初めに訪れる
def search(i):
        flag=False
        for j in kilist[i-1]:
            if visitedlist[j[0]-1]<0:
                visitedlist[j[0]-1]=1
                flag=True
                if j[1]%2==0:
                    anslist[j[0]-1]=anslist[i-1]
                elif j[1]%2!=0:
                    anslist[j[0]-1]=1-anslist[i-1]      
        if flag:    
            for j in kilist[i-1]:
                return search(j[0])
            
search(1)
for k in anslist:
    print(k)

Submission status: Runtime Error

Solution code:
#d-even relation
#ふかさ優先探索の実装
#再起関数の実装を行う
n=int(input())
visitedlist=[-1 for  i in range(n)]
lists=[tuple(map(int,input().split())) for i in range(n-1)]
kilist=[[] for i in range(n)]
for i in lists:
    kilist[i[0]-1].append(i[1:])
    kilist[i[1]-1].append(i[0:3:2])
visitedlist[0]=1
anslist=[0 for i in range(n)]
#開始地点1のところは0を基準としている
#1には初めに訪れる
def search(i):
        flag=False
        for j in kilist[i-1]:
            if visitedlist[j[0]-1]<0:
                visitedlist[j[0]-1]=1
                flag=True
                if j[1]%2==0:
                    anslist[j[0]-1]=anslist[i-1]
                elif j[1]%2!=0:
                    anslist[j[0]-1]=1-anslist[i-1]      
            else:
                pass  
        if flag:    
            for j in kilist[i-1]:
                return search(j[0])
        if not flag:
            pass
            
search(1)
for k in anslist:
    print(k)

Submission status: Runtime Error

Solution code:
#d-even relation
#ふかさ優先探索の実装
#再起関数の実装を行う
visitedlist=[-1 for  i in range(n)]
n=int(input())
lists=[tuple(map(int,input().split())) for i in range(n-1)]

kilist=[[] for i in range(n)]
for i in lists:
    kilist[i[0]-1].append(i[1:])
    kilist[i[1]-1].append(i[0:3:2])

visitedlist[0]=1
anslist=[0 for i in range(n)]
#開始地点1のところは0を基準としている
#1には初めに訪れる
def search(i):
        flag=False
        for j in kilist[i-1]:
            if visitedlist[j[0]-1]<0:
                visitedlist[j[0]-1]=1
                flag=True
                if j[1]%2==0:
                    anslist[j[0]-1]=anslist[i-1]
                elif j[1]%2!=0:
                    anslist[j[0]-1]=1-anslist[i-1]      
            else:
                pass  
        if flag:    
            for j in kilist[i-1]:
                return search(j[0])
        if not flag:
            pass
            
search(1)
for k in anslist:
    print(k)

Submission status: Runtime Error

Solution code:
import sys
def dfs(n,c):
    if Ans[n] == -1:
        Ans[n] = c
        for m,w in Data[n]:
            if w % 2 == 0:
                dfs(m,c)
            else:
                dfs(m,1-c)
    elif Ans[n] == c:
        return
    else:
        return

sys.setrecursionlimit(10000)
N = int(input())
Data = []
Ans  = [-1] * N
for i in range(N):
    Data.append([])
for i in range(N-1):
    u,v,w = map(int,input().split())
    Data[u-1].append([v-1,w])
    Data[v-1].append([u-1,w])
dfs(0,0)

for a in Ans:
    print(a)

Submission status: Runtime Error

Solution code:
# -*- coding utf-8 -*-
import numpy as np

MOD = 10 ** 9 + 7

n = int(input())

# graph = [[-1] * n for _ in range(n)]
graph = np.zeros((n, n), dtype=np.int64)
graph -= 1
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    graph[u - 1][v - 1] = w
    graph[v - 1][u - 1] = w

dist = [-1] * n
dist[0] = 0
que = [0]
while len(que) != 0:
    i = que.pop()
    for j in range(n):
        if dist[j] == -1 and graph[i][j] != -1:
            dist[j] = dist[i] + graph[i][j]
            que.append(j)

for d in dist:
    print(d % 2)

Submission status: Runtime Error

Solution code:
# -*- coding utf-8 -*-
import numpy as np

MOD = 10 ** 9 + 7

n = int(input())

# graph = [[-1] * n for _ in range(n)]
graph = np.zeros((n, n), dtype=np.int)
graph -= 1
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    graph[u - 1][v - 1] = w
    graph[v - 1][u - 1] = w

dist = [-1] * n
dist[0] = 0
que = [0]
while len(que) != 0:
    i = que.pop()
    for j in range(n):
        if dist[j] == -1 and graph[i][j] != -1:
            dist[j] = dist[i] + graph[i][j]
            que.append(j)

for d in dist:
    print(d % 2)

Submission status: Runtime Error

Solution code:
def dfs(n,c):
    if Ans[n] == -1:
        Ans[n] = c
        for m,w in Data[n]:
            if w % 2 == 0:
                dfs(m,c)
            else:
                dfs(m,1-c)
    elif Ans[n] == c:
        return
    else:
        return

N = int(input())
Data = []
Ans  = [-1] * N
for i in range(N):
    Data.append([])
for i in range(N-1):
    u,v,w = map(int,input().split())
    Data[u-1].append([v-1,w])
    Data[v-1].append([u-1,w])
dfs(0,0)

for a in Ans:
    print(a)

Submission status: Runtime Error

Solution code:
bw = [0] * (n + 1)

n = int(input())
tmp = list(map(int, input().split()))
u = tmp[0]
v = tmp[1]
w = tmp[2]

bw[u] = -1
if w % 2 == 0:
  bw[v] = -1
else:
  bw[v] = 1

for i in range(1,n-1):
  tmp = list(map(int, input().split()))
  u = tmp[0]
  v = tmp[1]
  w = tmp[2]

  if w%2 == 0:
    jdg = 1
  else:
    jdg = -1

  if bw[u] != 0:
      bw[v] = bw[u] * jdg
  else:
      bw[u] = bw[v] * jdg

for i in range(1,n+1):
  if bw[i] == -1:
    print('0')
  else:
    print('1')

Submission status: Runtime Error

Solution code:
bw = [0] * (n + 1)

n = int(input())
tmp = list(map(int, input().split()))
u = tmp[0]
v = tmp[1]
w = tmp[2]

bw[u] = -1
if w % 2 == 0:
  bw[v] = -1
else:
  bw[v] = 1

for i in range(1,n-1):
  tmp = list(map(int, input().split()))
  u = tmp[0]
  v = tmp[1]
  w = tmp[2]

  if w%2 == 0:
    jdg = 1
  else:
    jdg = -1

  if bw[u] != 0:
      bw[v] = bw[u] * jdg
  else:
      bw[u] = bw[v] * jdg

for i in range(1,n+1):
  if bw[i] == -1:
    print(0)
  else:
    print(1)

Submission status: Runtime Error

Solution code:
# Union-Find木
import sys
input = sys.stdin.readline

n = int(input())
tree = [0]
for i in range(1, n+1):
    tree.append(i)


def root(x):
    if(tree[x] == x):
        return x
    tree[x] = root(tree[x])
    return tree[x]  # 経路圧縮


def same(x, y):  # xとyが同じ木に属するか
    return root(x) == root(y)


def unite(x, y):
    x = root(x)
    y = root(y)
    if(x == y):
        return
    tree[x] = y


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

for i in range(1, n+1):
    if (same(1, i)):
        print(0)
    else:
        print(1)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(20000)

N = int(input())
d = {}
e = [[] for _ in range(N)]
dd = [-1]*N
for _ in range(N-1):
    u, v, w = list(map(int, input().split()))
    e[u-1].append(v-1)
    e[v-1].append(u-1)
    d[(u-1,v-1)]= d[(v-1,u-1)] = w

def dfs(i, _d):
    dd[i] = _d
    for c in e[i]:
        if dd[c]!=-1: continue
        dfs(c, _d+d[(i,c)])

dfs(0,0)
for _dd in dd:
    print(_dd%2)

Submission status: Runtime Error

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

def dfs(i, _d):
    dd[i] = _d
    for c in e[i]:
        if dd[c]!=-1: continue
        dfs(c, _d+d[(i,c)])

dfs(0,0)
for _dd in dd:
    print(_dd%2)

Submission status: Runtime Error

Solution code:
N = int(input())
nodes = [[[], -1] for _ in range(N + 1)]

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


def f(i, c):
    if nodes[i][1] >= 0: return
    nodes[i][1] = c
    for n in nodes[i][0]:
        f(n[0], (c + (n[1] % 2)) % 2)

# print(nodes)
f(0,0)
for i in range(N):
    print(nodes[i][1])

Submission status: Runtime Error

Solution code:
import sys

n = int(input())

node_ls = {i: Node(-1, i, []) for i in range(n)}
_map = {j: [0 for i in range(n)] for j in range(n)}
already = set()
color_ls = [-1 for i in range(n)]
for i in range(n - 1):
    u, v, w = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        color_ls[u] = 0
    _map[u][v] = w
    _map[v][u] = w
    if u not in already and v in already:
        node_ls[u].parent = v
        node_ls[v].child.append(u)
        color_ls[u] = 0 if (w % 2 == 0 and color_ls[v] == 0) or (w % 2 == 1 and color_ls[v] == 1) else 1
    else:
        node_ls[v].parent = u
        node_ls[u].child.append(v)
        color_ls[v] = 0 if (w % 2 == 0 and color_ls[u] == 0) or (w % 2 == 1 and color_ls[u] == 1) else 1
    already.update({u, v})
for c in color_ls:
    print(c)

Submission status: Runtime Error

Solution code:
# D - Even Relation

# ある頂点からの距離の偶奇が一致する点を同じ色で塗る
# scipy.sparse.csgraphのdijkstraではTLEになる模様

from scipy.sparse.csgraph import dijkstra
import sys

N = int(input())

neighbor = [[0]*N for _ in range(N)]

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

dist_from_0 = dijkstra(neighbor, indices = 0)

a = 1//0

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

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
from collections import deque

n = int(input())


color_ls = {i:None for i in range(n)}
_map = np.zeros((n, n))
already = set()
for i in range(n-1):
    u, v, w = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        root = u
    _map[u][v] = w
    _map[v][u] = w
    already.update({u, v})
color_ls[root] = 0
cur = root
already.clear()
que = deque()
while True:
    candidate = np.nonzero(_map[cur])[0]
    for cand in candidate:
        if cand in already:
            continue
        already.add(cand)
        que.append(cand)
        if _map[cur][cand] % 2 == 0:
            color_ls[cand] = 0 if color_ls[cur] == 0 else 1
        else:
            color_ls[cand] = 1 if color_ls[cur] == 0 else 0
    if len(que) == 0:
        break
    cur = que.popleft()

for i in range(n):
    print(color_ls[i])

Submission status: Runtime Error

Solution code:
import sys
import queue
n = int(input())
class Node:

    def __init__(self, parent, id, child):
        self.parent = parent
        self.id = id
        self.child = child
        self.color = None

def get_root(node):
    while True:
        parent = node.parent
        if parent == -1:
            break
        node = node_ls[parent]
    return node.id

node_ls = {i:Node(-1, i, []) for i in range(n)}
_map = {j:[0 for i in range(n)] for j in range(n)}
already = set()
for i in range(n-1):
    u, v, w = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        root = u
    _map[u][v] = w
    _map[v][u] = w
    if u not in already and v in already:
        node_ls[u].parent = v
        node_ls[v].child.append(u)
    else:
        node_ls[v].parent = u
        node_ls[u].child.append(v)
    already.update({u, v})
node_ls[root].color = 0
cur = node_ls[root]
que = []
while True:
    children = cur.child
    for child in children:
        if node_ls[child].color is not None:
            continue
        queue.heappush(que, child)
        if _map[cur.id][child] % 2 == 0:
            node_ls[child].color = 1 if node_ls[cur.id].color == 1 else 0
        else:
            node_ls[child].color = 0 if node_ls[cur.id].color == 1 else 1
    if len(que) == 0:
        break
    cur = queue.heappop(que)
    cur = node_ls[cur]
res = [node.color for node in node_ls]
for r in res:
    print(r)

Submission status: Runtime Error

Solution code:
import sys
import queue
n = int(input())
class Node:

    def __init__(self, parent, id, child):
        self.parent = parent
        self.id = id
        self.child = child
        self.color = None

def get_root(node):
    while True:
        parent = node.parent
        if parent == -1:
            break
        node = node_ls[parent]
    return node.id

node_ls = [Node(-1, i, []) for i in range(n)]
_map = [[0 for i in range(n)] for j in range(n)]
already = set()
for i in range(n-1):
    u, v, w = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        root = u
    _map[u][v] = w
    _map[v][u] = w
    if u not in already and v in already:
        node_ls[u].parent = v
        node_ls[v].child.append(u)
    else:
        node_ls[v].parent = u
        node_ls[u].child.append(v)
    already.update({u, v})
node_ls[root].color = 0
cur = node_ls[root]
que = []
while True:
    children = cur.child
    for child in children:
        if node_ls[child].color is not None:
            continue
        queue.heappush(que, child)
        if _map[cur.id][child] % 2 == 0:
            node_ls[child].color = 1 if node_ls[cur.id].color == 1 else 0
        else:
            node_ls[child].color = 0 if node_ls[cur.id].color == 1 else 1
    if len(que) == 0:
        break
    cur = queue.heappop(que)
    cur = node_ls[cur]
res = [node.color for node in node_ls]
for r in res:
    print(r)

Submission status: Runtime Error

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

    color = [-1 for _ in range(N)]

    def paint(i,c):
        color[i] = c
        for n,w in n_dict[i]:
            if color[n] == -1:
                paint(n,c^1) if w % 2 != 0 else paint(n,c)
            else:
                continue
        return
    for i in range(N):
        if color[i] == -1:
            paint(i,1)   
        print(color[i])
resolve()

Submission status: Runtime Error

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

    color = [-1 for _ in range(N)]

    def paint(i,c):
        color[i] = c
        for n,w in n_dict[i]:
            if color[n] == -1:
                paint(n,c^1) if w % 2 != 0 else paint(n,c)
            else:
                return
        return
    for i in range(N):
        if color[i] == -1:
            paint(i,1)   
        print(color[i])
resolve()

Submission status: Runtime Error

Solution code:
N=int(input())
to = [[] for _ in range(N)]
for _ in range(N-1):
    u, v, w = [int(x) for x in input().split()]
    u -= 1
    v -= 1
    to[u].append([v, w])
    to[v].append([u, w])
ans = [-1] * N
ans[0] = 0
def dfs(v, p=-1, c=0):
    for u, w in to[v]:
        if u == p: continue
        if (w%2 != 0):
            c ^= 1
        ans[u] = c
        dfs(u, v, c)
dfs(0)
for v in ans:
    print(v)

Submission status: Runtime Error

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

def dfs(d,col):
  if len(d)==0:
    return col
  s=d.pop()
  for i in lst[s]:
    v,w=i[0],i[1]
    lst[v].remove([s,w])
    d.append(v)
    if w%2==0:
      if col[u]!=-1:
        col[v]=col[u]
      elif col[v]!=-1:
        col[u]=col[v]
      else:
        col[u]=0
        col[v]=0
    else:
      if col[u]!=-1:
        col[v]=(col[u]+1)%2
      elif col[v]!=-1:
        col[u]=(col[v]+1)%2
      else:
        col[u]=0
        col[v]=1
  return dfs(d,col)

d=deque([1])
col=defaultdict(int)
for i in range(1,N+1):
  if i==1:
    col[i]=0
  else:
    col[i]=-1
ans=dfs(d,col)
for i in range(1,N+1):
  print(ans[i])

Submission status: Runtime Error

Solution code:
N = int(input())
V = [[] for _ in range(N)]
V_dst = [0 for _ in range(N)]

for _ in range(max(N - 1, 1)):
	u, v, w = map(int, input().split())
	V[u - 1].append([v - 1, w])
	V[v - 1].append([u - 1, w])
	
visited = [False for _ in range(N)]
visited[0] = True
	
def dfs(cur, dst):
	for adj, length in V[cur]:
		if not visited[adj]:
			visited[adj] = True
			V_dst[adj] = dst + length
			dfs(adj, dst + length)

if N != 1:		
	dfs(0, 0)

for v in V_dst:
	if v % 2 == 0:
		print(0)
	else:
		print(1)

Submission status: Runtime Error

Solution code:
N = int(input())
V = [[] for _ in range(N)]
V_dst = [0 for _ in range(N)]

for _ in range(N - 1):
	u, v, w = map(int, input().split())
	V[u - 1].append([v - 1, w])
	V[v - 1].append([u - 1, w])
	
visited = [False for _ in range(N)]
visited[0] = True
	
def dfs(cur, dst):
	for adj, length in V[cur]:
		if not visited[adj]:
			visited[adj] = True
			V_dst[adj] = dst + length
			dfs(adj, dst + length)

if N != 1:		
	dfs(0, 0)

for v in V_dst:
	if v % 2 == 0:
		print(0)
	else:
		print(1)

Submission status: Runtime Error

Solution code:
N = int(input())
V = [[] for _ in range(N)]
V_dst = [0 for _ in range(N)]

for _ in range(N - 1):
	u, v, w = map(int, input().split())
	V[u - 1].append([v - 1, w])
	V[v - 1].append([u - 1, w])
	
visited = [False for _ in range(N)]
visited[0] = True
	
def dfs(cur, dst):
	for adj, length in V[cur]:
		if not visited[adj]:
			visited[adj] = True
			V_dst[adj] = dst + length
			dfs(adj, dst + length)
			
dfs(0, 0)

for v in V_dst:
	if v % 2 == 0:
		print(0)
	else:
		print(1)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector< vector<int> > VVI;
typedef pair<int,int> P;
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a) (a).begin(),(a).end()

#define AMAX 100010
#define DMAX 10000000000

int N;
struct edge{
  int to,cost;
};
vector<edge> G[AMAX];
ll D[AMAX];

void dijkstra(int s){
  priority_queue<P,vector<P>,greater<P>> Q;
  rep(i,N) D[i]=DMAX;
  D[s]=0;
  Q.push(P(0,s));
  while(!Q.empty()){
    P p=Q.top();
    Q.pop();
    int x=p.second;
    if(D[x]<p.first) continue;
    rep(i,G[x].size()){
      edge e=G[x][i];
      if(D[e.to]>D[x]+e.cost){
        D[e.to]=D[x]+e.cost;
        Q.push(P(D[e.to],e.to));
      }
    }
  }
}

int main(){
  cin>>N;
  VI U(N-1),V(N-1),W(N-1);
  rep(i,N-1) cin>>U[i]>>V[i]>>W[i];
  rep(i,N-1){
    edge E1={V[i]-1,W[i]};
    edge E2={U[i]-1,W[i]};
    G[U[i]-1].push_back(E1);
    G[V[i]-1].push_back(E2);
  }
  dijkstra(0);
  rep(i,N) cout<<D[i]%2<<endl;
}

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e10;

int N;
vector<vector<int>> G;
vector<vector<long long>> dist;
vector<int> color;

void dfs(int now){
    if(color[now] == 0)color[now] = 1;
    for(auto v : G[now]){
        if(color[v] != 0)continue;
        color[v] = color[now];
        if(dist[now][v] % 2 == 1){
            color[v] *= -1;
        }
        dfs(v);
    }
    return;
}

int main(){
    cin >> N;
    G.assign(N, vector<int>());
    dist.assign(N, vector<long long>(N, INF));
    color.assign(N, 0);
    for(int i = 1; i < N; i++){
        int a, b;
        long long w;
        cin >> a >> b >> w;
        a--, b--;
        G[a].push_back(b);
        G[b].push_back(a);
        dist[a][b] = w;
        dist[b][a] = w;
    }
    
    dfs(0);
    
    for(int i = 0; i < N; i++){
        if(color[i] < 0)cout << 0 << endl;
        else cout << 1 << endl;
    }
    return 0;
}

Submission status: Runtime Error

Solution code:
# 2019/10/10
# 解答かんにんぐ
# 前回WAの原因は、u,vのいずれかが訪問済みである必要があった。
# edge参照順序によっては、u,vのいずれも未訪問となり、このときWAとなる。

n=int(input())
node=[None]*n
edge=[[] for _ in range(n)]


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


def dfs(v,p,c):
    node[v]=c
    for vv,w in edge[v]:
        if vv==p:continue
        dfs(vv,v,1-c) if w%2 else dfs(vv,v,c)


dfs(0,-1,0)

for e in node:
    print(e)