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:
# D - Even Relation

import sys
sys.setrecursionlimit = 10**6

n = int(input())

# adjacency list
alist = [[] for _ in range(n)]

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

#print(alist)

dlist = [-1] * n

def dfs(node, par, dist):
    dlist[node] = dist%2
    for i, j in alist[node]:
        if i==par:
            continue
        else:
            dfs(i, node, dist+j)

dfs(0,0,0)

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

Submission status: Runtime Error

Solution code:
# D - Even Relation

n = int(input())

# adjacency list
alist = [[] for _ in range(n)]

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

#print(alist)

dlist = [-1] * n

def dfs(node, par, dist):
    dlist[node] = dist%2
    for i, j in alist[node]:
        if i==par:
            continue
        else:
            dfs(i, node, dist+j)

dfs(0,0,0)

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

Submission status: Runtime Error

Solution code:
N = int(input())
a = []
b = {}

for i in range(N-1):
    c = list(map(int, input().split()))
    a.append(c)
    if i == 0:
        if c[2] % 2 == 0:
            b[c[0]] = 0
            b[c[1]] = 0
        else:
            b[c[0]] = 0
            b[c[1]] = 1
    if i > 0:
        if c[0] in b:
            if c[2] % 2 == 0:
                b[c[1]] = b[c[0]]
            else:
                b[c[1]] = (b[c[0]]+1)%2
                
        elif c[1] in b:
            if c[2] % 2 == 0:
                b[c[0]] = b[c[1]]
            else:
                b[c[0]] = (b[c[1]]+1)%2

while True:
    for j in a:
        if j[1] in b and j[0] not in b:
            if j[2] % 2 == 0:
                b[j[0]] = b[j[1]]
            else:
                b[j[0]] = (b[c[1]]+1)%2

        elif j[0] in b and j[1] not in b:
            if j[2] % 2 == 0:
                b[j[1]] = b[j[0]]
            else:
                b[j[1]] = (b[c[0]]+1)%2

    if len(b) == N:
        break
    
for l in range(1,N+1):
    print(b[l])

Submission status: Runtime Error

Solution code:
N = int(input())
weight = []
V = [[] for _ in range(N)]
VER = 0
INDEX = 1

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

dist = [-1] * N

def dfs(v, d):
    cur = v
    dist[v] = d % 2
    for adj, index in V[cur]:
        if dist[adj] != -1: continue
        dfs(adj, d + weight[index])

dfs(0, 0)

for d in dist:
    print(d)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
V = [[] for _ in range(N)]
visited = [False for _ in range(N)]
ans = 0

for _ in range(M):
	x, y, z = map(int, input().split())
	V[x - 1].append(y - 1)
	V[y - 1].append(x - 1)
	
for i in range(N):
	if V[i] == []:
		visited[i] = True
		ans += 1
	
def dfs(cur):
	visited[cur] = True
	for adj in V[cur]:
		if visited[adj]: continue
		dfs(adj)
	
while not all(visited):
	cur = visited.index(False)
	visited[cur] = True
	dfs(cur)
	ans += 1
	
print(ans)

Submission status: Runtime Error

Solution code:
def coloring(v, p, vs, cs, color):
	cs[v] = color
	for e in vs[v]:
		if e[0] == p:
			continue
		nc = color if e[1] % 2 == 0 else 1 - color
		coloring(e[0], v, vs, cs, nc)

def resolve():
	n = int(input())
	vs = [[] for _ in range(n)]
	cs = [None]*n
	for i in range(n-1):
		u, v, w = map(int, input().split())
		vs[u-1].append([v-1, w])
		vs[v-1].append([u-1, w])
	coloring(0, None, vs, cs, 1)
	for i in cs:
		print(i)
resolve()

Submission status: Runtime Error

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

for _ in range(N-1):
    u,v,w=[int(x) for x in input().split()]
    if w&1:
        edge[u].append((v,-1))
        edge[v].append((u,-1))
    else:
        edge[u].append((v,1))
        edge[v].append((u,1))
    
color = [None]*(N+1)

def set_color(v,c):
    color[v]=c
    for x,sgn in edge[v]:
        if color[x] is None:
            set_color(x,sgn*c)
set_color(1,1)

for x in color[1:]:
    print((x+1)//2)

Submission status: Runtime Error

Solution code:
import copy

n= int(input())
G = [[] for i in range(n)] #G(i): 頂点iと隣接する頂点のlist
a = [0] * (n-1)
b = [0] * (n-1)
c = [0] * (n-1)
for i in range(n-1):
    a[i], b[i], c[i] = map(int, input().split())
    G[a[i]-1].append([b[i]-1, c[i]]) #入力は 1-indexed とする
    G[b[i]-1].append([a[i]-1, c[i]]) #無向グラフの場合

seen = [-1] * n #頂点iが訪問済みの場合 seen[i] = 1

def dfs(g, s, cur): #頂点sを始点としたDFS
    seen[s] =  cur % 2
    for i in g[s]:
        if seen[i[0]] == -1:
            dfs(g, i[0], cur+i[1])

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

Submission status: Runtime Error

Solution code:
n=int(input())
l=[-1]*n
p=[[] for _ in range(n)]
for i in range(n-1):
    ui,vi,wi=map(int,input().split())
    p[ui-1].append([vi,wi])
    p[vi-1].append([ui,wi])

def tr1(x,y):
    #print(x)
    #print(y)
    
    if l[x-1]!=-1:
        return
    l[x-1]=y
    for i in range(len(p[x-1])):
        tr1(p[x-1][i][0],(y+p[x-1][i][1])%2)
tr1(1,0)
for j in range(n):
    print(l[j])

Submission status: Runtime Error

Solution code:
N=int(input())
L=[[] for _ in range(N+1)]
for a in range(N-1):
    u,v,w=map(int,input().split())  #これで良いのか
    L[u].append((v,w))
    L[v].append((u,w))

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

def dfs(x,cur=0):
    global L
    global color

    color[x]=cur
    for next in L[x]:
        if color[next[0]]!=-1:
            continue
        if next[1]%2==0:
            dfs(next[0],cur)
        if next[1]%2==1:
            dfs(next[0],1-cur)

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

Submission status: Runtime Error

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

w = [u_v_w[i][2] for i in range(N-1)]

tree = [[] for i in range(N)]

for i in range(N-1):
    u = u_v_w[i][0]
    v = u_v_w[i][1]
    tree[u-1].append((v,w[i]))
    tree[v-1].append((u,w[i]))

ans = [0] * N
ans[0] = 1
def dfs(now):
    global ans
    for next in tree[now-1]:
        if ans[next[0]-1]:
            continue
        if next[1] % 2 == 0:
            ans[next[0]-1] = ans[now-1]
        else:
            ans[next[0]-1] = ans[now-1] * (-1)
        dfs(next[0])

dfs(1)
for i in range(len(ans)):
    if ans[i] == 1:
        print(0)
    else:
        print(1)

Submission status: Runtime Error

Solution code:
import collections, math, bisect

local = False
if local:
    file = open("input.txt", "r")
    import time

def inp():
    if local:
        return file.readline().rstrip()
    else:
        return input().rstrip()

def ints():
    return [int(_) for _ in inp().split()]


def dfs(vertex, colors, visited, cur_dist, connection):
    if cur_dist%2==0:
        colors[vertex] = "0"
    else:
        colors[vertex] = "1"

    visited.add(vertex)

    for nextVertex in connection[vertex]:
        if nextVertex not in visited:
            dfs(nextVertex, colors, visited, cur_dist+connection[vertex][nextVertex], connection)
    

if local:
    start=time.time()

N = int(inp())

connection = [dict() for i in range(N)]

for i in range(1, N):
    u,v,w = ints()

    connection[u-1][v-1] = w
    connection[v-1][u-1] = w

colors = ["0"]*N
visited = set()
dfs(0, colors, visited, 0, connection)

for c in colors:
    print(c)


if local:
    fin = (time.time()-start)*1000
    print("{:.2f}".format(fin) + "ms")

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)]

for edge in edges:#edge0=u,edge1=v,edge2=w
  u=edge[0]
  v=edge[1]
  w=edge[2]
  A[u-1].append([v-1,w])
  A[v-1].append([u-1,w])
#A[i-1]に頂点iと隣接する頂点とその距離を格納

#depth[i-1]=頂点１から頂点iへの距離
#頂点1への距離は0
depth = [-1] * (N)
depth[0] = 0
#depth[i] = 1から頂点dまでの距離(=depth[d]) + 頂点dからiの距離(=X[1]))で更新可能

def dfs(A,d):
    for X in A[d]:#X[0]:頂点X　X[1]:頂点dから頂点Xの距離
        if depth[X[0]] == -1:#距離が-1ならまだ行っていない
            depth[X[0]] = depth[d] + X[1]
            dfs(A,X[0])#次の隣の頂点へ

dfs(A,0)#頂点0からスタート
for d in depth:
    if d % 2 == 0:
        print(0)
    else:
        print(1)

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)]

for edge in edges:#edge0=u,edge1=v,edge2=w
  u=edge[0]
  v=edge[1]
  w=edge[2]
  A[u-1].append([v-1,w])
  A[v-1].append([u-1,w])
#A[i-1]に頂点iと隣接する頂点とその距離を格納

#depth[i-1]=頂点１から頂点iへの距離
#頂点1への距離は0
depth = [-1] * (N)
depth[1] = 0
#depth[i] = 1から頂点dまでの距離(=depth[d]) + 頂点dからiの距離(=X[1]))で更新可能

def dfs(A,d):
    for X in A[d]:#X[0]:頂点X　X[1]:頂点dから頂点Xの距離
        if depth[X[0]] == -1:#距離が-1ならまだ行っていない
            depth[X[0]] = depth[d] + X[1]
            dfs(A,X[0])#次の隣の頂点へ

dfs(A,0)#頂点0からスタート
for d in depth:
    if d % 2 == 0:
        print(0)
    else:
        print(1)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100**6)


class Edge():
	def __init__(self, v, w):
		self._v = v
		self._w = w
	def v(self):
		return self._v
	def w(self):
		return self._w

def dfs(v, g, colors, color = 0):
	colors[v] = color
	if not len(g[v]):
		return
	for nv in g[v]:
		if colors[nv.v()] != -1:
			continue
		nc = color if nv.w() % 2 == 0 else 1 - color
		dfs(nv.v(), g, colors, nc)

def resolve():
	n = int(input())
	colors = [-1]*n
	g = [list() for _ in range(n)]
	for _ in range(n-1):
		u, v, w = map(int, input().split())
		g[v-1].append(Edge(u-1, w))
		g[u-1].append(Edge(v-1, w))
	for i in range(n):
		if colors[i] != -1:
			continue
		dfs(i, g, colors)
	for c in colors:
		print(c)
resolve()

Submission status: Runtime Error

Solution code:
class Edge():
	def __init__(self, v, w):
		self._v = v
		self._w = w
	def v(self):
		return self._v
	def w(self):
		return self._w

def dfs(v, g, colors, color = 0):
	colors[v] = color
	if not len(g[v]):
		return
	for nv in g[v]:
		if colors[nv.v()] != -1:
			continue
		nc = color if nv.w() % 2 == 0 else 1 - color
		dfs(nv.v(), g, colors, nc)

def resolve():
	n = int(input())
	colors = [-1]*n
	g = [list() for _ in range(n)]
	for _ in range(n-1):
		u, v, w = map(int, input().split())
		g[v-1].append(Edge(u-1, w))
		g[u-1].append(Edge(v-1, w))
	for i in range(n):
		if colors[i] != -1:
			continue
		dfs(i, g, colors)
	for c in colors:
		print(c)
resolve()

Submission status: Runtime Error

Solution code:
class Edge():
	def __init__(self, v, w):
		self._v = v
		self._w = w
	def v(self):
		return self._v
	def w(self):
		return self._w

def dfs(v, g, colors, color = 0):
	colors[v] = color
	if not len(g[v]):
		return
	for nv in g[v]:
		if colors[nv.v()] != -1:
			continue
		nc = color if nv.w() % 2 == 0 else 1 - color
		dfs(nv.v(), g, colors, nc)

def resolve():
	n = int(input())
	colors = [-1]*n
	g = [list() for _ in range(n)]
	for _ in range(n-1):
		u, v, w = map(int, input().split())
		g[v-1].append(Edge(u-1, w))
		g[u-1].append(Edge(v-1, w))
	dfs(0, g, colors)
	for c in colors:
		print(c)
resolve()

Submission status: Runtime Error

Solution code:
def dfs(v, g, colors, color = 0):
	colors[v] = color
	if not len(g[v]):
		return
	for nv in g[v]:
		if colors[nv.v()] != -1:
			continue
		nc = color if nv.w() % 2 == 0 else 1 - color
		dfs(nv.v(), g, colors, nc)

def resolve():
	n = int(input())
	colors = [-1]*n
	g = [list() for _ in range(n)]
	for _ in range(n-1):
		u, v, w = map(int, input().split())
		g[v-1].append(Edge(u-1, w))
		g[u-1].append(Edge(v-1, w))
	dfs(0, g, colors)
	for c in colors:
		print(c)
resolve()

Submission status: Runtime Error

Solution code:
class Edge():
	def __init__(self, v, w):
		self._v = v
		self._w = w
	def v(self):
		return self._v
	def w(self):
		return self._w

def dfs(v, g, colors, color = 0):
	colors[v] = color
	for nv in g[v]:
		if colors[nv.v()] != -1:
			continue
		nc = color if nv.w() % 2 == 0 else 1 - color
		dfs(nv.v(), g, colors, nc)

def resolve():
	n = int(input())
	colors = [-1]*n
	g = [list() for _ in range(n)]
	for _ in range(n-1):
		u, v, w = map(int, input().split())
		g[v-1].append(Edge(u-1, w))
		g[u-1].append(Edge(v-1, w))
	dfs(0, g, colors)
	for c in colors:
		print(c)
resolve()

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
g = [[] for i in range(n)]

for i in range(n-1):
    u,v,w = na()
    g[u-1].append((v-1,w))
    g[v-1].append((u-1,w))

dp = [0]*n

def f(a,b,c):
    dp[b] = c
    for i,j in g[b]:
        if i != a:
            f(b,i,c+j)

f(0,0,0)

for k in dp:
    print(k%2)

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
g = [[] for i in range(n)]

for i in range(n-1):
    u,v,w = na()
    g[u-1].append((v-1,w))
    g[v-1].append((u-1,w))

dp = [0]*n

def f(a,b,c):
    dp[b] = c
    for i,j in g[b]:
        if i != a:
            f(b,i,c+j)

f(0,0,0)
print(dp)
for k in dp:
    print(k%2)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n = int(input())

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

chk = [0]*n
chk[0] = 1

def dfs(now, prev):
    global chk
    # 今いる頂点から行ける頂点を順に next に入れてループ
    for next in g[now]:
        if next[0] != prev:
          if next[1]%2 == 0:
            chk[next[0]] = chk[now]
          elif next[1]%2 == 1:
            chk[next[0]] = (chk[now]*-1)
          
          dfs(next[0], now)
        
dfs(0,0)
#print(chk)
chk = tuple(chk)
for i in chk:
  if i == 1:
    print(1)
  else:
    print(0)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n = int(input())

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

chk = [0]*n
chk[0] = 1

def dfs(now, prev):
    global chk
    # 今いる頂点から行ける頂点を順に next に入れてループ
    for next in g[now]:
        if next[0] != prev:
          if next[1]%2 == 0:
            chk[next[0]] = chk[now]
          elif next[1]%2 == 1:
            chk[next[0]] = (chk[now]*-1)
          
          dfs(next[0], now)
        
dfs(0,-1)
#print(chk)
chk = tuple(chk)
for i in chk:
  if i == 1:
    print(1)
  else:
    print(0)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n = int(input())

def find(x):
    if par[x] < 0:
        return x
    else:
        px = find(par[x])
        wei[x] += wei[par[x]]
        par[x] = px
        return px

def weight(x):
    find(x)
    return wei[x]

#w[y]=w[x]+wとなるようにxとyを併合
def unite(x,y,w):
    w += wei[x]-wei[y]
    x = find(x)
    y = find(y)
    
    if x == y:
        return False
    else:
        #sizeの大きいほうがx
        if par[x] > par[y]:
            x,y = y,x
            w = -w
        par[x] += par[y]
        par[y] = x
        wei[y] = w
        return True

def diff(x,y):
    return weight(y)-weight(x)

par = [-1]*n
wei = [0]*n
chk = []
for i in range(n-1):
  u,v,w = [int(i) for i in input().split()]
  chk_append = chk.append
  chk_append((u,v,w))
  
from operator import itemgetter
chk = sorted(chk,key=itemgetter(0))

for i in chk:
  unite(i[0]-1,i[1]-1,i[2])    

m = find(6)
for i in range(n):
  #print(diff(m,i),i+1)
  if diff(m,i)%2 == 0:
    print(0)
  else:
    print(1)

Submission status: Runtime Error

Solution code:
import sys, copy, math, heapq, bisect
from itertools import accumulate
from collections import deque, defaultdict, Counter
input = sys.stdin.readline # 文字列の入力のとき'/n'まで受け取るので注意!!!
sys.setrecursionlimit(500000)

N = int(input())
T = [defaultdict(int) for i in range(N)]
for i in range(N-1):
    u,v,w = map(int,input().split())
    T[u-1][v-1] = w
    T[v-1][u-1] = w
visited = [0]*N
ans = [0]*N
def dfs(now,col):
    visited[now] = 1
    for i in T[now].keys():
        if visited[i] == 1:
            continue
        if T[now][i]%2==1:
            ans[i] = col^1
            next =  = col^1
        else:
            ans[i] = col
        dfs(i,next)
dfs(0,0)
for i in range(N):
    print(ans[i])

Submission status: Runtime Error

Solution code:
def main():
    import numpy as np
    n = int(input())
    graph = np.ones((n,n))*-1
    come = np.ones(n)*-1
    for i in range(n-1):
        u,v,w = map(int,input().split())
        graph[u-1][v-1] = graph[v-1][u-1] = w%2
    que = [0]
    come[0] = 0
    while len(que)>0:
        s = que.pop(0)
        for i,nv in enumerate(graph[s]):
            if nv!=-1:
                if come[i]!=-1:
                    continue
                if nv==0:
                    come[i]=come[s]
                else:
                    come[i]=(come[s]+1)%2
                que.append(i)
    for i in range(n):
        print(str(int(come[i])))
            
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def main():
    import numpy as np
    n = int(input())
    graph = np.zeros((n,n))
    come = np.ones(n)*-1
    for i in range(n-1):
        u,v,w = map(int,input().split())
        graph[u-1][v-1] = graph[v-1][u-1] = w
    que = [0]
    come[0] = 0
    while len(que)>0:
        s = que.pop(0)
        for i,nv in enumerate(graph[s]):
            if nv!=0:
                if come[i]!=-1:
                    continue
                if nv%2==0:
                    come[i]=come[s]
                else:
                    come[i]=(come[s]+1)%2
                que.append(i)
    for i in range(n):
        print(int(come[i]))
            
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100**6)

def dfs(v, p, g, color, c = 0):
	color[v] = c
	for nv in g[v]:
		if nv[0] == p:
			continue
		nc = c if nv[1] % 2 == 0 else 1 - c
		dfs(nv[0], v, g, color, nc)

def resolve():
	n = int(input())
	color = [-1]*n
	g = [[] for _ in range(n)]
	for i in range(n-1):
		u, v, w = map(int, input().split())
		g[u-1].append([v-1, w])
		g[v-1].append([u-1, w])
	dfs(0, -1, g, color)
	for i in color:
		print(i)
resolve()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100**6)

def dfs(v, p, g, color, c = 0):
	color[v] = c
	for nv in g[v]:
		if nv[0] == p:
			continue
		nc = c if nv[1] % 2 == 0 else 1 - c
		dfs(nv[0], v, g, color, nc)

def resolve():
	n = int(input())
	color = [-1]*n
	g = [[] for _ in range(n)]
	for i in range(n-1):
		u, v, w = map(int, input().split())
		g[u-1].append([v-1, w])
		g[v-1].append([u-1, w])
	for v in range(n):
		dfs(v, -1, g, color)
	for i in color:
		print(i)
resolve()

Submission status: Runtime Error

Solution code:

def dfs(v, p, g, color, c = 0):
	color[v] = c
	for nv in g[v]:
		if nv[0] == p:
			continue
		nc = c if nv[1] % 2 == 0 else 1 - c
		dfs(nv[0], v, g, color, nc)

def resolve():
	n = int(input())
	color = [-1]*n
	g = [[] for _ in range(n)]
	for i in range(n-1):
		u, v, w = map(int, input().split())
		g[u-1].append([v-1, w])
		g[v-1].append([u-1, w])
	for v in range(n):
		dfs(v, -1, g, color)
	for i in color:
		print(i)
resolve()

Submission status: Runtime Error

Solution code:
def dfs(v, g, color, paint=0):
	color[v] = paint
	for i in range(len(g)):
		if i == v:
			continue
		if color[i] != -1:
			continue
		if g[v][i]:
			# np = paint if g[v][i] % 2 == 0 else 1 - paint
			dfs(i, g, color, paint)

def resolve():
	n = int(input())
	color = [-1]*n
	g = [[-1]*n for _ in range(n)]
	for _ in range(n-1):
		u, v, w = map(int, input().split())
		g[u-1][v-1] = w
		g[v-1][u-1] = w
	for v in range(n):
		if color[v] > -1:
			continue
		dfs(v, g, color)

	for i in color:
		print(i)

resolve()

Submission status: Runtime Error

Solution code:
def dfs(v, g, color, paint=0):
	color[v] = paint
	for i in range(len(g)):
		if i == v:
			continue
		if color[i] != -1:
			continue
		if g[v][i]:
			np = paint if g[v][i] % 2 == 0 else 1 - paint
			dfs(i, g, color, np)

def resolve():
	n = int(input())
	color = [-1]*n
	g = [[-1]*n for _ in range(n)]
	for _ in range(n-1):
		u, v, w = map(int, input().split())
		g[u-1][v-1] = w
		g[v-1][u-1] = w
	for v in range(n):
		if color[v] > -1:
			continue
		dfs(v, g, color)

	for i in color:
		print(i)

resolve()

Submission status: Runtime Error

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

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

edges = [[] for _ in range(n)]
for u, v, w in uvw:
    u-=1
    v-=1
    edges[u].append([v, w%2])
    edges[v].append([u, w%2])

res = [[] for _ in range(n)]
def dfs(v, p, c):
    res[v] = c
    for e in edges[v]:
        if e[0] == p: continue
        if e[1]%2==1: dfs(e[0], v, 1-c%2)
        else: dfs(e[0], v, c)

dfs(0, -1, 1)
print('\n'.join(list(map(str, res))))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys

def solve(N: int, u: "List[int]", v: "List[int]", w: "List[int]"):

    nodes = [1]*N
    e = sorted([(ui, vi, wi) for ui, vi, wi in zip(u, v, w)])
    for eu, ev, ew in e:
        if w%2 == 1:
            nodes[ev-1] = nodes[eu-1]*-1
        else:
            nodes[ev-1] = nodes[eu-1]
            
    for n in nodes:
        print((n==1)*1)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    u = [int()] * (N - 1)  # type: "List[int]"
    v = [int()] * (N - 1)  # type: "List[int]"
    w = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        u[i] = int(next(tokens))
        v[i] = int(next(tokens))
        w[i] = int(next(tokens))
    solve(N, u, v, w)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys

def solve(N: int, u: "List[int]", v: "List[int]", w: "List[int]"):

    nodes = [1]*N
    e = sorted([(ui, vi, wi) for ui, vi, wi in zip(u, v, w)])
    for eu, ev, ew in e:
        if w%2 == 1:
            nodes[ev-1] = nodes[eu-1]*-1
        else:
            nodes[ev-1] = nodes[eu-1]
            

    for n in nodes:
        print((n.color==1)*1)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    u = [int()] * (N - 1)  # type: "List[int]"
    v = [int()] * (N - 1)  # type: "List[int]"
    w = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        u[i] = int(next(tokens))
        v[i] = int(next(tokens))
        w[i] = int(next(tokens))
    solve(N, u, v, w)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
#頂点iのindexは[i-1]
tree = []
for i in range(N):
    tree.append([])

#Wが0なら偶数、1なら奇数
for i in range(N-1):
    u, v, w = map(int,input().split())
    tree[v-1].append([u-1,w % 2])
    tree[u-1].append([v-1,w % 2])
    
#print(tree)

    
color = []
for i in range(N):
    color.append(0)
    color[0] = 1
#白=-1、黒=1、無色=0

distance = 0

def poyo(x):
    global tree
    global color
    for i in tree[x]:#その頂点から行ける頂点を一覧
        if color[i[0]] == 0:#iもリスト([0]が行ける頂点、[1]が距離)
            if i[1] == 0:
                color[i[0]] = color[x]
            else:
                color[i[0]] = color[x] * (-1)
            
            poyo(i[0])


poyo(0)

for i in color:
    if i == -1:
        i = 0
        
    print(i)

Submission status: Runtime Error

Solution code:
class UnionFind:
    def __init__(self,n):
        super().__init__()
        self.par = [-1]*n
        self.rank = [0]*n
        self.tsize = [1]*n
    
    def root(self,x):
        if self.par[x] == -1:
            return x 
        else:
            self.par[x] = self.root(self.par[x])
            return self.par[x]

    def unite(self, x, y):
        x_r = self.root(x)
        y_r = self.root(y)
        
        if self.rank[x_r]>self.rank[y_r]:
            self.par[y_r] = x_r

        elif self.rank[x_r]<self.rank[y_r]:
            self.par[x_r] = y_r

        elif x_r != y_r:
            self.par[y_r] = x_r
            self.rank[x_r] += 1

        if x_r != y_r:
            size = self.tsize[x_r]+self.tsize[y_r]
            self.tsize[x_r] = size
            self.tsize[y_r] = size

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

    def size(self,x):
        return self.tsize[self.root(x)]

def main():

    n= tuple([int(t)for t in input().split()])
    uf = UnionFind(2*(n+1))
    
    for _ in range(n-1):
      x,y,w = tuple([int(t)for t in input().split()])
      if w%2==0:
        uf.unite(2*x,2*y)
        uf.unite(2*x+1,2*y+1)
      else:
        uf.unite(2*x+1,2*y)
        uf.unite(2*x,2*y+1)

    w = uf.root(0)
    for i in range(n):
      if w == ufroot(2*i):
        print(1)
      else:
        print(0)

if __name__ == "__main__":
    main()

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())
    u -= 1
    v -= 1
    G[u].append((v, w))
    G[v].append((u, w))

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


def dfs(pos, color):
    for e in G[pos]:
        if C[e[0]] != -1:
            continue
        # odd
        if e[1] & 1:
            c = 1 - color
            C[e[0]] = c
            dfs(e[0], c)
        # even
        else:
            c = color
            C[e[0]] = c
            dfs(e[0], c)


C[0] = 0
dfs(0, 0)
for i in range(N):
    print(C[i])

Submission status: Runtime Error

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

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


def dfs(pos, color):
    for e in G[pos]:
        if C[e[0]] != -1:
            continue
        # odd
        if e[1] & 1:
            c = 1 - color
            C[e[0]] = c
            dfs(e[0], c)
        # even
        else:
            c = color
            C[e[0]] = c
            dfs(e[0], c)


C[0] = 0
dfs(0, 0)
for i in range(N):
    print(C[i])

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

def dfs(v, p, c):
    color[v] = c
    for e in graph[v]:
        if e[0] == p: continue
        if e[1] % 2: dfs(e[0], v, 1-c)
        else: dfs(e[0], v, c)


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

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

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

Submission status: Runtime Error

Solution code:
def dfs(v, p, c):
    color[v] = c
    for e in graph[v]:
        if e[0] == p: continue
        if e[1] % 2: dfs(e[0], v, 1-c)
        else: dfs(e[0], v, c)
    return


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

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

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

Submission status: Runtime Error

Solution code:
N=int(input())
#頂点の色
colors=[0]*(N)
L=[[] for _ in range(N)]
for i in range(N-1):
  #u,vを長さwでつなぐ
  u,v,w=map(int, input().split())
  L[u-1].append((v-1,w,i))
  L[v-1].append((u-1,w,i))
  
def dfs(start_node, pre_node,is_even, ):
  for next_node,l,i in L[start_node]:
    if next_node==pre_node:
      continue
    if is_even:
      if l%2==0:
        colors[next_node]=0
      else:
        colors[next_node]=1
        is_even=False
    else:
      if l%2==0:
        colors[next_node]=1
      else:
        colors[next_node]=0
        is_even=True
    dfs(next_node, start_node, is_even)
    
dfs(0,-1,True)
for color in colors:
  print(color)

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline

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

A=[0]*n
def dfs(s,p=-1):
    for g in G[s]:
        if g[0]!=p:
            A[g[0]]=A[s]+g[1]
            dfs(g[0],s)

dfs(0)
for a in A:
    if a%2==0:
        print(0)
    else:
        print(1)

Submission status: Runtime Error

Solution code:
def dfs(u,D):
    for v,w in tree[u]:
        if d[v]==-1:
            d[v]=(D+w)%2
            dfs(v,D+w)

N=int(input())
d=[-1 for _ in range(N)]
d[0]=0
tree=[[] for _ in range(N)]

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

dfs(0,0)
for i in d:
    print(i%2)

Submission status: Runtime Error

Solution code:
def dfs(u,D):
    for v,w in tree[u]:
        if d[v]==-1:
            d[v]=(w+D)%2
            dfs(v,D+w)

N=int(input())
d=[-1 for _ in range(N)]
d[0]=0
tree=[[] for _ in range(N)]

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

dfs(0,0)
for i in d:
    print(i%2)