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())
U = [list(map(int, input().split())) for _ in range(N-1)]

par = [i for i in range(N+1)]
rank = [0] * (N+1)

def find(x):
    if par[x] == x:
        return x
    else:
        par[x] == find(par[x])
        return par[x]

def same_check(x, y):
    return find(x) == find(y)

def union(x, y):
    x = find(x)
    y = find(y)

    if rank(x) < rank(y):
        par[x] = y
    else:
        par[y] = x

        if rank[x] == rank[y]:
            rank[x] += 1

for i in range(N-1):
    x = U[i][0]
    y = U[i][1]
    if U[i][2] % 2 == 0:
        union(x, y)

par.pop(0)
uni = list(set(par))
for i in range(N):
    if par[i] == uni[0]:
        print(0)
    else:
        print(1)

Submission status: Runtime Error

Solution code:
import sys
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def II(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def SI(): return input()
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')

import numpy as np

N = II()
u_li = []
v_li = []
w_li = []
for i in range(N-1):
    u, v, w = LI()
    u_li.append(u)
    v_li.append(v)
    w_li.append(w)


node_count = max(max(u_li), max(v_li))

mat = np.zeros((node_count,node_count))

for i in range(N-1):
    a = min(u_li[i], v_li[i])
    b = max(u_li[i], v_li[i])
    mat[a-1, b-1] = w_li[i]
mat = mat.astype(int)

color = [None]*N
checked = [False]*N

color[0] = 0
checked[0] = True
queue = [0]

while True:
    i = queue[0]
    for j in mat[i].nonzero():
        if mat[i,j]%2 == 0:
            color[j] = color[i]
        else:
            color[j] = 1 if color[i] == 0 else 0
        queue.append(j)
    queue.pop(0)
    if len(queue) == 0: break

for i in color:
    print(i)

Submission status: Runtime Error

Solution code:
# 木の深さ優先探索

def add_edge(u, v, distance ):
	G[u].append([v, distance])
	G[v].append([u, distance])

def dfs(vertex, distance_from_start):
	global colors

	colors[vertex] = distance_from_start % 2

	for info in G[vertex]:
		v = info[0]
		if colors[v] >= 0: 
			#探索済み
			pass
		else:
			dfs(v, distance_from_start + info[1])

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

G = [[] for _ in range(n)] #グラフの隣接リスト表現
for edge in edges:
	add_edge(edge[0]-1, edge[1]-1, edge[2])

colors = [-1] * n
colors[0] = 0

dfs(0, 0)

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

Submission status: Runtime Error

Solution code:
n = int(input())
edge = [[] for _ in range(n+1)]
node = [0]*(n+1)
distance = {}
for _ in range(n-1):
    u,v,w = map(int,input().split())
    edge[u].append(v)
    distance[str(u)+"&"+str(v)] = w

stack = edge[1]
now = 1
while True:
    if len(stack) == 0:
        break
    temp = stack[-1]
    stack.pop(-1)
    for e in edge[temp]:
        stack.append(e)
    if distance[str(now)+"&"+str(temp)]%2 == 1:
        node[temp] = 1
    now = temp
print(*node[1:],sep ="\n")

Submission status: Runtime Error

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

branch = [0]*n

dist.sort()

for i in range(n):
    if dist[i][2] % 2 == 1:
        if branch[dist[i][0]-1] == 0:
            branch[dist[i][1]-1] == 1
        if branch[dist[i][0]-1] == 1:
            branch[dist[i][1]-1] == 0

for j in range(n):
    print(branch[j])

Submission status: Runtime Error

Solution code:
n = int(input())
uvw = [[int(v) for v in input().split()] for _ in range(n-1)]

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

rinsetu = [[] for _ in range(n)]

for v in uvw:
  rinsetu[v[0]-1].append({
    "cost" : v[2],
    "to": v[1]-1
  })
  rinsetu[v[1]-1].append({
    "cost" : v[2],
    "to": v[0]-1
  })

def dps(i,n):
  for v in rinsetu[i]:
    to = v["to"]
    c = v["cost"]

    if color[to] != -1:
      continue
    
    color[to] = 1 if n % 2 == 0 else 0

    dps(to,n+c)


color[0] = 1

dps(0,0)
for v in color:
  print(v)

Submission status: Runtime Error

Solution code:
def solve():
    n = int(input())
    ans = [-1] * (n + 1)
    li = [[] for _ in range(n + 1)]
    for _ in range(n - 1):
        u, v, w = map(int, input().split())
        li[u].append((v, w))
        li[v].append((u, w))
    def f(i, ans, c):
        if li[i] == []:
            return
        for u, w in li[i]:
            if ans[u] == -1:
                if w % 2 == 0:
                    ans[u] = c
                    f(u, ans, c)
                else:
                    ans[u] = c ^ 1
                    f(u, ans, c ^ 1)
    for i in range(1, n):
        if li[i] != [] and ans[i] == -1:
            ans[i] = 0
            f(i, ans, 0)
    print(*ans[1:], sep="\n")
solve()

Submission status: Runtime Error

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

branch = [0]*n

dist.sort()

for i in range(n):
    if dist[i][2] % 2 == 1:
        if branch[dist[i][0]-1] == 0:
            branch[dist[i][1]-1] == 1
        if branch[dist[i][0]-1] == 1:
            branch[dist[i][1]-1] == 0

for j in range(n):
    print(branch[j])

Submission status: Runtime Error

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

tree_dict = defaultdict(list)
for i in range(N-1):
    u, v, w = list(map(int, input().split()))
    tree_dict[u].append((v, w))
    tree_dict[v].append((u, w))
# print(tree_dict)


key=u

paint_dict = dict()

q = [key]
paint_dict[key] = 0

idx = 0
while idx < N:
    k = q[idx]
    #print("k", k, paint_dict[k])
    for to, w in tree_dict[k]:
        if to not in [i[0] for i in tree_dict[to]]:
            if (w + paint_dict[k]) % 2 == 0:
                paint_dict[to] = 0
            else:
                paint_dict[to] = 1
            #print(paint_dict[to], to, w)
            q.append(to)
    idx += 1
for i in range(N):
    print(paint_dict[i+1])

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

readline = sys.stdin.readline

N = int(readline())

nodes = [deque() for i in range(N)]
node_colors = [-1 for i in range(N)]

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


node_colors[0] = 0

def process(u, parent, w): 
    p_color = node_colors[parent]
    node_colors[u] = p_color if w % 2 == 0 else 1 - p_color
    
    for v, w in nodes[u]:
        if v == parent:
            continue
        process(v, u, w)

for v, w in nodes[0]:
    process(v, 0, w)

for c in node_colors:
    print(c)

Submission status: Runtime Error

Solution code:
n = int(input())
uvw = [[int(v) for v in input().split()] for _ in range(n-1)]

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

rinsetu = [[] for _ in range(n)]

for v in uvw:
  rinsetu[v[0]-1].append({
    "cost" : v[2],
    "to": v[1]-1
  })
  rinsetu[v[1]-1].append({
    "cost" : v[2],
    "to": v[0]-1
  })

def dps(i,n):
  for v in rinsetu[i]:
    to = v["to"]
    c = v["cost"]

    if color[to] != -1:
      continue
    
    color[to] = 1 if n % 2 == 0 else 0

    dps(to,n+c)


color[0] = 0

dps(0,0)
for v in color:
  print(v)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
w = np.zeros(shape = (N,N))
flag = N-1
while flag:
    l = input.split()
    i = int(l[0])-1
    j = int(l[1])-1
    w[i][j] = int(l[2])
    flag -= 1
color = np.ones(shape = (N,))
for i in range(N//2 + 1):
    for j in range(i+1,N):
        weight = w[i][j]
        if weight!=0 and weight % 2 == 0:
            color[j] = color[i]       
        else:
            color[j] = 0
for i in color:
    print(int(i))

Submission status: Runtime Error

Solution code:
n=int(input())
vertex=[i for i in range(n)]
uvw=[]
u,v,w=0,1,2
for i in range(n-1):
    ui,vi,wi=[int(x) for x in input().split()]
    uvw.append((ui-1,vi-1,wi))
uvw=sorted(uvw,key=lambda x :x[0])
# print(uvw)
for i in range(n-1):
    if uvw[i][w]%2==0:
        minNum=min(vertex[uvw[i][u]],vertex[uvw[i][v]])
        (vertex[uvw[i][u]],vertex[uvw[i][v]])=(minNum,minNum)
def find_root(k):
    if k==vertex[k]:
        return k
    else:
        return find_root(vertex[k])
for i in range(n):
    vertex[i]=find_root(i)
# print(vertex)
for i in range(n-1):
    if uvw[i][w]%2==1:
        min_root=min(vertex[uvw[i][u]],vertex[uvw[i][v]])
        max_root=max(vertex[uvw[i][u]],vertex[uvw[i][v]])
        vertex[max_root]=str(vertex[min_root])
def ans(k):
    if k==vertex[k]:
        return 0
    else:
        if type(vertex[k])==str:
            return ans(int(vertex[k]))^1
        else:
            return ans(vertex[k])
#print(vertex)
for i in range(n):
    print(ans(i))

Submission status: Runtime Error

Solution code:
N = int(input())
arr = [-1] * N
later = []
u, v, w = map(int, input().split())
u -= 1
v -= 1
if w % 2 == 0:
	arr[u] = 0
	arr[v] = 0
else:
	arr[u] = 0
	arr[v] = 1

for _ in range(N-2):
	u, v, w = map(int, input().split())
	u -= 1
	v -= 1
	if (arr[u] == -1) & (arr[v] == -1):
		later.append(u, v, w)
	elif arr[u] == -1:
		if w % 2 == 0:
			arr[u] = arr[v]
		elif arr[v] == 0:
			arr[u] = 1
		else:
			arr[u] = 0
	elif arr[v] == -1:
		if w % 2 == 0:
			arr[v] = arr[u]
		elif arr[u] == 0:
			arr[v] = 1
		else:
			arr[v] = 0

if len(later) > 0:
	for i in range(len(later)/3):
		u = later[3*i]
		v = later[3*i+1]
		w = later[3*i+2]
		if arr[u] == -1:
			if w % 2 == 0:
				arr[u] = arr[v]
			elif arr[v] == 0:
				arr[u] = 1
			else:
				arr[u] = 0
		elif arr[v] == -1:
			if w % 2 == 0:
				arr[v] = arr[u]
			elif arr[u] == 0:
				arr[v] = 1
			else:
				arr[v] = 0

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

Submission status: Runtime Error

Solution code:


def main():
    N = int(input())
    a = [0 for _ in range(N+1)]
    s = []
    for i in range(N-1):
        s.append(list(map(int, input().split())))
    s = sorted(s)
    # print(s)
    for y in s:
        u, v, w = tuple(y)
        a[v] = w
    for A in a[1:]:
        if a % 2 == 0:
            print(0)
        else:
            print(1)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
N=int(input())
root=[-1]*N
color=[-1]*N
from collections import defaultdict
tree=dict()
for i in range(N):
    tree[i]={i}
def search(i,cnt):
    if root[i]>=0:
        cnt+=1
        root[i]=search(root[i])
        return root[i],cnt
    return i,cnt
def makecolor(u,v):
    if color[u]==color[v]==-1:
        color[u]=0
        color[v]=1
    elif color[u]=-1:
        color[v]=(color[u]+1)%2
    elif color[v]=-1:
        color[u]=(color[v]+1)%2
    else:
        if color[u]==color[v]:
            for x in tree[v]:
                color[x]=(color[x]+1)%2
for i in range(N-1):
    u,v,w=map(int,input().split())
    if w%2==0:
        continue
    u-=1
    v-=1
    branch[u]|={v}
    branch[v]|={u}
    ru=search(u)
    rv=search(v)
    if ru==rv:
        continue
    
    if root[ru]<=root[rv]:
        root[rv]+=root[ru]
        root[ru]=rv
        makecolor(rv,ru)
        tree[rv]|=tree[ru]
    else:
        root[ru]+=root[rv]
        root[rv]=ru
        makecolor(ru,rv)
        tree[ru]|=tree[rv]
color=[i if i!=-1 else 1 for i in color]
for i in range(N):
    print(color[i])

Submission status: Runtime Error

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

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

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

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

Submission status: Runtime Error

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

import queue
ans=[-1 for i in range(n)]
ans[0]=0

fin=[]
q=queue.Queue()
q.put(1)

while  (set(fin)!=set(range(1,n+1))):
    r+=1
    p=q.get()
    fin.append(p)
    t=[i for i, x in enumerate(u) if x == p ]
    
    for i in t:

        if v[i] not in fin:
            q.put(v[i])
        if w[i]==0:
            ans[v[i]-1]=ans[p-1]
        else:
            if ans[p-1]==0:
                ans[v[i]-1]=1
            else:
                ans[v[i]-1]=0
        
    t=[i for i, x in enumerate(v) if x == p]

    for i in t:
        if u[i] not in fin:
            q.put(u[i])        
        if w[i]==0:
            ans[u[i]-1]=ans[p-1]
        else:
            if ans[p-1]==0:
                ans[u[i]-1]=1
            else:
                ans[u[i]-1]=0
    
    
for i in ans:
    print(i)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

list_ans = []
list_ans.append(True)
buf_0 = 0
buf_1 = 1
print(uvw)
for i in range(1,N):
    if list_ans[i-1] == 0:
        buf_0 = 0
        buf_1 = 1
    else:
        buf_0 = 1
        buf_1 = 1
    print(i)
    print(uvw[i])
    if (uvw[i][2] % 2) == 0:
        list_ans.append(buf_0)
    else:
        list_ans.append(buf_1)

Submission status: Runtime Error

Solution code:
import random
n = int(input())
listA = []
listB = []
listN=[]
stockL=[]
index=0
while index < n-1:
    u, v, w = map(int, input().split())
    if w %2== 1:
        listA.append(u)
        listB.append(v)
        listN.append(u)
        listN.append(v)
    else:
        stockL.append([u, v])
    index+=1

flag=1
while flag:
    flag=0
    index=0
    while index < len(stockL):
        u = stockL[index][0]
        v = stockL[index][1]
        if u in listA:
            if v in listB:
                flag=1
                vI = listB.index(v)
                uI = listA.index(u)
                if random.random() > 0.5:
                    listA[vI], listB[vI] = listB[vI], listA[vI]
                else:
                    listA[uI], listB[uI] = listB[uI], listA[uI]
            else:
                if v in listA:
                    pass
                else:
                    listA.append(v)
        elif u in listB:
            if v in listA:
                flag=1
                vI = listA.index(v)
                uI = listB.index(u)
                if random.random() > 0.5:
                    listA[vI], listB[vI] = listB[vI], listA[vI]
                else:
                    listA[uI], listB[uI] = listB[uI], listA[uI]
            else:
                if v in listB:
                    pass
                else:
                    listB.append(v)
        else:
            if v in listB:
                listB.append(u)
            elif v in listA:
                listA.append(u)
            else:
                pass
        index+=1
index2=1
while index2 < n+1:
    if index2 in listA:
        print(0)
    elif index2 in listB:
        print(1)
    else:
        print(0)
    index2+=1

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

n = int(input())
ans = [-1] * n
ans[0] = 0

edges = [[]]*n
for i in range(n-1):
    u,v,w = get_nums_l()
    edges[u-1].append([v-1,w%2==0])
    edges[v-1].append([u-1,w%2==0])

def aaa(x):
    # xを含むエッジ

    for edge in edges[x]:
        to = edge[0] # edgeのうちxじゃないほう
        if ans[to] != -1:
            continue
        same = edge[1]
        ans[to] = ans[x] ^ (not same)

        aaa(x)
aaa(0)
for annn in ans:
    print(annn)

Submission status: Runtime Error

Solution code:
# D問題
edge = {}
node = {}
n = int(input())
for i in range(n-1):
    u, v, w = map(int, input().split())
    if(w%2 == 0):
        # uとvは同グループ.初出はとにかくgroup_0に入れる.
        if(u in edge.keys()):
            edge[u][v] = 0
        else:
            edge[u] = {v:0}
        if(v in edge.keys()):
            edge[v][u] = 0
        else:
            edge[v] = {u:0}
    else:
        # uとvは別グループ
        if(u in edge.keys()):
            edge[u][v] = 1
        else:
            edge[u] = {v:1}
        if(v in edge.keys()):
            edge[v][u] = 1
        else:
            edge[v] = {u:1}

def define_node(node_from, node_to, color):
    if(node_to in node):
        # 決定済み
        return 0
    else:
        node[node_to] = ((node[node_from] + edge[node_from][node_to]) % 2)
        for i in edge[node_to].keys():
            define_node(node_to, i, node[node_to])
print(edge)
for i in edge[1].keys():
    node[1] = 0
    define_node(1, i, node[1])
for i in range(1, n+1):
    print(node[i])

Submission status: Runtime Error

Solution code:
N = int(input())
# white: 0, black: 1
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))

visited = [False]*N
def dfs(num, kyori): # 始点からの距離
    if not visited[num]:
        visited[num] = '1' if kyori%2 == 1 else '0'
        for hen in g[num]:
            dfs(hen[0], kyori+hen[1])
    return visited

print('\n'.join(dfs(0,0)))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from operator import itemgetter

class graph:
    def __init__(self,n):
        self.n = n
        self.con = []
        self.team = -1

    def connect(self,n,dist):
        self.con.append((n,dist))

    def is_edge(self):
        if(len(self.con)==1):
            return True
        else: return False

    def set_team(self,team):
        self.team = team

N = int(input())
uvw = [[int(x) for x in input().split()]for _ in range(N-1)]
li = []
edge = -1
for i in range(1,N+1):
    li.append(graph(i))
for i in uvw:
    li[i[0]-1].connect(i[1],i[2])
    li[i[1]-1].connect(i[0],i[2])
for i in li:
    if(i.is_edge()):
        edge=i.n
        break
before = edge
next = edge
li[edge-1].set_team(0)
for i in range(N):
    print(li[next-1].team)
    if(i==N-1):break
    if(li[next-1].con[0][0]==before):
        before = next
        next = li[next-1].con[1][0]
        li[next-1].set_team((li[before-1].team+li[before-1].con[1][1])%2)
    else:
        before = next
        next = next = li[next-1].con[0][0]
        li[next-1].set_team((li[before-1].team+li[before-1].con[0][1])%2)

Submission status: Runtime Error

Solution code:
#
# abc126 d
#


def main():
    n = int(input())
    tmp = []
    ans = [0] * n
    for i in range(n-1):
        tmp.append(list(map(int, input().split())))
    tmp = sorted(tmp)
    for i in range(len(tmp)):
        if tmp[i][0] == 1 and tmp[i][2] % 2 != 0:
            ans[tmp[i][2]-1] = 1
        if tmp[i][2] % 2 != 0 and ans[tmp[i][0]-1] == ans[tmp[i][1]-1]:
            if ans[tmp[i][0]-1] == 0:
                ans[tmp[i][1]-1] = 1
            else:
                ans[tmp[i][1]-1] = 0

    for i in range(n):
        print(ans[1])


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = int(input())

class UnionFind():
    def __init__(self, N):
        self.nodes = [i for i in range(N)]

    def root(self, x):
        if self.nodes[x] != x:
            self.nodes[x] = self.root(self.nodes[x])
        return self.nodes[x]

    def same(self, x, y):
        return self.root(x) == self.root(y)

    def unite(self, x, y):
        if self.same(x, y):
            return
        self.nodes[self.root(x)] = self.root(y)

tree = UnionFind(N)
anti_list = []
for _ in range(N-1):
    u, v, w = map(int, input().split())
    if w % 2 == 0:
        tree.unite(u-1, v-1)
    else:
        anti_list.append((u-1, v-1))

while len(anti_list) > 1:
    base_u, base_v = anti_list[0]
    new_anti_list = [(base_u, base_v)]
    for anti_u, anti_v in anti_list[1:]:
        u_root = tree.root(anti_u)
        v_root = tree.root(anti_v)
        if u_root == base_u:
            tree.unite(base_v, v_root)
        elif u_root == base_v:
            tree.unite(base_u, v_root)
        elif v_root == base_u:
            tree.unite(base_v, u_root)
        elif v_root == base_v:
            tree.unite(base_u, u_root)
        else:
            new_anti_list.append((u_root, v_root))
    anti_list = new_anti_list

for i in range(N):
    print(1 if tree.same(0, i) else 0)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# coding: utf-8

import sys
sys.setrecursionlimit(10000)

def paint(k, same, diff, visited, csame, cdiff):
    if k in visited:
        return
    if k in same:
        return
    csame.add(k)
    visited.add(k)
    if k in diff:
        for e in diff[k]:
            if e in visited:
                continue
            paint(e, same, diff, visited, cdiff, csame)
    if k in same:
        for e in same[k]:
            if e in visited:
                continue
            paint(e, same, diff, visited, csame, cdiff)

def main():
    n = int(input())
    same = {}
    diff = {}
    for _ in range(n-1):
        u, v, w = list(map(int, input().split()))
        if w % 2 == 0:
            same.setdefault(u, [])
            same.setdefault(v, [])
            same[u].append(v)
            same[v].append(u)
        else:
            diff.setdefault(u, [])
            diff.setdefault(v, [])
            diff[u].append(v)
            diff[v].append(u)
    white = set()
    black = set()
    visited = set()
    for k in diff:
        paint(k, same, diff, visited, white, black)
    ret = []
    for i in range(n):
        if i+1 in white:
            ret.append("0")
        else:
            ret.append("1")
    print("\n".join(ret))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
no = [0 for i in range(N)]
for i in range(N):
  u, v, w =  map(int, input().split())
  if w % 2 == 0:
    if no[u-1]==1:
      no[v-1] = 1
    else:
      no[v-1] = 0
  else:
    if no[u-1]==1:
      no[v-1] = 0
    else:
      no[v-1] = 1
for i in range(N):
  print(no[i])

Submission status: Runtime Error

Solution code:
import pandas as pd

N = int(input())

u = [0] * (N-1)
v = [0] * (N-1)
w = [0] * (N-1)
ans = [0] * N

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

df = pd.DataFrame({'u': u, 'v': v, 'w': w})
df_sorted = df.sort_values(['u', 'v'])

u = df_sorted['u'].values
v = df_sorted['v'].values
w = df_sorted['w'].values

for i in range(N-1):
    if w[i] % 2 == 1:
        if ans[int(u[i]-1)] == 0:
            ans[int(v[i]-1)] = 1
        if ans[int(u[i]-1)] == 1:
            ans[int(v[i]-1)] = 0

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import numpy as np

n,m=map(int,input().split())
E=[]
W=[]
node_l=np.zeros(n)
node_l[0]=1
for i in range(m):
    u,v,w=map(int,input().split())
    #print(u,v,w)
    E.append([u,v])
    W.append(w)
#print(E,W)

def bfs(node,node_l,l,w_l):
    i=0
    A=[]
    L=[]
    for edge in l:
        if edge[0]==node and node_l[edge[1]-1]==0:
            L.append(edge[1])
            A.append(i)
        elif edge[1]==node and node_l[edge[0]-1]==0:
            L.append(edge[0])
            A.append(i)
        i+=1
    if L==[]:
        return
    for j in range(len(L)):
        if W[A[j]]%2==0:
            node_l[L[j]-1]=node_l[node-1]
            bfs(L[j],node_l,l,w_l)
        else:
            node_l[L[j]-1]=node_l[node-1]*-1
            bfs(L[j],node_l,l,w_l)
    return
bfs(1,node_l,E,W)
for i in range(n):
    if node_l[i]==1:
        print('1',end='\n')
    else:
        print('0',end='\n')

Submission status: Runtime Error

Solution code:
from collections import deque


ans = {1:1}

def dfs(tree,nodes):
    for node in nodes:
        nexts = tree[node]
        for next,l in nexts.items():
            if l% 2 == 0 :
                ans[next] = ans[node]
            else:
                ans[next] = 1 - ans[node]
            tree[next].pop(node)

        dfs(tree,nexts)

    return 0




if __name__ == "__main__":
    tree= {}
    N = int(input())
    for i in range(N-1):
        u,v,w = list(map(int,input().split(" ")))

        if u not in tree:
            tree[u] = {}
        tree[u][v] = w

        if v not in tree:
            tree[v] = {}
        tree[v][u] = w

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

Submission status: Runtime Error

Solution code:
N = int(input())
# white: 0, black: 1
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))

visited = [False]*N
def dfs(num, kyori): # 始点からの距離
    if not visited[num]:
        visited[num] = '1' if kyori%2 == 1 else '0'
        for hen in g[num]:
            dfs(hen[0], kyori+hen[1])
    else:
        return visited
    return visited

print('\n'.join(dfs(0,0)))

Submission status: Runtime Error

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

UVW.sort(key = lambda x: (x[0], x[1]))
color = [2 for _ in range(N)]
color[0] = 0

def coloring(l):
  for i in range(len(l)):
    uvw = UVW[i]
    if uvw[2] % 2 == 0:
      if color[uvw[0] - 1] != 2:
        color[uvw[1] - 1] = color[uvw[0] - 1]
      elif color[uvw[1] - 1] != 2:
        color[uvw[0] - 1] = color[uvw[1] - 1]
    else:
      if color[uvw[0] - 1] != 2:
        color[uvw[1] - 1] = color[uvw[0] - 1] ^ 1
      elif color[uvw[1] - 1] != 2:
        color[uvw[0] - 1] = color[uvw[1] - 1] ^ 1

coloring(UVW)
tmp = []
cnt = 0
for i in range(N):
  if color[i] == 2:
    cnt += 1
    for uvw in UVW:
      if uvw[0] == i + 1 or uvw[1] == i + 1:
        tmp.append(uvw)
if cnt > 0:
  color[tmp[0][0] - 1] = 0
  coloring(tmp)

for c in color:
  print(c)

Submission status: Runtime Error

Solution code:
from sys import exit

N = int(input())

u = []
v = []
w = []

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

reached = []
distances = {}


def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default


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

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

    while True:
        if x in u:
            index = u.index(x)
            new_x = v[index]
            new_distance = distance + w[index]

            u.pop(index)
            v.pop(index)
            w.pop(index)

            search(new_x, new_distance)
        elif x in v:
            index = v.index(x)
            new_x = u[index]
            new_distance = distance + w[index]

            u.pop(index)
            v.pop(index)
            w.pop(index)

            search(new_x, new_distance)
        else:
            break


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:
N = int(input())
test_list = []
for i in range(N - 1):
    test_list.append(list(map(int, input().split())))

all_black = False
kisuichi = []

for i in test_list:
    # print('i', i)
    if i[2] % 2 != 0:
        pass
    else:
        kisuichi.append(i[1])

ichi = 0

for i in range(N):
    # print(i)
    if i == kisuichi[ichi]:
        ichi += 1
        print('0')
    else:
        print('1')

Submission status: Runtime Error

Solution code:
N = int(input())
W = []
for _ in range(N - 1):
    u, v, w = map(int, input().split())
    W.append([u - 1, v - 1, w])
color = [0] * N
tree = [[] for _ in range(N)]
for w in W:
    tree[w[0]].append([w[1], w[2]])
    tree[w[1]].append([w[0], w[2]])
start = N
for i in range(len(tree)):
    if len(tree[i]) < start:
        start = i


def rec(n, prev, w):
    if color[prev] == color[n]:
        if w % 2 == 1:
            color[n] = 1 if color[prev] == 0 else 0
    elif w % 2 == 0:
        color[n] = color[prev]
    for i in tree[n]:
        if i[0] == prev:
            continue
        else:
            rec(i[0], n, i[1])


rec(tree[start][0][0], start, tree[start][0][1])
for c in color:
    print(c)

Submission status: Runtime Error

Solution code:
n = int(input())

uvw = []

for i in range(n):
    uvw.append([int(x) for x in input().split()])
    
for i in range(n):
    print(0)