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:
n = int(input())
tree = [[] for _ in range(n)]
color = [0]*n
for _ in range(n-1):
  a,b,c = map(int,input().split())
  a -= 1
  b -= 1
  tree[a].append((b,c))
  tree[b].append((a,c))
  
  
def dfs(i,before,cost):
  if cost%2 == 1:
    color[i] = 1
  for j in tree[i]:
    if j[0] == before:
      continue
    dfs(j[0],i,cost + j[1])
    
dfs(0,-1,0)

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

Submission status: Runtime Error

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

visited = [False for _ in range(n)]
visited[0] = True
ans = [0 for _ in range(n)]

def journey(next, bit):
    for pos in next:
        v, w = pos
        if visited[v]: continue
        visited[v] = True
        #print(pos, bit, bit+w)
        if (bit+w) % 2 == 0: ans[v] = 0
        else: ans[v] = 1
            
        journey(tree[v], bit+w)
    
journey(tree[0], 0)
[print(x) for x in ans]

Submission status: Runtime Error

Solution code:
n = int(input())
# 最終的に出力する色
colors = [-1] * n
# 重み付き無向グラフ
conns = [set() for _ in [0] * n] 

for x in range(1, n):
  v1, v2, dis = map(int, input().split())
  # (接続ノード, 重み) を加える
  conns[v1-1].add((v2-1, dis))
  conns[v2-1].add((v1-1, dis))
  
def dfs(now: int, prev: int,  color: int):
  # 頂点に色を塗る
  colors[now] = color
  # 頂点から生える各エッジに対し
  for edge, w in conns[now]:
    # 親と一緒 -> 無視
    if edge == prev:
      continue
    # 偶数なら同色,奇数なら異色で塗る
    dfs(edge, now, 1-color) if w&1 else dfs(edge, now, color)

# 適当に根ノードを決め,グラフ彩色開始
dfs(0, -1, 1)

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

Submission status: Runtime Error

Solution code:
from collections import deque
n = int(input())
# 最終的に出力する色
colors = [-1] * n
# 重み付き無向グラフ
conns = [set() for _ in [0] * n] 

for x in range(1, n):
  v1, v2, dis = map(int, input().split())
  # (接続ノード, 重み) を加える
  conns[v1-1].add((v2-1, dis))
  conns[v2-1].add((v1-1, dis))

# BFSの為に根ノードを初期化. (基準ノード, 距離)
q = deque([(0, 0)])
# BFS開始
while q:
  v, dis, past = q.popleft()
  
  # 奇数なら1で,偶数なら0で塗る
  colors[v] = dis&1
  
  # 基準ノードから生える各エッジに対し
  for edge, w in conns[v]:
    
    # 根からここまでの距離を足しつつ,新しく探索先を追加.
    q.append((edge, dis + w))

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

Submission status: Runtime Error

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

visited = [False] * N
color = [0] * N

def dfs(u, c):
    visited[u] = True   #; print('u, c', u, c)
    for vw in M[u]:
        v, w = vw       #; print(vw)
        if visited[v] == False:
            if w%2 == 0:
                color[v] = c * 0b1
            else:
                color[v] = c ^ 0b1
            #print('v, c', v, c)
            dfs(v, color[v])

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

Submission status: Runtime Error

Solution code:
N = int(input())
UVW = []
for n in range(N - 1):
    uvw = [int(s) for s in input().split(' ')]
    UVW.append(uvw)
UVW.sort()
c = [-1] * (N + 1)
u1, _, _ = UVW[0]
c[u1] = 0

while UVW:
    skip_UVW = []
    for u, v, w in UVW:
        if c[u] == -1 and c[v] == -1:
            skip_UVW.append((u, v, w))
            continue
        if w % 2 == 0:
            if c[u] != -1:
                c[v] = c[u]
            else:
                c[u] = c[v]
        else:
            if c[u] == 0:
                c[v] = 1
            elif c[u] == 1
                c[v] = 0
            elif c[v] == 0:
                c[u] = 1
            elif c[v] == 1:
                c[u] = 0
    UVW = skip_UVW

for ret in c[1:]:
    print(ret)

Submission status: Runtime Error

Solution code:
import os, sys, re, math, queue

N = int(input())
colors = [-1 for _ in range(N+1)]
nodes = {}

for _ in range(N-1):
	u,v,w = list(map(int,input().split(' ')))
	if u in nodes:
		nodes[u].append((v,w % 2))
	else:
		nodes[u] = [(v,w % 2)]

	if v in nodes:		
		nodes[v].append((u,w % 2))
	else:
		nodes[v] = [(u,w % 2)]

q = queue.Queue()
fst = list(nodes.keys())[0]
q.put((fst,0))

while not q.empty():
	pos, color = q.get()
	colors[pos] = color
	for cand in nodes[pos]:
		if colors[cand[0]] == -1:
			q.put((cand[0], (color+cand[1]) % 2)

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

Submission status: Runtime Error

Solution code:

def main():
    u = []
    v = []
    w = []
    N = int(input())

    path_dict = {}
    length_dict = {}

    def check_child(connected, path_list ,parent):
        for i in range(len(connected)):
            key = "{}".format(connected[i])
            if key not in out_dict.keys():
                out_dict[key] =  1-out_dict[parent] if path_list[i] ==1 else out_dict[parent]
            else:
                continue
            if key not in path_dict.keys():
                continue
            connected2 = path_dict[key]
            path_list2 = length_dict[key]
            check_child(connected2,path_list2, key)
        return 




    for i in range(N-1):
        a,b,c = map(int, input().split())
        u.append(a)
        v.append(b)
        w.append(c%2)

        d = str(a)
        e = str(b)

        if d in path_dict.keys():
            path_dict[d].append(e)
            length_dict[d].append(c)
        else:
            path_dict[d] = [e]
            length_dict[d] = [c]
 
        if e in path_dict.keys():
            path_dict[e].append(d)
            length_dict[e].append(c)
        else:
            path_dict[e] = [d]
            length_dict[e] = [c]
 

    out_dict = {}

    for i in range(N):
        key = "{}".format(i+1)
        if key not in path_dict.keys():
            continue
        if i==0:
            out_dict[key] = 0

        connected = path_dict[key] 
        path_list =  length_dict[key]
        check_child(connected, path_list, key)
    
#    print(path_dict,length_dict)
#    print(out_dict)

    for i in range(N):
        key = "{}".format(i+1)
        print(out_dict[key])


main()

Submission status: Runtime Error

Solution code:
import math
N = int(input())
edges = [list(map(int,input().split())) for _ in range(N-1)]
A = [[] for _ in range(N+1)]

for edge in edges:
  A[edge[0]].append([edge[1],edge[2]])
  A[edge[1]].append([edge[0],edge[2]])
  
depth = [-1] * (N+1)
depth[1] = 0

def dfs(A,s):
  for v in A[s]:
    if depth[v[0]] == -1:
      depth[v[0]] = depth[s] + v[1]
      dfs(A,v[0])
      
dfs(A,1)

for d in depth[1:]:
  if d % 2 == 0:
    print(0)
  else:
    print(1)

Submission status: Runtime Error

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


colors = [-1]*N


def dfs(v, color):
    if colors[v] != -1:
        return
    colors[v] = color
    for adjacent_v in adjacent_list[v]:
        if adjacent_v[1] % 2 == 0:
            dfs(adjacent_v[0], color)
        else:
            dfs(adjacent_v[0], 1-color)

dfs(0,0)

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

Submission status: Runtime Error

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

ans = [-1]*N 
def dfs(s, c):
  if ans[s] != -1:
    return
  else:
    ans[s] = c
    for t,w in lines[s]:
      if w%2==0:
        dfs(t,c)
      else:
        dfs(t,c^1)
      
dfs(0, 0)

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

Submission status: Runtime Error

Solution code:
N = int(input())
L = [-1 for i in range(N)]
flg = [0 for i in range(N)]
M = [[] for i in range(N)]
for i in range(N-1):
  u, v, w = map(int, input().split())
  M[u-1].append([v-1, w%2])
  M[v-1].append([u-1, w%2])
L[0] = 0
def dist(m, d):
  L[m] = d % 2
  for i in M[m]:
    if L[i[0]] == -1:
      dist(i[0], i[1] + d)
dist(0, 0)
for i in range(N):
  print(L[i])

Submission status: Runtime Error

Solution code:
N = int(input())
L = [-1 for i in range(N)]
flg = [0 for i in range(N)]
D = {}
S = []
A = []
u, v, w = map(int, input().split())
if w % 2 == 0:
  D[u], D[v] = 0, 0
else:
  D[u], D[v] = 0, 1
for i in range(N - 2):
  u, v, w = map(int, input().split())
  if u in D:
    D[v] = (D[u] + w) % 2
  elif v in D:
    D[u] = (D[v] + w) % 2
  else:
    if w % 2 == 0:
      S.append([u, v])
    else:
      A.append([u, v])
for i in range(len(S)):
  if S[i][0] in D:
    D[S[i][1]] = D[S[i][0]]
  elif S[i][1] in D:
    D[S[i][0]] = D[S[i][1]]
for i in range(len(A)):
  if A[i][0] in D:
    D[A[i][1]] = (D[A[i][0]] + 1) % 2
  elif A[i][1] in D:
    D[A[i][0]] = (D[A[i][1]] + 1) % 2
for i in range(len(S)):
  if S[i][0] in D:
    D[S[i][1]] = D[S[i][0]]
  elif S[i][1] in D:
    D[S[i][0]] = D[S[i][1]]
for i in range(1, N+1):
  print(D[i])

Submission status: Runtime Error

Solution code:
N = int(input())
L = [-1 for i in range(N)]
flg = [0 for i in range(N)]
D = {}
S = []
A = []
u, v, w = map(int, input().split())
if w % 2 == 0:
  D[u], D[v] = 0, 0
else:
  D[u], D[v] = 0, 1
for i in range(N - 2):
  u, v, w = map(int, input().split())
  if u in D:
    D[v] = (D[u] + w) % 2
  elif v in D:
    D[u] = (D[v] + w) % 2
  else:
    if w % 2 == 0:
      S.append([u, v])
    else:
      A.append([u, v])
for i in range(len(S)):
  if S[i][0] in D and S[i][1] not in D:
    D[S[i][1]] = D[S[i][0]]
  elif S[i][1] in D and S[i][0] not in D:
    D[S[i][0]] = D[S[i][1]]
for i in range(len(A)):
  if A[i][0] in D and A[i][1] not in D:
    D[A[i][1]] = (D[A[i][0]] + 1) % 2
  elif A[i][1] in D and A[i][0] not in D:
    D[A[i][0]] = (D[A[i][1]] + 1) % 2
for i in range(1, N+1):
  print(D[i])
print(S, A)

Submission status: Runtime Error

Solution code:
N = int(input())
L = [-1 for i in range(N)]
flg = [0 for i in range(N)]
D = {}
S = []
A = []
u, v, w = map(int, input().split())
if w % 2 == 0:
  D[u], D[v] = 0, 0
else:
  D[u], D[v] = 0, 1
for i in range(N - 2):
  u, v, w = map(int, input().split())
  if u in D:
    D[v] = (D[u] + w) % 2
  elif v in D:
    D[u] = (D[v] + w) % 2
  else:
    if w % 2 == 0:
      S.append([u, v])
    else:
      A.append([u, v])
for i in range(len(S)):
  if S[i][0] in D:
    D[S[i][1]] = D[S[i][0]]
  elif S[i][1] in D:
    D[S[i][0]] = D[S[i][1]]
for i in range(len(A)):
  if A[i][0] in D:
    D[A[i][1]] = (D[A[i][0]] + 1) % 2
  elif A[i][1] in D:
    D[A[i][0]] = (D[A[i][1]] + 1) % 2
for i in range(1, N+1):
  print(D[i])

Submission status: Runtime Error

Solution code:
N = int(input())
a = [[] for _ in range(N)]
ans = [0]*N
for _ in range(N-1):
  u, v, w = map(int, input().split())
  a[u-1].append([v-1, w])
  a[v-1].append([u-1, w])
def calc(u, b):
  for v, w in a[u]:
    if not b == v:
      ans[v]=(ans[u] + w)%2
      calc(v, u)
calc(0, -1)
for i in ans:
  if i % 2 == 0:
    print(0)
  else:
    print(1)

Submission status: Runtime Error

Solution code:
N = int(input())
a = [[] for _ in range(N)]
ans = [0]*N
for _ in range(N-1):
  u, v, w = map(int, input().split())
  a[u-1].append([v-1, w])
  a[v-1].append([u-1, w])
def calc(u, b):
  print(u)
  for v, w in a[u]:
    if not b == v:
      ans[v]=(ans[u] + w)
      calc(v, u)
calc(0, -1)
print(ans)
for i in ans:
  if i % 2 == 0:
    print(0)
  else:
    print(1)

Submission status: Runtime Error

Solution code:
N = int(input())
a = [[] for _ in range(N)]
ans = [0]*N
for _ in range(N-1):
  u, v, w = map(int, input().split())
  a[u-1].append([v-1, w])
  a[v-1].append([u-1, w])
def calc(u, b):
  for v, w in a[u]:
    ans[v]=(ans[u] + w)
    if not b == v:
      calc(v, u)

calc(0, -1)
for i in ans:
  if i % 2 == 0:
    print(0)
  else:
    print(1)

Submission status: Runtime Error

Solution code:
N = int(input())
a = [[] for _ in range(N)]
ans = [0]*N
for _ in range(N-1):
  u, v, w = map(int, input().split())
  a[u-1].append([v-1, w])
  a[v-1].append([u-1, w])
print(a)
def calc(u, b):
  for v, w in a[u]:
    ans[v]=(ans[u] + w)
    if not b == v:
      calc(v, u)

calc(0, -1)
print(ans)
for i in ans:
  if i % 2 == 0:
    print(0)
  else:
    print(1)

Submission status: Runtime Error

Solution code:
def prepare(n,arr):
    nodeArr = [[] for i in range(n)]
    for ar in arr:
        nodeArr[ar[0]-1].append([ar[1]-1,ar[2]])
        nodeArr[ar[1]-1].append([ar[0]-1,ar[2]])
    return nodeArr

def dfs(currentNode,distance):
    res[currentNode] = distance
    for next in nodeArr[currentNode]:
        nextNode = next[0]
        nextNodeDistance = next[1]
        if res[nextNode] == -1:
            dfs(nextNode,distance + nextNodeDistance)




n = int(input())

arr = []

for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

nodeArr = prepare(n, arr)


if n == 1:
    print(1)
else:
    res = [-1 for i in range(n)]

    dfs(0,0)

    result = [(re % 2) for re in res]

    for re in res:
        print(re % 2)

Submission status: Runtime Error

Solution code:
def prepare(n,arr):
    nodeArr = [[] for i in range(n)]
    for ar in arr:
        nodeArr[ar[0]-1].append([ar[1]-1,ar[2]])
        nodeArr[ar[1]-1].append([ar[0]-1,ar[2]])
    return nodeArr

def dfs(parent,currentNode,distance):

    if len(nodeArr[currentNode]) == 1:
        if currentNode != 0:
            # 末点
            res[currentNode] = distance
            return

    for next in nodeArr[currentNode]:
        nextNode = next[0]
        nextNodeDistance = next[1]
        if nextNode != parent:
            dfs(currentNode,nextNode,distance + nextNodeDistance)
    res[currentNode] = distance


n = int(input())

arr = []

for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

nodeArr = prepare(n, arr)


if n == 1:
    print(1)
else:
    res = [-1 for i in range(n)]

    dfs(-1,0,0)

    result = [(re % 2) for re in res]

    for re in res:
        print(re % 2)

Submission status: Runtime Error

Solution code:
def prepare(n,arr):
    nodeArr = [[] for i in range(n)]
    for ar in arr:
        nodeArr[ar[0]-1].append([ar[1]-1,ar[2]])
        nodeArr[ar[1]-1].append([ar[0]-1,ar[2]])
    return nodeArr

def dfs(parent,currentNode,distance):

    if len(nodeArr[currentNode]) == 1:
        if currentNode != 0:
            # 末点
            # res[currentNode] = distance
            return

    for next in nodeArr[currentNode]:
        nextNode = next[0]
        nextNodeDistance = next[1]
        if nextNode == parent:
            continue
        dfs(currentNode,nextNode,distance + nextNodeDistance)
    res[currentNode] = distance


n = int(input())

arr = []

for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

nodeArr = prepare(n, arr)


if n == 1:
    print(1)
else:
    res = [-1 for i in range(n)]

    dfs(-1,0,0)

    result = [(re % 2) for re in res]

    for re in res:
        print(re % 2)

Submission status: Runtime Error

Solution code:
def prepare(n,arr):
    nodeArr = [[] for i in range(n)]
    for ar in arr:
        nodeArr[ar[0]-1].append([ar[1]-1,ar[2]])
        nodeArr[ar[1]-1].append([ar[0]-1,ar[2]])
    return nodeArr

def dfs(parent,currentNode,distance):
    if len(nodeArr) == 0:
        return

    if len(nodeArr[currentNode]) == 1:
        if currentNode != 0:
            # 末点
            res[currentNode] = distance
            return

    for next in nodeArr[currentNode]:
        nextNode = next[0]
        nextNodeDistance = next[1]
        if nextNode == parent:
            continue
        dfs(currentNode,nextNode,distance + nextNodeDistance)
    res[currentNode] = distance


n = int(input())

arr = []

for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

nodeArr = prepare(n, arr)


if n == 1:
    print(1)
else:
    res = [-1 for i in range(n)]

    dfs(-1,0,0)

    result = [(re % 2) for re in res]

    for re in res:
        print(re % 2)

Submission status: Runtime Error

Solution code:
def prepare(n,arr):
    nodeArr = [[] for i in range(n)]
    for ar in arr:
        nodeArr[ar[0]-1].append([ar[1]-1,ar[2]])
        nodeArr[ar[1]-1].append([ar[0]-1,ar[2]])
    return nodeArr

def dfs(parent,currentNode,distance):

    if len(nodeArr[currentNode]) == 1:
        if currentNode != 0:
            # 末点
            res[currentNode] = distance
            return

    for next in nodeArr[currentNode]:
        if len(next) == 0:
            continue
        nextNode = next[0]
        nextNodeDistance = next[1]
        if nextNode == parent:
            continue
        dfs(currentNode,nextNode,distance + nextNodeDistance)
    res[currentNode] = distance


n = int(input())

arr = []

for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

nodeArr = prepare(n, arr)


if n == 1:
    print(1)
else:
    res = [-1 for i in range(n)]

    dfs(-1,0,0)

    result = [(re % 2) for re in res]

    for re in res:
        print(re % 2)

Submission status: Runtime Error

Solution code:
def prepare(n,arr):
    nodeArr = [[] for i in range(n)]
    for ar in arr:
        nodeArr[ar[0]-1].append([ar[1]-1,ar[2]])
        nodeArr[ar[1]-1].append([ar[0]-1,ar[2]])
    return nodeArr

def dfs(parent,currentNode,distance):

    if len(nodeArr[currentNode]) == 1:
        if currentNode != 0:
            # 末点
            res[currentNode] = distance
            return

    for next in nodeArr[currentNode]:
        nextNode = next[0]
        nextNodeDistance = next[1]
        if nextNode == parent:
            continue
        dfs(currentNode,nextNode,distance + nextNodeDistance)
    res[currentNode] = distance


n = int(input())

arr = []

for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

nodeArr = prepare(n, arr)


if n == 1:
    print(1)
else:
    res = [-1 for i in range(n)]

    dfs(-1,0,0)

    result = [(re % 2) for re in res]

    for re in res:
        print(re % 2)

Submission status: Runtime Error

Solution code:
def prepare(n,arr):
    nodeArr = [[] for i in range(n)]
    for ar in arr:
        nodeArr[ar[0]-1].append([ar[1]-1,ar[2]])
        nodeArr[ar[1]-1].append([ar[0]-1,ar[2]])
    return nodeArr

def dfs(parent,currentNode,distance):

    if len(nodeArr[currentNode]) == 1:
        if currentNode != 0:
            # 末点
            res[currentNode] = distance
            return

    for next in nodeArr[currentNode]:
        nextNode = next[0]
        nextNodeDistance = next[1]
        if nextNode == parent:
            continue
        dfs(currentNode,nextNode,distance + nextNodeDistance)
        # res[currentNode] = distance


n = int(input())

arr = []

for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

nodeArr = prepare(n, arr)


if n == 1:
    print(1)
else:
    res = [-1 for i in range(n)]

    dfs(-1,0,0)

    result = [(re % 2) for re in res]

    for re in res:
        print(re % 2)

Submission status: Runtime Error

Solution code:
def prepare(n,arr):
    nodeArr = [[] for i in range(n)]
    for ar in arr:
        nodeArr[ar[0]-1].append([ar[1]-1,ar[2]])
        nodeArr[ar[1]-1].append([ar[0]-1,ar[2]])
    return nodeArr

def dfs(parent,currentNode,distance):

    if len(nodeArr[currentNode]) == 1:
        if currentNode != 0:
            # 末点
            res[currentNode] = distance
            return

    for next in nodeArr[currentNode]:
        nextNode = next[0]
        nextNodeDistance = next[1]
        if nextNode == parent:
            continue
        dfs(currentNode,nextNode,distance + nextNodeDistance)
        res[currentNode] = distance


n = int(input())

arr = []

for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

nodeArr = prepare(n, arr)


if n == 1:
    print(1)
else:
    res = [-1 for i in range(n)]

    dfs(-1,0,0)

    result = [(re % 2) for re in res]

    for re in res:
        print(re % 2)

Submission status: Runtime Error

Solution code:
def prepare(n,arr):
    nodeArr = [[] for i in range(n)]
    for ar in arr:
        nodeArr[ar[0]-1].append([ar[1]-1,ar[2]])
        nodeArr[ar[1]-1].append([ar[0]-1,ar[2]])
    return nodeArr

def dfs(parent,currentNode,distance):

    if len(nodeArr[currentNode]) == 1:
        if currentNode != 0:
            # 末点
            res[currentNode] = distance
            return

    for next in nodeArr[currentNode]:
        nextNode = next[0]
        nextNodeDistance = next[1]
        if nextNode == parent:
            continue
        dfs(currentNode,nextNode,distance + nextNodeDistance)
        res[currentNode] = distance


n = int(input())

# arr = []
#
# for i in range(n-1):
#     arr.append([int(s) for s in input().split(" ")])
#
# nodeArr = prepare(n, arr)

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


res = [-1 for i in range(n)]

dfs(-1,0,0)

result = [(re % 2) for re in res]

for re in res:
    print(re % 2)

Submission status: Runtime Error

Solution code:
def prepare(n,arr):
    nodeArr = [[] for i in range(n)]
    for ar in arr:
        nodeArr[ar[0]-1].append([ar[1]-1,ar[2]])
        nodeArr[ar[1]-1].append([ar[0]-1,ar[2]])
    return nodeArr





def dfs(parent,currentNode,distance):

    if len(nodeArr[currentNode]) == 1:
        if currentNode != 0:
            # 末点
            res[currentNode] = distance
            return

    for next in nodeArr[currentNode]:
        nextNode = next[0]
        nextNodeDistance = next[1]
        if nextNode == parent:
            continue
        dfs(currentNode,nextNode,distance + nextNodeDistance)
        res[currentNode] = distance


n = int(input())

arr = []

for i in range(n-1):
    arr.append([int(s) for s in input().split(" ")])

nodeArr = prepare(n, arr)
res = [-1 for i in range(n)]

dfs(-1,0,0)

result = [(re % 2) for re in res]

for re in res:
    print(re % 2)

Submission status: Runtime Error

Solution code:
# coding: utf-8

def paint(node):
	for n in adj_list[node]:
		if color_list[n[0]] != "-1":
			continue

		if n[1] % 2 == 0:
			color_list[n[0]] = color_list[node]
		else:
			if color_list[node] == "0":
				color_list[n[0]] = "1"
			else:
				color_list[n[0]] = "0"

		paint(n[0])

N = int(input())
adj_list = [[] for i in range(N)]

for i in range(N - 1):
	elm = [int(x) for x in input().split()]
	adj_list[elm[0] - 1].append([elm[1] - 1, elm[2]])
	adj_list[elm[1] - 1].append([elm[0] - 1, elm[2]])

color_list = ["-1"] * N
color_list[0] = "0"

import sys
sys.setrecursionlimit(10000)
paint(0)

for c in color_list:
	print(c)

Submission status: Runtime Error

Solution code:
# coding: utf-8

def paint(node):
	for n in adj_list[node]:
		if color_list[n[0]] != "-1":
			continue

		if n[1] % 2 == 0:
			color_list[n[0]] = color_list[node]
		else:
			if color_list[node] == "0":
				color_list[n[0]] = "1"
			else:
				color_list[n[0]] = "0"

		paint(n[0])

N = int(input())
adj_list = [[] for i in range(N)]

for i in range(N - 1):
	elm = [int(x) for x in input().split()]
	adj_list[elm[0] - 1].append([elm[1] - 1, elm[2]])
	adj_list[elm[1] - 1].append([elm[0] - 1, elm[2]])

color_list = ["-1"] * N

color_list[0] = "0"
paint(0)

for c in color_list:
	print(c)

Submission status: Runtime Error

Solution code:
import sys

input = sys.stdin.readline

n = int(input())
graph = [[] for i in range(n + 1)]
cost = [[] * (n + 1) for j in range(n + 1)]
for i in range(n - 1):
    u, v, w = map(int, input().split())
    graph[u].append(v)
    graph[v].append(u)
    cost[u][v] = w
    cost[v][u] = w
ans = [] * (n + 1)


def dfs(a):
    dist = [-1] * (n + 1)
    stack = [a]
    dist[a] = 0
    while stack:
        v = stack.pop()
        for w in graph[v]:
            if dist[w] == -1:
                dist[w] = dist[v] + cost[w][v]
                dist[w] %= 2
                stack.append(w)
            else:
                continue
    return dist


dist = dfs(1)
print(*dist[1:], sep="\n")

Submission status: Runtime Error

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

for edge in edges:
    A[edge[0]].append([edge[1],edge[2]])
    A[edge[1]].append([edge[0],edge[2]])

depth = [-1] * (N+1)
depth[1] = 0

def dfs(A,s):
    for v in A[s]:
        if depth[v[0]] == -1:
            depth[v[0]] = depth[s] + v[1]
            dfs(A,v[0])    
dfs(A,1)
for d in depth[1:]:
    if d % 2 == 0:
        print(0)
    else:
        print(1)

Submission status: Runtime Error

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

def dfs(pos, not_pos, tree_dict, ans_li, length):
  n_li = tree_dict[pos]
  for n, w in n_li:
    if n == not_pos:
      continue
    new_length = length+w
    if new_length%2 == 0:
      ans_li[n] = 0
    else:
      ans_li[n] = 1
    ans = dfs(n, pos, tree_dict, ans_li, new_length)
  return ans_li

def run():
  N = int(input())
  tree_dict = {}
  ans_li = [0]*(N+1)
  for n in range(N-1):
    u, v, w = map(int, input().split())
    tree_dict.setdefault(u, []).append((v, w))
    tree_dict.setdefault(v, []).append((u, w))
  length = 0
  ans = dfs(1, -1, tree_dict, ans_li, length)
  for a in ans[1:]:
    print(a)
    

if __name__ == '__main__':
  run()

Submission status: Runtime Error

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

def dfs(pos, not_pos, tree_dict, ans_li, length):
  n_li = tree_dict[pos]
  for n, w in n_li:
    if n == not_pos:
      continue
    new_length = length+w
    if new_length%2 == 0:
      ans_li[n] = 0
    else:
      ans_li[n] = 1
    ans = dfs(n, pos, tree_dict, ans_li, new_length)
  return ans_li

def run():
  N = int(input())
  tree_dict = {}
  ans_li = [0]*(N+1)
  for n in range(N-1):
    u, v, w = map(int, input().split())
    tree_dict.setdefault(u, []).append((v, w))
    tree_dict.setdefault(v, []).append((u, w))
  length = 0
  ans = dfs(1, -1, tree_dict, ans_li, length)
  for a in ans[1:]:
    print(a)
    

if __name__ == '__main__':
  run()

Submission status: Runtime Error

Solution code:
def dfs(pos, not_pos, tree_dict, ans_li, length):
  n_li = tree_dict[pos]
  for n, w in n_li:
    if n == not_pos:
      continue
    new_length = length+w
    if new_length%2 == 0:
      ans_li[n] = 0
    else:
      ans_li[n] = 1
    ans = dfs(n, pos, tree_dict, ans_li, new_length)
  return ans_li

def run():
  N = int(input())
  tree_dict = {}
  ans_li = [0]*(N+1)
  for n in range(N-1):
    u, v, w = map(int, input().split())
    tree_dict.setdefault(u, []).append((v, w))
    tree_dict.setdefault(v, []).append((u, w))
  length = 0
  ans = dfs(1, -1, tree_dict, ans_li, length)
  for a in ans[1:]:
    print(a)
    

if __name__ == '__main__':
  run()

Submission status: Runtime Error

Solution code:
N=int(input())
uvw=[list(map(int, input().split())) for i in range(N-1)]
nodelink=[[] for i in range(N+1)]
for i in range(N-1):
  nodelink[uvw[i][0]].append([uvw[i][1], uvw[i][2]])
  nodelink[uvw[i][1]].append([uvw[i][0], uvw[i][2]])
  
color=[-1]*(N+1)

def f(nuru, iro):
  color[nuru]=iro
  tonari=nodelink[nuru]
  for nuruyo in tonari:
    if color[nuruyo[0]]<0:
      if (nuruyo[1]%2)==0:
        f(nuruyo[0], iro)
      else:
        f(nuruyo[0], 1-iro)
      
f(1, 0)

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

Submission status: Runtime Error

Solution code:
N, = map(int, input().split())
d = [set() for _ in range(N+1)]
l = [set() for _ in range(N+1)]
c = [0 for _ in range(N+1)]
for _ in range(N-1):
    u, v, p = map(int, input().split())
    d[u].add(v)
    d[v].add(u)
    l[min(u,v)] = p

def it(v, p):
    for u in d[v]:
        if u == p:
            continue
        c[u] = int(not c[v]) if l[min(u, v)]%2 else c[v]
        it(u, v)
it(1, None)
for i in range(1, N+1):
    print(c[i])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
As = [0] + list(map(int, input().split()))
from itertools import accumulate
As = list(accumulate(As))
M = len(As)
d = []
j = 0
r = 0
for i in range(M):
    a = As[i]
    while True:
        if j == M:
            break
        if a + K <= As[j]:
            break
        j += 1
    r += M - j
print(r)

Submission status: Runtime Error

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
#from scipy.misc import comb

N = int(input())

G = [[] for _ in range(N+1)]

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

dist = [-1]*(N+1)
dist[1] = 0

def dfs(u):
    for edge in G[u]:
        if dist[edge[0]] == -1:
            dist[edge[0]] = (dist[u]+edge[1])%2
            dfs(edge[0])

dfs(1)

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

Submission status: Runtime Error

Solution code:
def dfs(u):
    for v, w in edge[u]:
        if trail[v] is None:
            trail[v] = (trail[u] + w) % 2
            dfs(v)


N = int(input())
trail = [None] * N
edge = [[] for _ in range(N)]

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

trail[0] = 0
dfs(0)

for t in trail:
    print(t)

Submission status: Runtime Error

Solution code:
sys.setrecursionlimit(10**7)
n = int(input())
g = [[] for _ in range(n)]

for _ 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))
    
ans = [0] * n

def dfs(v,p,c):
    ans[v] = c
    for i in g[v]:
        if i[0] != p:
            if i[1] % 2 ==0:
                dfs(i[0],v,c)
            else:
                dfs(i[0],v,1-c)

dfs(0,-1,0)

for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
n = int(input())
g = [[] for _ in range(n)]

for _ 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))
    
ans = [0] * n

def dfs(v,p,c):
    ans[v] = c
    for i in g[v]:
        if i[0] != p:
            if i[1] % 2 ==0:
                dfs(i[0],v,c)
            else:
                dfs(i[0],v,1-c)

dfs(0,-1,0)

for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
N = int(input())
edges = [[] for _ in range(N+1)]
for _ in range(N-1):
    u, v, w = map(int, input().split())
    edges[u].append([v, w])
    edges[v].append([u, w])
parent = [[] for _ in range(N)]
ans = [0 for _ in range(N)]


def dfs(edge, pd):
    for child, d in edges[edge]:
        if edge in parent[child-1]:
            continue
        cd = pd + d
        if cd % 2 == 0:
            ans[child-1] = 0
        else:
            ans[child-1] = 1
        parent[child-1].append(edge)
        dfs(child, cd)


def solve():
    dfs(1, 0)
    for i in range(N):
        print(ans[i])


if __name__ == '__main__':
    solve()