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:
import sys
input = sys.stdin.readline

import math

def main(): 

    N=int(input())
    to=[[] for _ in range(N+1)]
    ans=[0]*(N+1)

    for i in range(N-1):
        u,v,w=map(int,input().split())
        print(u,v,w)
        to[u].append([v,w])
        to[v].append([u,w])
    
    def dfs(chil, par):
        for v,l in to[chil]:
            if v == par: continue
            if (ans[chil] == 1 and l%2==0) or (ans[chil] == 0 and l%2==1):
                ans[v]=1
            dfs(v,chil)
    
    ans[1] = 1
    dfs(1,-1)
    print(*ans[1:])
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
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))

#print(edge)

def dfs(u,v):
  for e,w in edge[u]:
    if e != v:
      ans[e] = (w + ans[u]) % 2
      # print(ans)
      dfs(e,u)

ans = [0] + [-1] * (N-1)
dfs(0,-1)
print(*ans)

Submission status: Runtime Error

Solution code:
def survey(now, mlist, clist):
    for ml in mlist[now]:
        if clist[ml[0]]==2:
            clist[ml[0]]=(clist[now]+ml[1])%2
            _, _=survey(ml[0], mlist, clist)
    return mlist, clist

n=int(input())
mlist=[]
clist=[]
for i in range(n):
    mlist.append([])
    clist.append(2)

for i in range(n-1):
    u, v, m = map(int, input().split())
    u-=1
    v-=1
    mlist[u].append([v, m%2])
    mlist[v].append([u, m%2])

#print(mlist)
clist[0]=0
now=0
mlist, clist = survey(now, mlist, clist)
for cl in clist:
    print(cl)

Submission status: Runtime Error

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

def dfs(v, cur):
  color[v] = cur
  for next_v, w in G[v]:
    if color[next_v] != -1:
      continue
    if w % 2 == 0:
      dfs(next_v, cur)
    else:
      dfs(next_v, 1 - cur)
      
dfs(0, 0)
for i in range(N):
  print(color[i])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000)

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

def dfs(v, cur):
  color[v] = cur
  for next_v, w in zip(G[v], W[v]):
    if color[next_v] != -1:
      continue
    if w % 2 == 0:
      dfs(next_v, cur)
    else:
      dfs(next_v, 1 - cur)
      
for i in range(N):
  if color[i] != -1:
    continue
  dfs(i, 0)
for i in range(N):
  print(color[i])

Submission status: Runtime Error

Solution code:
N = int(input())
color = [-1] * N
G = [[] for _ in range(N)]
W = [[] for _ in range(N)]
for i in range(N-1):
  u, v, w = map(int, input().split())
  G[u-1].append(v-1)
  G[v-1].append(u-1)
  W[u-1].append(w)
  W[v-1].append(w)

def dfs(v, cur):
  color[v] = cur
  for next_v, w in zip(G[v], W[v]):
    if color[next_v] != -1:
      continue
    if w % 2 == 0:
      dfs(next_v, cur)
    else:
      dfs(next_v, 1 - cur)
      
for i in range(N):
  if color[i] != -1:
    continue
  dfs(i, 0)
for i in range(N):
  print(color[i])

Submission status: Runtime Error

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

neighbor = [[] for _ in range(n+1)]
for edge in lst:
    neighbor[edge[0]].append((edge[1], edge[2]))
    neighbor[edge[1]].append((edge[0], edge[2]))

    
color = [-1] * (n+1)

#startをbwで塗る
def dfs(start, bw):
    color[start] = bw
    for pair in neighbor[start]:
        if color[pair[0]] != -1:
            continue
        elif pair[1] % 2 == 0:
            dfs(pair[0], bw)
        else:
            dfs(pair[0], (bw + 1) % 2)

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

Submission status: Runtime Error

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

neighbor = [[] for _ in range(n+1)]
for edge in lst:
    neighbor[edge[0]].append((edge[1], edge[2]))
    neighbor[edge[1]].append((edge[0], edge[2]))

    
color = [-1] * (n+1)

#startをbwで塗る
def dfs(start, bw):
    color[start] = bw
    for pair in neighbor[start]:
        if color[pair[0]] != -1:
            continue
        elif pair[1] % 2 == 0:
            dfs(pair[0], bw)
        else:
            dfs(pair[0], (bw + 1) % 2)

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

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
# https://atcoder.jp/contests/abc126/tasks/abc126_d

import sys

def dfs(i):
  for v,w in V.get(i, []):
    if (w % 2) == 0:
      if color[v] == -1:
        color[v] = color[i]
      elif color[v] == color[i]:
        continue
      elif color[v] != color[i]:
        return False
    else:
      if color[v] == -1:
        color[v] = 0 if color[i] else 1
      elif color[v] == color[i]:
        return False
      elif color[v] != color[i]:
        continue
    dfs(v)
  return True

if __name__ == '__main__':
  n = int(input())
  V = {} # key:vertex, value:[[vertex opposited, wight]]
  color = {} # 0 or 1 or -1
  for i in range(1, n):
    u,v,w = map(int, input().split())
    if not V.get(u, False):
      V[u] = [[v,w]]
    else:
      V[u].append([v,w])

    if not V.get(v, False):
      V[v] = [[u,w]]
    else:
      V[v].append([u,w])
    # 同じ頂点間で複数の辺はないと仮定
  for i in range(1, n+1):
    color[i] = -1

  while True:
    i = 0
    for j in range(1, n+1):
      if color[j] == -1:
        i = j
        color[i] = 0
        break
    if i == 0:
      for j in range(1, n+1):
        print(color[j])
      break
    else:
      if not dfs(i):
        print('No')
        break

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
# https://atcoder.jp/contests/abc126/tasks/abc126_d

import sys

def dfs(i):
  for v,w in V.get(i, []):
    if (w % 2) == 0:
      if color[v] == -1:
        color[v] = color[i]
      elif color[v] == color[i]:
        continue
      elif color[v] != color[i]:
        return False
    else:
      if color[v] == -1:
        color[v] = 0 if color[i] else 1
      elif color[v] == color[i]:
        return False
      elif color[v] != color[i]:
        continue
    dfs(v)
  return True

if __name__ == '__main__':
  n = int(input())
  V = {} # key:vertex, value:[[vertex opposited, wight]]
  color = {} # 0 or 1 or -1
  for i in range(1, n):
    u,v,w = map(int, input().split())
    if not V.get(u, False):
      V[u] = [[v,w]]
    else:
      V[u].append([v,w])

    if not V.get(v, False):
      V[v] = [[u,w]]
    else:
      V[v].append([u,w])
    # 同じ頂点間で複数の辺はないと仮定
  for i in range(1, n+1):
    color[i] = -1

  i = 1
  color[i] = 0
  while dfs(i):
    for j in range(1, n+1):
      if color[j] == -1:
        i = j
        color[j] = 0
        break
    for j in range(1, n+1):
      print(color[j])
    sys.exit()
  print('No')

Submission status: Runtime Error

Solution code:
n=int(input())

#ノードクラスの定義
class Node:
    def __init__(self,name):
        self.name=name
        self.near=[]
        self.length=[]
    def coloring(self,color):
        self.color=color


#ノードリストの作成
nodelist=[]
for i in range(n):
    nodelist.append(Node(i))
   
    
#辺の作成
for i in range(n-1):
    u,v,w=[int(i) for i in input().split()]
    w=w%2
    nodelist[u-1].near.append(nodelist[v-1])
    nodelist[u-1].length.append(w)
    nodelist[v-1].near.append(nodelist[u-1])
    nodelist[v-1].length.append(w)
    
#行ったかカラーでチェックリスト
color=[None for i in range(n)]
color[0]=0



def paint(node):
    nodecolor=node.color
    for j in range(len(node.near)):
        nearnode=node.near[j]
        if color[nearnode.name]!=None:
            continue
        if node.length[j]==0:
            nearnode.coloring(nodecolor)
        else:
            nearnode.coloring((nodecolor+1)%2)
        color[nearnode.name]=0
        paint(nearnode)

nodelist[0].coloring(0)
paint(nodelist[0])


for i in nodelist:
    print(i.color)

Submission status: Runtime Error

Solution code:
N = int(input())
import sys
sys.setrecursionlimit(10000)

graph = [[] for _ in range(N)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    graph[a-1].append((b-1, c))
    graph[b-1].append((a-1, c))

color = [-1 for _ in range(N)]
def dfs(p, c, color):
    color[p] = c
    cinv = 0 if c == 1 else 1
    for q, d in graph[p]:
        if color[q] != -1:
            continue
        if d % 2 == 1:
            color = dfs(q, cinv, color)
        else:
            color = dfs(q, c, color)
    return color

A = dfs(0, 0, color)
for a in A:
    print(a)

Submission status: Runtime Error

Solution code:
N = int(input())

graph = [[] for _ in range(N)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    graph[a-1].append((b-1, c))
    graph[b-1].append((a-1, c))


color = [-1 for _ in range(N)]
def dfs(p, c, color):
    color[p] = c
    cinv = 0 if c == 1 else 1
    for q, d in graph[p]:
        if color[q] != -1:
            continue
        if d % 2 == 1:
            color = dfs(q, cinv, color)
        else:
            color = dfs(q, c, color)
    return color
A = dfs(0, 0, color)
for a in A:
    print(a)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

n = int(input())
seen = [-1]*(n+1)
path = defaultdict(dict)

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


def dfs(seen, pos, color):
    if seen[pos] != -1:
        return
    else:
        seen[pos] = color
        for nx in path[pos].keys():
            check = path[pos][nx]
            if check % 2 == 1:
                color = 1 - color
            dfs(seen, nx, color)


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

Submission status: Runtime Error

Solution code:
from collections import defaultdict

n = int(input())
seen = []
path = defaultdict(dict)

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


def dfs(seen, pos, color):
    if seen[pos] == color:
        return
    elif seen[pos] == 1 - color:
        return False
    else:
        seen[pos] = color
        for nx in path[pos].keys():
            if path[pos][nx] % 2 == 1:
                color = 1 - color
            dfs(seen, nx, color)


for start in range(1, n + 1):
    seen = [-1] * (n+1)
    if dfs(seen, start, 0) is None:
        break

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

Submission status: Runtime Error

Solution code:

import collections
import itertools

def getint(): return int(input())
def getints(): return list(map(int, input().split()))

##* graph
##pre graph
m,lst,nxt,to=0,[],[],[]

def init_graph(node_count, edge_count):
    global lst,nxt,to
    lst = [-1 for _ in range(node_count)]
    nxt = [0  for _ in range(edge_count)]
    to  = [0  for _ in range(edge_count)]

# bidirection
def add_edge(u, v):
    global m
    nxt[m], lst[u], to[m], m = lst[u], m, v, m + 1
    nxt[m], lst[v], to[m], m = lst[v], m, u, m + 1

# bidirection weight
def add_edge(u, v, w):
    global m
    nxt[m], lst[u], to[m], m = lst[u], m, (v, w), m + 1
    nxt[m], lst[v], to[m], m = lst[v], m, (u, w), m + 1

def traverse(u):
    m = lst[u]
    while m != -1:
        yield m
        m = nxt[m]


n = getint()
init_graph(n, 2 * n)

for _ in range(n - 1):
    u, v, w = getints()
    add_edge(u - 1, v - 1, w)

def get_distance(u, dist, d):
    dist[u] = d
    for m in traverse(u):
        v, w = to[m]
        if dist[v] >= 0:
            continue
        get_distance(v, dist, d + w)

dist = [-1]*n
get_distance(0, dist, 0)


for i in range(n):
    print(dist[i] % 2)

Submission status: Runtime Error

Solution code:
N = int(input().strip())
colours = []
for i in range(N):
    colours.append(0)
uvw = []
uvw.sort(key=lambda x: x[0])
for i in range(N-1):
    uvw.append(list(map(int, input().strip().split())))
for branch in uvw:
    A = colours[branch[0]]
    B = colours[branch[1]]
    C = colours[branch[2]]
    if A == B:
        if branch[2] % 2 == 1:
            if B == 1:
                B = 0
            if B == 0:
                B = 1
        else:
            pass
    else:
        pass

for i in colours:
    print(i)

Submission status: Runtime Error

Solution code:
n = int(input())
uvw = []
for i in range(n-1):
    uvw.append(list(map(int, input().split(' '))))
result = [0]*n
for i in range(n-1):
    if uvw[i][2]%2 == 0:
        result[uvw[i][0]-1] = 1
        result[uvw[i][1]-1] = 1
if sum(result) == n:
    result[uvw[0][1]-1] = 0
    result[uvw[0][2]-1] = 0
for i in result:
    print(i)

Submission status: Runtime Error

Solution code:
n = int(input())
uvw = []
for i in range(n-1):
    uvw.append(list(map(int, input().split(' '))))
result = [0]*n
for i in range(n-1):
    if uvw[i][2]%2 == 0:
        result[uvw[i][0]] = 1
        result[uvw[i][1]] = 1
for i in result:
    print(i)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(200_000)

n = int(input())
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))


visited = [0] * n
ans = [0] * n
def dfs(u, d):
    for i in range(len(edge[u])):
        v, w = edge[u][i]
        if visited[v] == 0:
            ans[v] = (d + w) % 2
            visited[v] = 1
            dfs(v, d + w)


visited[0] = 1
dfs(0, 0)
for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1_000_000)

n = int(input())
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))


visited = [0] * n
ans = [0] * n
def dfs(u, d):
    for i in range(len(edge[u])):
        v, w = edge[u][i]
        if visited[v] == 0:
            ans[v] = (d + w) % 2
            visited[v] = 1
            dfs(v, d + w)


visited[0] = 1
dfs(0, 0)
for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:
n = int(input())
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))


visited = [0] * n
ans = [0] * n
def dfs(u, d):
    for i in range(len(edge[u])):
        v, w = edge[u][i]
        if visited[v] == 0:
            ans[v] = (d + w) % 2
            visited[v] = 1
            dfs(v, d + w)


visited[0] = 1
dfs(0, 0)
for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:
class Edge:
  def __init__(self, to, length):
    self.to = to
    self.length = length

class Node:
  def __init__(self, ID):
    self.id = ID
    self.checked = False
    self.children = []
    self.color = -1
    self.edges = []

N = int(input())
v1 = [0] * N
v2 = [0] * N
length = [0] * N
nodes = [ Node(i) for i in range(N) ]
for i in range(N-1):
  v1, v2, length = list(map(int, input().split(' ')))
  v1 = nodes[v1-1]
  v2 = nodes[v2-1]
  v1.children.append(v2)
  v1.edges.append(Edge(v2, length))
  v2.children.append(v1)
  v2.edges.append(Edge(v1, length))

def main(node):
  for edge in node.edges:
    to = edge.to
    if to.color == -1:
      if edge.length % 2 == 0:
        to.color = node.color
      else:
        to.color = 1 - node.color


def dfs(node, func, nodes):
  func(node)
  node.checked = True
  for child in node.children:
    if child.checked: continue
    dfs(child, func, nodes)

root = nodes[0]
root.color = 0
dfs(root, main, nodes)

for node in nodes:
  print(node.color)

Submission status: Runtime Error

Solution code:
n = int(input())
graph = [[] for _ in range(n)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    if w % 2 == 0:
        graph[u].append((v, 1))
        graph[v].append((u, 1))
    else:
        graph[u].append((v, -1))
        graph[v].append((u, -1))
            
color = [None for i in range(n)]
def set_color(v, c):
    color[v] = c
    for next_v, d in graph[v]:
        if color[next_v] is None:
            set_color(next_v, d * c)

set_color(0, 1)
for c in color:
    if c != 1:
        print(0)
    else:
        print(1)

Submission status: Runtime Error

Solution code:
N = int(input())
uvw = [list(map(int,input().split())) for _ in range(N-1)]
#testcase = open('testcase_04','r')
#contents = testcase.read()
#N = int(contents[0][0])
#uvw = [[int(j) for j in contents[i]]for i in range(1,N)]


ans = [0] * N
adj_list = [[] for _ in range(N+1)]

for u,v,w in uvw:
    adj_list[u-1].append([v-1,w])
    adj_list[v-1].append([u-1,w])
    
memo = [True] * N
def dfs(s,cnt): 
    memo[s] = False
    if cnt % 2 == 1:
        ans[s] = 1
    nxt = adj_list[s]
    for v,w in nxt:
        if memo[v]:
            dfs(v,cnt+w)
    return
    
dfs(0,0)
for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:

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

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

for u,v,w in uvw:
    adj_list[u-1].append([v-1,w])
    adj_list[v-1].append([u-1,w])
    
memo = [True] * N
def dfs(s,cnt): 
    memo[s] = False
    if cnt % 2 == 1:
        ans[s] = 1
    nxt = adj_list[s]
    for v,w in nxt:
        if memo[v]:
            dfs(v,cnt+w)
    return
    
dfs(0,0)
for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:
N = int(input())
G = [[] for i in range(N)]

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

color = [None for i in range(N)]


def set_color(v, c):
    color[v] = c
    for next_v, d in G[v]:
        if color[next_v] is None:
            set_color(next_v, d*c)


set_color(0, 1)

for c in color:
    if c == 1:
        print(1)
    else:
        print(0)

Submission status: Runtime Error

Solution code:
N = int(input())
G = [[] * N for i in range(N)]

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

color = [None for i in range(N)]


def set_color(v, c):
    color[v] = c
    for next_v, d in G[v]:
        if color[next_v] is None:
            set_color(next_v, d*c)


set_color(0, 1)

for c in color:
    if c == 1:
        print(1)
    else:
        print(0)

Submission status: Runtime Error

Solution code:
from scipy.sparse.csgraph import csgraph_from_dense, dijkstra
import numpy as np

N = int(input())

G = np.full((N, N), 10**12)

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

    # G2 = csgraph_from_dense(G, null_value=10**12)
    # d = dijkstra(G2, indices=0)

    # for dist in d:
    #     if dist % 2 == 0:
    #         print(1)
    #     else:
    #         print(0)

Submission status: Runtime Error

Solution code:
from scipy.sparse.csgraph import csgraph_from_dense, dijkstra
import numpy as np

N = int(input())

G = np.full((N, N), 10**12)

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

    # G2 = csgraph_from_dense(G, null_value=10**12)
    # d = dijkstra(G2, indices=0)

    for dist in d:
        if dist % 2 == 0:
            print(1)
        else:
            print(0)

Submission status: Runtime Error

Solution code:
from scipy.sparse.csgraph import csgraph_from_dense, dijkstra
import numpy as np

N = int(input())

G = np.full((N, N), 10**12)

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

    G2 = csgraph_from_dense(G, null_value=10**12)
    d = dijkstra(G2, indices=0)

    # for dist in d:
    #     if dist % 2 == 0:
    #         print(1)
    #     else:
    #         print(0)

Submission status: Runtime Error

Solution code:
from scipy.sparse.csgraph import csgraph_from_dense, dijkstra
import numpy as np

N = int(input())

G = np.full((N, N), 10**12)

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

    G2 = csgraph_from_dense(G, null_value=10**12)
    d = dijkstra(G2, indices=0)

    for dist in d:
        if dist % 2 == 0:
            print(1)
        else:
            print(0)

Submission status: Runtime Error

Solution code:
from scipy.sparse.csgraph import csgraph_from_dense, dijkstra
import numpy as np

N = int(input())

G = np.full((N, N), 10**12)


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

G2 = csgraph_from_dense(G, null_value=10**12)
d = dijkstra(G2, indices=0)

for dist in d:
    if dist % 2 == 0:
        print(1)
    else:
        print(0)

Submission status: Runtime Error

Solution code:
from scipy.sparse.csgraph import csgraph_from_dense
from scipy.sparse.csgraph import dijkstra
import numpy as np

N = int(input())

G = np.full((N, N), 10**12)


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

G2 = csgraph_from_dense(G, null_value=10**12)
d = dijkstra(G, indices=0)

for dist in d:
    if dist % 2 == 0:
        print(1)
    else:
        print(0)

Submission status: Runtime Error

Solution code:
N = int(input())
nodes = [set() for _ in range(N)]
for line in sys.stdin:
    u, v, w = map(int, line.split())
    u, v = (u - 1, v - 1)
    nodes[u].add((v, w))
    nodes[v].add((u, w))


ans = [-1 for _ in range(N)]
q = [(0, 0, -1)]

while q:
    v, dist, parent = q.pop()

    if dist % 2 == 0:
        ans[v] = 0
    else:
        ans[v] = 1

    for u, w in nodes[v]:
        if u == parent:
            continue

        q.append((u, dist + w, v))

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

Submission status: Runtime Error

Solution code:
N = int(input())
a = [[] for _ in range(N)]
b = [[] for _ in range(N)]
for i in range(N-1):
    x, y, z = map(int, input().split())
    x, y, z = x-1, y-1, z % 2
    a[x].append([y, z])
    b[y].append([x, z])
dist = [0] * N
visited = [0] * N
print("")

def dfs(v, prev, dist, predist, visited):
    if visited[v]:
        return dist
    dist[v] = dist[prev] ^ predist
    visited[v] = 1
    for i in a[v]:
        dist = dfs(i[0], v, dist, i[1], visited)
    for i in b[v]:
        dist = dfs(i[0], v, dist, i[1], visited)
    return dist

dist = dfs(0, 0, dist, 0, visited)
for i in dist:
    print(i)

Submission status: Runtime Error

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


sys.setrecursionlimit(10000)

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

d = defaultdict(lambda: {})
for u, v, w in uvw:
    d[u][v] = w
    d[v][u] = w


dist = {1: 0}


def calc_dist(i, up):
    if not(i in dist):
        dist[i] = dist[up]+d[up][i]
        [calc_dist(k, i) for k in list(d[i].keys())]


[calc_dist(k, 1) for k in list(d[1].keys())]

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

Submission status: Runtime Error

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

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

d = defaultdict(lambda: {})
for u, v, w in uvw:
    d[u][v] = w
    d[v][u] = w


dist = {1: 0}


def calc_dist(i, up):
    if not(i in dist):
        dist[i] = dist[up]+d[up][i]
        [calc_dist(k, i) for k in list(d[i].keys())]


[calc_dist(k, 1) for k in list(d[1].keys())]

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

Submission status: Runtime Error

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

color = [-1]*N

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

d = defaultdict(lambda: [])
for u, v, w in uvw:
    if w % 2 == 1:
        d[u].append(v)
        d[v].append(u)


def set_color(color, i, up=None):
    if color[i-1] == -1:
        if up is None:
            color[i-1] = 0
        else:
            color[i-1] = int(not color[up-1])
        for child in d[i]:
            color = set_color(color, child, i)
    return color


for k in d:
    color = set_color(color, k)


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

Submission status: Runtime Error

Solution code:
N = int(input())

color = [0]*N

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

for u, v, w in uvw:
    if w % 2 == 1:
        color[v-1] = int(not color[u-1])

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

Submission status: Runtime Error

Solution code:
import array

N = int(input())

EVEN = {}
ODD = {}
COLOR = array.array('i', [-1]*(N+1))

for i in range(1,N+1):
    EVEN[i] = []
    ODD[i] = []

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

print(EVEN)
print(ODD)

def determine_color(i):
    for j in EVEN[i]:
        if COLOR[j] == 0:
            return 0
        elif COLOR[j] == 1:
            return 1
    for j in ODD[i]:
        if COLOR[j] == 0:
            return 1
        elif COLOR[j] == 1:
            return 0

def reverse_color(color):
    if color == 0:
        return 1
    if color == 1:
        return 0
    
COLOR[1] = 0

for i in range(1,N+1):
    if COLOR[i] == -1:
        color = determine_color(i)
        COLOR[i] = color
    else:
        color = COLOR[i]

    for j in EVEN[i]:
        COLOR[j] = color
    for j in ODD[i]:
        COLOR[j] = reverse_color(color)


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

Submission status: Runtime Error

Solution code:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
  return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
  read_all = [tuple(map(int, input().split())) for _ in range(N)]
  return map(list,zip(*read_all))

#################

N = I()
u,v,w = Line(N-1)

c = [[float('inf')]*N for _ in range(N)]
for i in range(N-1):
  c[i][i] = 0
  c[u[i]-1][v[i]-1] = w[i]
  c[v[i]-1][u[i]-1] = w[i]

for k in range(N):
  for i in range(N):
    for j in range(N):
      c[i][j]=min(c[i][j],c[i][k]+c[k][j])

from scipy.sparse.csgraph import floyd_warshall
cost=floyd_warshall(c)
print(cost)

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

Submission status: Runtime Error

Solution code:
from collections import deque

N = int(input())

q = {}

for i in range(N):
    q[i] = []

for _ in range(N - 1):
    _u, _v, _w = map(int, input().split())

    q[_u - 1].append((_v - 1, _w))
    q[_v - 1].append((_u - 1, _w))

ans = [None] * N
ans[0] = 0


def search(x, parent_num):
    for s in q[x]:
        if ans[s[0]] is None:
            ans[s[0]] = (parent_num + s[1]) % 2
            search(s[0], ans[s[0]])


search(0, 0)

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

Submission status: Runtime Error

Solution code:
from collections import deque

N = int(input())

q = {}

for i in range(1, N + 1):
    q[i] = []

for _ in range(N - 1):
    _u, _v, _w = map(int, input().split())

    q[_u].append((_v, _w))
    q[_v].append((_u, _w))

s = 0
ans = [None] * N
ans[s] = 0


def search(x, _ans):
    for s in q[x]:
        if ans[s[0] - 1] is None:
            ans[s[0] - 1] = (_ans + s[1]) % 2
            search(s[0], ans[x - 1])


search(1, 0)

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

Submission status: Runtime Error

Solution code:
from sys import exit

N = int(input())

q = {}

for i in range(1, N + 1):
    q[i] = []

for _ in range(N - 1):
    _u, _v, _w = map(int, input().split())

    q[_u].append((_v, _w))
    q[_v].append((_u, _w))

reached = []
distances = {}


def search(x, distance):
    # 達したことを記録
    reached.append(x)
    distances[x] = distance

    for s in q[x]:
        if s[0] not in reached:
            search(s[0], distance + s[1])


search(1, 0)

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

Submission status: Runtime Error

Solution code:
from sys import exit

N = int(input())

q = {}

for i in range(N):
    q[i + 1] = []

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

    q[_u].append((_v, _w))
    q[_v].append((_u, _w))

reached = []
distances = {}


def search(x, distance):
    if x in reached:
        # 既に達しているところだったら
        return

    # 達したことを記録
    reached.append(x)
    distances[x] = distance

    for s in q[x]:
        search(s[0], distance + s[1])


search(1, 0)

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

Submission status: Runtime Error

Solution code:
from heapq import *
import numpy as np

def dk(start,node,edge):
  d = [0] * node
  d[start] = 0
  q = [(0, start)]
  
  while(len(q) != 0):
    ci, i = heappop(q)
    for j in edge[i]:
      nc = ci + cost[i][j]
      d[j] = int(nc%2)
      heappush(q,(nc, j))
  return d

#入力
N = int(input())
#エッジ
E = [[] for _ in [0]*(N)]
cost = np.zeros((N, N))

for i in range(N-1):
  a,b,w = (int(x) for x in input().split())
  E[a-1].append(b-1)
  cost[a-1][b-1] = w
  cost[b-1][a-1] = w


d = dk(0, N, E)

#出力
print(*d, sep = "\n")

Submission status: Runtime Error

Solution code:
from heapq import *
import numpy as np
import sys
sys.setrecursionlimit(200000)

def dk(start,node,edge):
  d = [0]*node
  d[start] = 0
  q = [(0, start)]
  
  while(len(q) != 0):
    ci, i = heappop(q)
    for j in edge[i]:
      nc = ci + cost[i][j]
      if nc%2 == 1:
        d[j] = 1
      heappush(q,(nc, j))
  return d

#入力
N = int(input())
#エッジ
E = [[] for _ in [0]*(N+1000)]
cost = np.zeros((N+1000, N+1000))

for i in range(N-1):
  a,b,w = (int(x) for x in input().split())
  E[a-1].append(b-1)
  cost[a-1][b-1] = w
  cost[b-1][a-1] = w


d = dk(0, N, E)

#出力
print(*d, sep = "\n")