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:
sys.setrecursionlimit(10000000)
N = int(input())
JU = [-1] * N

LI = [[] for _ in range(N)]
for _ in range(N-1):
    u,v,w = map(int,input().split())
    W = w%2
    LI[u-1].append([v-1,W])
    LI[v-1].append([u-1,W])
def dfs(x):
    for i in range(len(LI[x])):
        j = LI[x][i][0]
        k = LI[x][i][1]
        if JU[j] == -1:
            if JU[i] == 0:
                if k == 0:
                    JU[j] = 0
                    dfs(j)
                else:
                    JU[j] = 1
                    dfs(j)
            else:
                if k == 0:
                    JU[j] = 1
                    dfs(j)
                else:
                    JU[j] = 0
                    dfs(j)

for i in range(N):
    if JU[i] == -1:
        JU[i] = 0
        dfs(i)
for i in range(N):
    print(JU[i])

Submission status: Runtime Error

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

col = [ -1 for i in range(N+1)]

def dfs(x,c):
    if col[x] == -1:
        col[x] = c
        for v,w in e[x]:
            if col[v] == -1:
                dfs(v , c+w)

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

Submission status: Runtime Error

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

col = [ -1 for i in range(N+1)]

def dfs(x,c):
    col[x] = c
    for v,w in e[x]:
        if col[v] == -1:
            dfs(v , c+w)

dfs(1,0)

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

Submission status: Runtime Error

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

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

dist=[-1 for _ in range(N)]
def measure(src):
    for v,w in edges[src]:
        if dist[v]==-1:
            dist[v]=dist[src]+w
            measure(v)

dist[0]=0
measure(0)

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

Submission status: Runtime Error

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

def decide_color(tree_dic, current_node, bw_list):
    if 0 not in bw_list:
        return None
    
    for next_node, same_diff in tree_dic[current_node]:
        if bw_list[next_node-1] == 0:
            if same_diff == "same":
                bw_list[next_node-1] = bw_list[current_node-1]
                return decide_color(tree_dic, next_node, bw_list)
            else:
                bw_list[next_node-1] = -bw_list[current_node-1]
                return decide_color(tree_dic, next_node, bw_list)

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

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10000)

n = int(input())

ulist = []

for _ in range(n):
    ulist.append([])

vlist = [True] * n

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


def check(checkV, fromV):
    for data in ulist[checkV]:
        if data[0] != fromV:
            vlist[data[0]] = vlist[checkV] if data[1] % 2 == 0 else not vlist[checkV]
            check(data[0], checkV)

check(0, -1)

for i in range(n):
    print(1 if vlist[i] else 0)

Submission status: Runtime Error

Solution code:
n = int(input())

ulist = []

for _ in range(n):
    ulist.append([])

vlist = [True] * n

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


def check(checkV, fromV):
    for data in ulist[checkV]:
        if data[0] != fromV:
            vlist[data[0]] = vlist[checkV] if data[1] % 2 == 0 else not vlist[checkV]
            check(data[0], checkV)

check(0, -1)

for i in range(n):
    print(1 if vlist[i] else 0)

Submission status: Runtime Error

Solution code:
def dfs(now,parent,color):
    col[now]=color
    for node in nodes[now]:
        if node[0]==parent:
            continue
        if node[1]%2==0:
            dfs(node[0],now,color)
        else:
            dfs(node[0],now,1-color)

n=int(input())
e_list = [[int(i) for i in input().split()] for i in range(n-1)]
col=[-1]*n

nodes={}
for e in e_list:
    nodes.setdefault(e[0]-1, []).append((e[1]-1,e[2]))
    nodes.setdefault(e[1]-1, []).append((e[0]-1,e[2]))

dfs(0,-1,1)

for c in col:
    print(c)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

def search_neighbor_node(node_num):
    for k, v in e[node_num].items():
        if color_list.get(k) is not None:
            continue
        if v % 2 == 0:
            color_list[k] = color_list[node_num]
        else:
            color_list[k] = (color_list[node_num] + 1) % 2
        search_neighbor_node(k)


n = int(input())
e = defaultdict(lambda :defaultdict(list))

color_list = {1: 0}

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


search_neighbor_node(1)

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

Submission status: Runtime Error

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys

sys.setrecursionlimit(2000)

mod = 10**9+7

N = int(input())
graph = [set() for i in range(N+1)]

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


res = [None for i in range(N+1)]

# v は今見ている頂点，p はｖの親
def dfs(node,p,color): # Depth First Serch
    res[node] = color
    for new_node,w in graph[node]:
        if new_node == p:
            pass
        else:
            if w == 1:dfs(new_node,node,1-color)
            else:dfs(new_node,node,color)
#tree 探索　
# vは頂点，ｐは親
dfs(0,-1,0)

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

Submission status: Runtime Error

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

mod = 10**9+7

N = int(input())
graph = [set() for i in range(N+1)]

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


res = [None for i in range(N+1)]

# v は今見ている頂点，p はｖの親
def dfs(node,p,color): # Depth First Serch
    res[node] = color
    for new_node,w in graph[node]:
        if new_node == p:
            pass
        else:
            if w == 1:dfs(new_node,node,1-color)
            else:dfs(new_node,node,color)
#tree 探索　
# vは頂点，ｐは親
dfs(0,-1,0)

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

Submission status: Runtime Error

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

mod = 10**9+7

N = int(input())
graph = [set() for i in range(N+1)]

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


res = [None for i in range(N+1)]

# v は今見ている頂点，p はｖの親
def dfs(node,p,color): # Depth First Serch
    res[node] = color
    for new_node,w in graph[node]:
        if new_node == p:
            continue
        if w == 1:dfs(new_node,node,1-color)
        else:dfs(new_node,node,color)
#tree 探索　
# vは頂点，ｐは親
dfs(0,-1,0)

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

Submission status: Runtime Error

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

mod = 10**9+7

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

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


res = [None for i in range(N+1)]

# v は今見ている頂点，p はｖの親
def dfs(node,p,color): # Depth First Serch
    res[node] = color
    for new_node,w in graph[node]:
        if new_node == p:
            continue
        if w % 2 == 1:dfs(new_node,node,1-color)
        else:dfs(new_node,node,color)
#tree 探索　
# vは頂点，ｐは親
dfs(0,-1,0)

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

Submission status: Runtime Error

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

mod = 10**9+7

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

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


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

# v は今見ている頂点，p はｖの親
def dfs(node,p,color): # Depth First Serch
    res[node] = color
    for new_node,w in graph[node]:
        if new_node == p:
            continue
        if w % 2 == 1:dfs(new_node,node,1-color)
        else:dfs(new_node,node,color)
#tree 探索　
# vは頂点，ｐは親
dfs(0,-1,0)

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

Submission status: Runtime Error

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

mod = 10**9+7

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

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

res = [0 for i in range(N)]

# v は今見ている頂点，p はｖの親
def dfs(node,p,color): # Depth First Serch
    res[node] = color
    new_nodes = graph[node]
    # print(index_num)
    for new_node,w in new_nodes:
        if new_node == p:
            continue
        if w% 2 == 1:dfs(new_node,node,1-color)
        else:dfs(new_node,node,color)

#tree 探索　
# vは頂点，ｐは親
dfs(0,-1,0)

# print(res[1:])
for i in range(N):
    print(res[i])

Submission status: Runtime Error

Solution code:
def rec(edges, nodes, cp, cdistance, visited):
    if not visited[cp]:
        visited[cp] = True
        nodes[cp] = (cdistance % 2 == 0)
        if edges[cp]:
            for v, w in edges[cp]:
                rec(edges, nodes, v, cdistance + w, visited)


def main():
    N = int(input())
    nodes = [False] * N
    visited = [False] * N
    edges = [[] for _ in range(N)]

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

    rec(edges, nodes, 0, 0, visited)

    for n in nodes:
        print(0 if n else 1)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
t = []

for i in range(n - 1):
    t.append(list(map(int, input().split())))

t.sort(key=lambda x:(x[0], x[1]))

white_list = [1]
black_list = []

for e in t:
    if e[2] % 2 == 0:
        if e[0] in white_list or e[1] in white_list:
            white_list.append(e[0])
            white_list.append(e[1])
        else:
            black_list.append(e[0], e[1])
            black_list.append(e[1], e[1])
    else:
        if e[0] in white_list or e[1] in black_list:
            white_list.append(e[0])
            black_list.append(e[1])
        else:
            white_list.append(e[1])
            black_list.append(e[0])

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

Submission status: Runtime Error

Solution code:
def coloring(A, n, colors, color):
  for (i,w) in A[n].items():
    if w != 0:
    #   print(i)
      A[i].pop(n, None)
      if w % 2 == 1:
        color = str(1-int(color))
      colors[int(i)-1] = color
    #   print(colors)
      coloring(A, i, colors, color)


if __name__ == '__main__':
  a = int(input())
  A = {str(j+1):{} for j in range(a)}
#   print(A)
  
  colors = ['0' for i in range(a)]
  V = [[x for x in input().split()] for _ in range(a-1)]
#   print(V)
  for v in V:
    A[v[0]][v[1]]=int(v[2])
    A[v[1]][v[0]]=int(v[2])    
#   print(A)
  coloring(A, '1', colors, '0')
  for c in colors:
    print(c)

Submission status: Runtime Error

Solution code:
dp = [-1] * (10 ** 5)

n = int(input())
tree = [list(map(int, input().split())) for i in range(n - 1)]

flag = 1
for t in tree:
    if t[2] % 2 == 0:
        if dp[t[0] - 1] != -1:
            dp[t[1] - 1] = dp[t[0] - 1]
        elif dp[t[1] - 1] != -1:
            dp[t[0] - 1] = dp[t[1] - 1]
        else:
            dp[t[0] - 1] = dp[t[1] - 1] = 0
            flag = 0
    else:
        if dp[t[0] - 1] ==  0 and dp[t[1] - 1] == -1:
            dp[t[1] - 1] = 1
        elif dp[t[0] - 1] == 1 and dp[t[1] - 1] == -1:
            dp[t[1] - 1] = 0
        elif dp[t[1] - 1] ==  0 and dp[t[0] - 1] == -1:
            dp[t[0] - 1] = 1
        elif dp[t[1] - 1] == 1 and dp[t[0] - 1] == -1:
            dp[t[0] - 1] = 0
        else:
            dp[t[0] - 1] = 0
            dp[t[1] - 1] = 1

for i in range(n):
    if dp[n] == -1:
        if flag:
            dp[n] = 0
        else:
            dp[n] = 1

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

Submission status: Runtime Error

Solution code:
N = int(input())
nodes = [[] for _ in range(N)]
visited = [False for _ in range(N)]
dist = [0 for _ in range(N)]

def dfs(i,d):
    dist[i] = d
    for v, w in nodes[i]:
        if not visited[v]:
            visited[v] = True
            dfs(v, d+w)

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

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

Submission status: Runtime Error

Solution code:
n=int(input())
kyori=sorted([list(map(int,input().split())) for i in range(n)])
ans=[0]*n

for i in range(n):
  C=kyori[i-1][0]
  if kyori[i-1][2]%2==0:
    ans[kyori[i-1][1]-1]=C
  else:
    ans[kyori[i-1][1]-1]=1-C
    
for i in range(n):
  print(ans[i])

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())

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

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

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

Submission status: Runtime Error

Solution code:
def d_even_relation_dfs(N, Nodes):  # DFSによる
    graph = [[] for _ in range(N)]
    for u, v, w in Nodes:  # 0-basedとする
        graph[u - 1].append((v - 1, w))
        graph[v - 1].append((u - 1, w))

    def distance_from_root(parent, current, distance, ret=[0] * N):  # 根(頂点0)から各頂点への距離
        def dfs(p, c, d):
            ret[c] = d
            for next_edge, dist in graph[c]:
                if next_edge != p:  # 木を「逆戻り」しない頂点だけ調べる
                    dfs(c, next_edge, d + dist)
        dfs(parent, current, distance)
        return ret
    ans = (1 if d % 2 == 0 else 0 for d in distance_from_root(-1, 0, 0))  # 根の親は-1番とする
    return ' '.join(map(str, ans))

N = int(input())
Nodes = [[int(i) for i in input().split()] for j in range(N - 1)]
print(d_even_relation_dfs(N, Nodes))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:


N = int(input())

G = {}

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

# print(G)

# G = {1: [(2, 2)], 2: [(1, 2), (3, 1)], 3: [(2, 1)]}
# G = {2: [(5, 2), (3, 10)], 5: [(2, 2)], 3: [(2, 10), (1, 8), (4, 2)], 1: [(3, 8)], 4: [(3, 2)]}
# N = len(G)

def color(coloring, current):
    for v, w in G[current]:
        if v not in coloring:
            if w%2==0:
                coloring[v] = coloring[current]
            else:
                coloring[v] = 1-coloring[current]
            color(coloring, v)
    return coloring

coloring = color({1: 0}, 1)

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

Submission status: Runtime Error

Solution code:


N = int(input())

G = {}

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

# print(G)

# G = {1: [(2, 2)], 2: [(1, 2), (3, 1)], 3: [(2, 1)]}
# G = {2: [(5, 2), (3, 10)], 5: [(2, 2)], 3: [(2, 10), (1, 8), (4, 2)], 1: [(3, 8)], 4: [(3, 2)]}
# N = len(G)

def color(coloring, current):
    for v, w in G[current]:
        if v not in coloring:
            if w%2==0:
                coloring[v] = coloring[current]
            else:
                coloring[v] = 1-coloring[current]
            color(coloring, v)
    return coloring

coloring = color({1: 0}, 1)

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

Submission status: Runtime Error

Solution code:


N = int(input())

G = {}

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

# print(G)

# G = {1: [(2, 2)], 2: [(1, 2), (3, 1)], 3: [(2, 1)]}
# G = {2: [(5, 2), (3, 10)], 5: [(2, 2)], 3: [(2, 10), (1, 8), (4, 2)], 1: [(3, 8)], 4: [(3, 2)]}

def color(coloring, current):
    for v, w in G[current]:
        if v not in coloring:
            if w%2==0:
                coloring[v] = coloring[current]
            else:
                coloring[v] = 1-coloring[current]
            color(coloring, v)
    return coloring

print(color({1: 0}, 1))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
class Node:
  def __init__(self, id):
    self.id = id
    self.parent = None
    self.d = 0

  def dist(self):
    if self.parent is None:
      return self.d
    else:
      return self.d + self.parent.dist()
    
    
n = int(input())
edges = {k:[] for k in range(1, n+1)}
for _ in range(n-1):
  u, v, w = map(int, input().split(' '))
  edges[u].append((v,w))
  edges[v].append((u,w))

nodes = dict()
root = Node(1)
tmp = [root]
nxt = []

while len(nodes.keys()) < n:
  for node in tmp:
    edge = edges[node.id]
    for i, w in edge:
      newn = Node(i)
      newn.parent = node
      newn.d = w
      nxt.append(newn)
      edges[i].remove((node.id, w))
    nodes[node.id] = node
  tmp = nxt
  nxt = []

for i in range(1, n+1):
  t = nodes[i]
  print(t.dist()%2)

Submission status: Runtime Error

Solution code:
class Node:
  def __init__(self, id):
    self.id = id
    self.parent = None
    self.d = 0

  def dist(self):
    if self.parent is None:
      return self.d
    else:
      return self.d + self.parent.dist()
    
    
n = int(input())

nodes = dict()
for _ in range(n-1):
  u, v, w = map(int, input().split(' '))
  un = nodes[u] if u in nodes else Node(u)
  vn = nodes[v] if v in nodes else Node(v)
  
  if vn.parent is None:
    vn.d = w
    vn.parent = un
  elif un.parent is None:
    un.d = w
    un.parent = vn
  else:
    tmp = vn
    stack = [vn.id]
    while tmp.parent is not None:
      p = tmp.parent
      stack.insert(0, p.id)
      tmp = p
    for i in range(len(stack)-1):
      t1 = nodes[stack[i]]
      t2 = nodes[stack[i+1]]
      t1.parent = t2
      t1.d = t2.d
    vn.parent = un
    vn.d = w
      
  nodes[u] = un
  nodes[v] = vn
  
      
for i in range(1, n+1):
  t = nodes[i]
  print(t.dist()%2)

Submission status: Runtime Error

Solution code:
class Node:
  def __init__(self, id):
    self.id = id
    self.parent = None
    self.d = 0

  def dist(self):
    if self.parent is None:
      return self.d
    else:
      return self.d + self.parent.dist()
    
    
n = int(input())

nodes = dict()
for _ in range(n-1):
  u, v, w = map(int, input().split(' '))
  un = nodes[u] if u in nodes else Node(u)
  vn = nodes[v] if v in nodes else Node(v)
  
  if vn.parent is None:
    vn.d = w
    vn.parent = un
  elif un.parent is None:
    un.d = w
    un.parent = vn
  else:
    tmp = vn
    stack = [vn.id]
    while tmp.parent is not None:
      p = tmp.parent
      stack.insert(0, p.id)
      tmp = p
    for i in range(len(stack)-1):
      t1 = nodes[stack[i]]
      t2 = nodes[stack[i+1]]
      t1.parent = t2
    vn.parent = un
      
  nodes[u] = un
  nodes[v] = vn
  
      
for i in range(1, n+1):
  t = nodes[i]
  print(t.dist()%2)

Submission status: Runtime Error

Solution code:
import numpy as np
n=[] 
while True:
    try:
        n.append(list(map(int,input().split())))
    except:
        break;
 
times=n[0]
a = n[1:]
ans=[3]*times[0]
for i in range(times[0]-1):
    if ans[a[i][0]-1]==3 and ans[a[i][1]-1]==3:
        if a[i][2]%2==0:
            ans[a[i][0]-1]=0;ans[a[i][1]-1]=0
        else:
	        ans[a[i][0]-1]=0;ans[a[i][1]-1]=1           
    if ans[a[i][0]-1]==3 and ans[a[i][1]-1]==0:
        if a[i][2]%2==0:
            ans[a[i][0]-1]=0
        else:
            ans[a[i][0]-1]=1
    if ans[a[i][0]-1]==0 and ans[a[i][1]-1]==3:
        if a[i][2]%2==0:
            ans[a[i][1]-1]=0
        else:
            ans[a[i][1]-1]=1  
    if ans[a[i][0]-1]==3 and ans[a[i][1]-1]==1:
        if a[i][2]%2==0:
            ans[a[i][0]-1]=0
        else:
            ans[a[i][0]-1]=0
    if ans[a[i][0]-1]==1 and ans[a[i][1]-1]==3:
        if a[i][2]%2==0:
            ans[a[i][1]-1]=0
        else:
            ans[a[i][1]-1]=0  
              
for i in range(times[0]):
    print(ans[i])

Submission status: Runtime Error

Solution code:
n = int(input())

nodes = [[0, 0] for _ in range(n+1)]
for _ in range(n-1):
  u, v, w = map(int, input().split())
  
  un = nodes[u]
  vn = nodes[v]
  
  if vn[0] == 0:
    vn[1] = w % 2
    vn[0] = u
  else:
    un[1] = w % 2
    un[0] = v  


def dist(u):
  t = nodes[u]
  if t[0]==0:
    return t[1]
  else:
    return t[1] + dist(t[0])
    
for i in range(1, n+1):
  print(dist(i)%2)

Submission status: Runtime Error

Solution code:
class Node:
    def __init__(self, val):
        self.num = val
        self.nodes = []
        self.distance = []

def set_color(node, color):
    if color_list[node.num] == None:
        color_list[node.num] = color
        if len(node.nodes) > 0:
            for child, dist in zip(node.nodes, node.distance):
                if child != node:
                    if dist == 0:
                        set_color(child, color)
                    else:
                        set_color(child, not color)

N= int(input())

node_list = [Node(i) for i in range(N)]
color_list = [None for i in range(N)]

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

set_color(node_list[0], True)

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

Submission status: Runtime Error

Solution code:
class Node:
    def __init__(self, val):
        self.num = val
        self.nodes = []
        self.distance = []

def set_color(node, color):
    if color_list[node.num] == None:
        color_list[node.num] = color
        if len(node.child) > 0:
            for child, dist in zip(node.nodes, node.distance):
                if child != node:
                    if dist == 0:
                        set_color(child, color)
                    else:
                        set_color(child, not color)

N= int(input())

node_list = [Node(i) for i in range(N)]
color_list = [None for i in range(N)]

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

set_color(node_list[0], True)

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

Submission status: Runtime Error

Solution code:
class Node:
  def __init__(self):
    self.parent = None
    self.d = 0

  def dist(self):
    if self.parent is None:
      return self.d
    else:
      return self.d + self.parent.dist()
    
    
n = int(input())

nodes = dict()
for _ in range(n-1):
  u, v, w = map(int, input().split(' '))
  un = nodes[u] if u in nodes else Node()
  vn = nodes[v] if v in nodes else Node()
  
  if vn.parent is None:
    vn.d = w%2
    vn.parent = un
  else:
    un.d = w%2
    un.parent = vn
  
  nodes[u] = un
  nodes[v] = vn
  
      
for i in range(1, n+1):
  t = nodes[i]
  print(t.dist()%2)

Submission status: Runtime Error

Solution code:
class Node:
    def __init__(self, val):
        self.num = val
        self.nodes = []
        self.distance = []

def set_color(node, color):
    if color_list[node.num] == None:
        color_list[node.num] = color
        for child, dist in zip(node.nodes, node.distance):
            if child != node:
                if dist == 0:
                    set_color(child, color)
                else:
                    set_color(child, not color)

N= int(input())

node_list = [Node(i) for i in range(N)]
color_list = [None for i in range(N)]

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

set_color(node_list[0], True)

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

Submission status: Runtime Error

Solution code:
class Node:
  def __init__(self):
    self.parent = None
    self.d = 0

  def dist(self):
    if self.parent is None:
      return self.d
    else:
      return self.d + self.parent.dist()
    
    
n = int(input())

nodes = dict()
for _ in range(n-1):
  u, v, w = map(int, input().split(' '))
  un = nodes[u] if u in nodes else Node()
  vn = nodes[v] if v in nodes else Node()
  
  if vn.parent is None:
    vn.d = w
    vn.parent = un
  else:
    un.d = w
    un.parent = vn
  
  nodes[u] = un
  nodes[v] = vn
  
      
for i in range(1, n+1):
  t = nodes[i]
  print(t.dist()%2)

Submission status: Runtime Error

Solution code:
import numpy as np

class Node:
    def __init__(self, val):
        self.num = val
        self.nodes = []
        self.distance = []

def set_color(node, color):
    if color_list[node.num] == None:
        color_list[node.num] = color
        for child, dist in zip(node.nodes, node.distance):
            if child != node:
                if dist == 0:
                    set_color(child, color)
                else:
                    set_color(child, not color)

N= int(input())

node_list = [Node(i) for i in range(N)]
color_list = np.array([None for i in range(N)])

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

set_color(node_list[0], True)

for i in np.array(color_list, dtype=int):
    print(i)