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: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
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 LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)


def main():
    n = I()
    aa = [LI() for _ in range(n-1)]
    e = collections.defaultdict(list)
    for a,b,c in aa:
        e[a].append((b,c))
        e[b].append((a,c))

    def search(s):
        d = collections.defaultdict(lambda: inf)
        d[s] = 0
        q = []
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        while len(q):
            k, u = heapq.heappop(q)
            if v[u]:
                continue
            v[u] = True

            for uv, ud in e[u]:
                if v[uv]:
                    continue
                vd = k + ud
                if d[uv] > vd:
                    d[uv] = vd
                    heapq.heappush(q, (vd, uv))

        return d
    d = search(1)
    rr = []
    for i in range(1,n+1):
        rr.append(d[i]%2)

    return '\n'.join(map(str, rr))


print(main())

Submission status: Accepted

Solution code:
import sys

n = int(input())
links = [set() for _ in [0] * n]
for line in sys.stdin:
    u, v, w = map(int, line.split())
    u -= 1
    v -= 1
    links[u].add((v, w))
    links[v].add((u, w))
ans = [-1] * n
q = [(0, 0, -1)]
while q:
    v, d, p = q.pop()
    if d % 2 == 0:
        ans[v] = 0
    else:
        ans[v] = 1
    for u, w in links[v]:
        if u == p:
            continue
        q.append((u, d + w, v))
print('\n'.join(map(str, ans)))

Submission status: Runtime Error

Solution code:
from collections import deque

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

colors = [-1 for _ in range(N)]
stack = deque()
stack.append(0)
colors[0] = 0

while stack:
    n = stack.pop()
    for _next, w in graph[n]:
        if colors[_next] != -1:
            continue
        colors[_next] = (colors[n] + w) % 2
        stack.append([_next])

for c in colors:
    print(c)

Submission status: Runtime Error

Solution code:
def dfs(node, color=0):
    colors[node] = color
    for next_n, weight in graph.get(node, set()):
        if colors[next_n] != -1:
            continue
        if weight % 2 == 0:
            color = colors[node]
        else:
            color = 1 if colors[node] == 0 else 0
        colors[next_n] = color
        dfs(next_n, color)


N = int(input())
if N == 1:
    u_v_w_l = []
else:
    u_v_w_l = [map(int, input().split()) for _ in range(N - 1)]

graph = {}
for u, v, w in u_v_w_l:
    u, v = u - 1, v - 1
    u_set = graph.get(u, set())
    u_set.add((v, w))
    v_set = graph.get(v, set())
    v_set.add((u, w))
    graph[u] = u_set
    graph[v] = v_set

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

if N == 1:
    colors[0] = 0
else:
    for n in range(N):
        if colors[n] != -1:
            continue
        dfs(n)

for c in colors:
    print(c)

Submission status: Runtime Error

Solution code:
def dfs(node, color=0):
    colors[node] = color
    for next_n, weight in graph.get(node, set()):
        if colors[next_n] != -1:
            continue
        if weight % 2 == 0:
            color = colors[node]
        else:
            color = 1 if colors[node] == 0 else 0
        colors[next_n] = color
        dfs(next_n, color)


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

graph = {}
for u, v, w in u_v_w_l:
    u, v = u - 1, v - 1
    u_set = graph.get(u, set())
    u_set.add((v, w))
    v_set = graph.get(v, set())
    v_set.add((u, w))
    graph[u] = u_set
    graph[v] = v_set

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

if N == 1:
    colors[0] = 0
else:
    for n in range(N):
        if colors[n] != -1:
            continue
        dfs(n)

for c in colors:
    print(c)

Submission status: Runtime Error

Solution code:
from collections import deque
N=int(input())
G=[[] for _ in range(N)]
G2=[[] for _ 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])

color=[-1]*N
que=deque([0])
color[0]=0

while que:
  x=que.popleft()
  for y, W in G[x]:
    if color[y]==-1:
      color[y]=(color[x]+1)%2
      que.append([y])
print(*color, sep="\n")

Submission status: Runtime Error

Solution code:
n = int(input())
a = [[] for i in range(n)]; b = [-1]*n; b[0] = 0
for i in range(n-1):
    u, v, w = map(int, input().split())
    a[u-1].append([v-1, w]); a[v-1].append([u-1, w])
def dfs(A, k):
    for i in A[k]:
        if b[i[0]] == -1:
            b[i[0]] = b[k]+i[1]
            dfs(A, i[0])
dfs(a, 0)
for i in b:
    print(0) if i%2 == 0 else print(1)

Submission status: Runtime Error

Solution code:
from collections import deque

N = int(input())


vlist = [[]] * N

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

ans = [-1] * N
q = deque()

ans[0] = 0
q.append(0)

while q:
    new = q.popleft()
    for node, weight in vlist[new]:
        if ans[node] != -1:
            continue
        ans[node] = (ans[new] + weight) % 2
        q.append(node)
    
for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
n = int(input())
p = []
for i in range(n-1):
    p.append(list(map(int,input().split())))
path = [[] for i in range(n+1)]
for i in range(n-1):
    path[p[i][0]].append((p[i][1],p[i][2]))
    path[p[i][1]].append((p[i][0],p[i][2]))

dist = [0 for _ in range(n+1)]
def dfs(u,par):
    for v,w in path[u]:
        if(v == par):
            continue
        dist[v] += dist[u] + w
        dfs(v,u)
dfs(1,-1)
ret  = [0]*n
for i,num in enumerate(dist):
    if(num%2 == dist[1]%2):
        ret[i-1] = 0
    else:
        ret[i-1] = 1
for i in ret:
    print(i)

Submission status: Runtime Error

Solution code:
n = int(input())
p = []
for i in range(n-1):
    p.append(list(map(int,input().split())))
path = [[] for i in range(n+1)]
for i in range(n-1):
    path[p[i][0]].append((p[i][1],p[i][2]))
    path[p[i][1]].append((p[i][0],p[i][2]))

dist = [0 for _ in range(n+1)]
def dfs(u,par):
    for v,w in path[u]:
        if(v == par):
            continue
        dist[v] += dist[u] + w
        dfs(v,u)
dfs(1,-1)
ret  = [-1 for _ in range(n)]
ret[1] = 0
for i,num in enumerate(dist):
    if(num%2 == dist[1]%2):
        ret[i-1] = 0
    else:
        ret[i-1] = 1
print(*ret,sep='\n')

Submission status: Runtime Error

Solution code:
n = int(input())
p = []
for i in range(n-1):
    p.append(list(map(int,input().split())))
path = [[] for i in range(n+1)]
for i in range(n-1):
    path[p[i][0]].append((p[i][1],p[i][2]))
    path[p[i][1]].append((p[i][0],p[i][2]))

dist = [0 for _ in range(n+1)]
def dfs(u,par):
    for v,w in path[u]:
        if(v == par):
            continue
        dist[v] += dist[u] + w
        dfs(v,u)
dfs(1,-1)
ret  = [-1 for _ in range(n+1)]
ret[1] = 0
for i,num in enumerate(dist):
    if(num%2 == dist[1]%2):
        ret[i] = 0
    else:
        ret[i] = 1
ret = ret[1:]
print(*ret,sep='\n')

Submission status: Runtime Error

Solution code:
N = int(input())
e = [[] for i in range(N)]
for i in range(1, N):
    u, v, w = map(int, input().split())
    e[u-1].append((v-1, w))
    e[v-1].append((u-1, w))
l = [-1 for i in range(N)]
def dfs(u, p, col):
    l[u] = col
    for v, w in e[u]:
        if v == p:
            continue
        if w % 2 == 0:
            dfs(v, u, col)
        else:
            dfs(v, u, 1-col)
    
dfs(0, -1, 0)
for col in l:
    print(col)

Submission status: Runtime Error

Solution code:
N = int(input())
e = [[] for i in range(N)]
for i in range(1, N):
    u, v, w = map(int, input().split())
    e[u-1].append(v-1)
    e[v-1].append(u-1)
l = [-1 for i in range(N)]
def dfs(u, p, col):
    l[u] = col
    for v in e[u]:
        if v != p:
            dfs(v, u, 1 - col)
    
dfs(0, -1, 0)
for col in l:
    print(col)

Submission status: Runtime Error

Solution code:
from collections import deque

n = int(input())

distance = [-1] * n

length = [[0] * n for _ in range(n)]
edge = [[] for _ in range(n)]


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

D = deque([0])
distance[0] = 0

while D:
    now = D.popleft()
    for i in edge[now]:
        if distance[i] == -1:
            distance[i] = (distance[now] + length[now][i]) % 2
            D.append(i)

for i in distance:
    print(i)

Submission status: Runtime Error

Solution code:
N=int(input())
G=[[0]*(N+1) for  _ in [0]*(N+1)]
res=[0]*(N+1)

for i in range(N-1):
    u,v,w=map(int,input().split())
    G[u][v]=w
    G[v][u]=w
    
    
stack=[]
stack.append(1)
res[1]=1
while stack:
    r=stack.pop()
    for k in range(N+1):
        if G[r][k]>0 and res[k]==0:
            stack.append(k)
            if G[r][k]%2:
                res[k]=-res[r]
            else:
                res[k]=res[r]
for i in res[1:]:
    print((i+1)//2)

Submission status: Runtime Error

Solution code:
class Graph():
	def __init__(self):
		self.adjacency_dict = {}
		self.weight = []

	def add_vartex(self, v):
		self.adjacency_dict[v] = []
		self.weight.append([])

	def add_edge(self, v1, v2, w):
		self.adjacency_dict[v1].append(v2)
		self.adjacency_dict[v2].append(v1)
		self.weight[v1].append(w)
		self.weight[v2].append(w)

	def get_vartexes(self):
		return self.adjacency_dict.keys()

	def get_edges(self, v):
		return self.adjacency_dict[v], self.weight[v]

	def printgraph(self):
		return print(self.adjacency_dict)

n = int(input())

res = []
g = Graph()
for i in range(n):
	res.appned(-1)
	g.add_vartex(i)

for i in range(n-1):
	u, v, w = map(int, input().strip().split())
	g.add_edge(u - 1, v - 1, w % 2)


q = []
res[0] = 0
q.append(0)
while(len(q) > 0):
	v = q.pop()
	u_l, w_l = get_edges(v)
	for u, w in zip(u_l, w_l):
		if res[u] != -1:
			continue
		res[u] = (res[v] + w) % 2
		q.appned(u)

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

Submission status: Runtime Error

Solution code:
class Graph():
	def __init__(self):
		self.adjacency_dict = {}
		self.weight = []

	def add_vartex(self, v):
		self.adjacency_dict[v] = []
		self.weight.append([])

	def add_edge(self, v1, v2, w):
		self.adjacency_dict[v1].append(v2)
		self.adjacency_dict[v2].append(v1)
		self.weight[v1].append(w)
		self.weight[v2].append(w)

	def get_vartexes(self):
		return self.adjacency_dict.keys()

    def get_edges(self, v):
        return self.adjacency_dict[v], self.weight[v]

    def printgraph(self):
    	return print(self.adjacency_dict)

n = int(input())

res = []
g = Graph()
for i in range(n):
	res.appned(-1)
	g.add_vartex(i)

for i in range(n-1):
	u, v, w = map(int, input().strip().split())
	g.add_edge(u - 1, v - 1, w % 2)


q = []
res[0] = 0
q.append(0)
while(len(q) > 0):
	v = q.pop()
	u_l, w_l = get_edges(v)
	for u, w = in zip(u_l, w_l):
		if res[u] != -1:
			continue
		res[u] = (res[v] + w) % 2
		q.appned(u)

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

Submission status: Runtime Error

Solution code:
class Graph():
	def __init__(self):
		self.adjacency_dict = {}
		self.weight = []

	def add_vartex(self, v):
		self.adjacency_dict[v] = []
		self.weight.append([])

	def add_edge(self, v1, v2, w):
		self.adjacency_dict[v1].append(v2)
		self.adjacency_dict[v2].append(v1)
		self.weight[v1].append(w)
		self.weight[v2].append(w)

	def get_vartexes(self):
		return self.adjacency_dict.keys()

    def get_edges(self, v):
        return self.adjacency_dict[v], self.weight[v]

    def printgraph(self):
    	return print(self.adjacency_dict)

n = int(input())

res = []
g = Graph()
for i in range(n):
	res.appned(-1)
	g.add_vartex(i)

for i in range(n-1):
	u, v, w = map(int, input().strip().split())
	g.add_edge(u - 1, v - 1, w % 2)


q = []
res[0] = 0
q.append(0)
while(len(q) > 0):
	v = q.pop()
	u_l, w_l = get_edges(v)
	for u, w = in zip(u_l, w_l):
		if res[u] != -1:
			continue
		res[u] = (res[v] + w) % 2
		q.appned(u)

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

Submission status: Runtime Error

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

stack = [0]
color = [-1]*N
color[0] = 0
while stack:
    v = stack.pop()
    for child, w in adj[v]:
        if color[child] < 0:
            color[child] = (color[v] + w) % 2

for i in color:
    print(color)

Submission status: Runtime Error

Solution code:
from collections import deque
n = int(input())
tree = [[] for _ in range(n)]
for _ in range(n-1):
    u, v, w = map(int, input().split())
    tree[u].append((v - 1, w))
    tree[v].append((u - 1, w))
q = deque()
q.append(0)
color = [-1] * n
color[0] = 0
while q:
    v = q.popleft()
    for child, w in tree[v]:
        if color[child] < 0:
            color[child] = (color[v] + w) % 2
            q.append(child)
for i in color:
    print(i)

Submission status: Runtime Error

Solution code:
n=int(input())
G=[[] for i in range(n)]
for i in range(n-1):
    a,b,w=map(int,input().split())
    a-=1;b-=1
    G[a].append((b,w))
    G[b].append((a,w))
C=[-1]*n 
C[0]=0
def dfs(x):
    for v,w in G[x]:
        if C[v]==-1:
            C[v]=1-C[x] if w%2==1 else C[x]
            dfs(v)

dfs(0)
#print(G)
for i in range(n):
    print(C[i])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**7)
N = int(input())

Graph = [[] for _ in range(N)]
dist  = []*(N)

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


def func(fromE,now,length):
	dist[now] = length
	
	for i in range(len(Graph[now])):
		target = Graph[now][i][0]
		lengt  = Graph[now][i][1]

		if target == fromE:
			continue

		func(now,target,dist[now]+lengt)

func(-1,0,0)

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

Submission status: Runtime Error

Solution code:
N = int(input())

Graph = [[] for _ in range(N+10)]
dist  = [10000000000000]*(N+10)

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

dist[0] = 0

def func(fromE,now,length):
	if fromE != -1:
		dist[now] = dist[fromE]+length
	for i in range(len(Graph[now])):
		target = Graph[now][i][0]
		lengt  = Graph[now][i][1]

		if target == fromE:
			continue

		func(now,target,lengt)

func(-1,0,0)

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

Submission status: Runtime Error

Solution code:
N = int(input())

Graph = [[] for _ in range(N)]
dist  = [10000000000000]*(N)

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

dist[0] = 0

def func(fromE,now,length):
	if fromE != -1:
		dist[now] = dist[fromE]+length
	for i in range(len(Graph[now])):
		target = Graph[now][i][0]
		lengt  = Graph[now][i][1]

		if target == fromE:
			continue

		func(now,target,lengt)

func(-1,0,0)

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

Submission status: Runtime Error

Solution code:
from collections import deque
n = int(input())
ad_ls = [[] for _ in range(n)]
for _ in range(n-1):
    a,b,w = map(int,input().split())
    ad_ls[a-1].append([b-1,w])
    ad_ls[b-1].append([a-1,w])

cost_ls = [0] * n
done_ls = [0] * n
def dfs(v,c):
    for new,cost in ad_ls[v]:
        if not done_ls[new]:
            cost_ls[new] = c+cost
            done_ls[new] = 1
            dfs(new,c+cost)

dfs(0,0)

color_ls = [0] * n
for i in range(n):
    color_ls[i] = cost_ls[i] % 2

for color in color_ls:
    print(color)

Submission status: Runtime Error

Solution code:
def f(x):
  for tmp in links[x]:
    if c[tmp[0]]<0:
      if tmp[1]%2==0:
        c[tmp[0]]=c[x]+0
      else:
        c[tmp[0]]=1-c[x]
      f(tmp[0])

N=int(input())
links=[[] for _ in range(N+1)]
for _ in range(N-1):
  u, v, w=map(int, input().split())
  links[u].append([v, w])
  links[v].append([u, w])
  
c=[-1]*(N+1)
c[1]=0
f(1)

print(*c[1:], sep='\n')

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 問題：https://atcoder.jp/contests/abc126/tasks/abc126_d


class Graph():

    def __init__(self):
        self.adjacency_dict = {}
    
    def add_vertex(self, v):
        self.adjacency_dict[v] = []

    def add_edge(self, v1, v2):
        self.adjacency_dict[v1].append(v2)
        self.adjacency_dict[v2].append(v1)

    def get_vertexes(self):
        return self.adjacency_dict.keys()

    def get_edges(self, v):
        return self.adjacency_dict[v]

    def print_graph(self):
        print(self.adjacency_dict)

import numpy as np
n = int(input())
p = np.full((n, n), 0)

res = []
g = Graph()
for i in range(n):
    res.append(-1)
    g.add_vertex(i)

for i in range(n-1):
    u, v, w = map(int, input().strip().split())
    g.add_edge(u-1, v-1)
    p[u-1][v-1] = w % 2
    p[v-1][u-1] = w % 2

q = []
res[0] = 0
q.append(0)
while(len(q) > 0):
    v = q.pop()
    for u in g.get_edges(v):
        if res[u] != -1:
            continue
        w = p[u][v]
        res[u] = (res[v] + w) % 2
        q.append(u)

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

Submission status: Runtime Error

Solution code:
# import itertools
# import math
# import sys
# import numpy as np

N = int(input())
# S = input()
# n, *a = map(int, open(0))
# H, W, K = map(int, input().split())
# A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# S = input()

# d = sorted(d, reverse=True, key=lambda x:x[0])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

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

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

# print(tree)

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

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

dfs(tree, 1)

# print(depth)

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

Submission status: Runtime Error

Solution code:
#二部グラフになっている時の色の塗り分けを聞いている
n=int(input())
import sys
if n==1:
  print(0)
  sys.exit()
es=[[] for i in range(n)]
for i in range(n-1):
  u,v,w=list(map(int,input().split()))
  es[u-1].append([v,w])
  es[v-1].append([u,w])

colors=[0 for i in range(n)]
#深さ優先探索　頂点vをcolorで塗ることを考える
def dfs(v,color):
  colors[v-1]=color
  for i in es[v-1]:
    if i[1]%2==0 and colors[i[0]-1]==0:
      dfs(i[0],color)
    elif i[1]%2==1 and colors[i[0]-1]==0:
      dfs(i[0],(-1)*color)
  return colors

    
a=dfs(1,-1)
for i in a:
  if i==-1:
    print(0)
  else:
    print(1)

Submission status: Runtime Error

Solution code:
#二部グラフになっている時の色の塗り分けを聞いている
import sys
if n==1:
  print(0)
  sys.exit()
  
n=int(input())
es=[[] for i in range(n)]
for i in range(n-1):
  u,v,w=list(map(int,input().split()))
  es[u-1].append([v,w])
  es[v-1].append([u,w])

colors=[0 for i in range(n)]
#深さ優先探索　頂点vをcolorで塗ることを考える
def dfs(v,color):
  colors[v-1]=color
  for i in es[v-1]:
    if i[1]%2==0 and colors[i[0]-1]==0:
      dfs(i[0],color)
    elif i[1]%2==1 and colors[i[0]-1]==0:
      dfs(i[0],(-1)*color)
  return colors

    
a=dfs(1,-1)
for i in a:
  if i==-1:
    print(0)
  else:
    print(1)

Submission status: Runtime Error

Solution code:
#二部グラフになっている時の色の塗り分けを聞いている
n=int(input())
es=[[] for i in range(n)]
for i in range(n-1):
  u,v,w=list(map(int,input().split()))
  es[u-1].append([v,w])
  es[v-1].append([u,w])
import sys
if n==1:
  print(0)
  sys.exit()
colors=[0 for i in range(n)]
#深さ優先探索　頂点vをcolorで塗ることを考える
def dfs(v,color):
  colors[v-1]=color
  for i in es[v-1]:
    if i[1]%2==0 and colors[i[0]-1]==0:
      dfs(i[0],color)
    elif i[1]%2==1 and colors[i[0]-1]==0:
      dfs(i[0],(-1)*color)
  return colors

    
a=dfs(1,-1)
for i in a:
  if i==-1:
    print(0)
  else:
    print(1)

Submission status: Runtime Error

Solution code:
#二部グラフになっている時の色の塗り分けを聞いている
n=int(input())
es=[[] for i in range(n)]
for i in range(n-1):
  u,v,w=list(map(int,input().split()))
  es[u-1].append([v,w])
  es[v-1].append([u,w])

colors=[0 for i in range(n)]
#深さ優先探索　頂点vをcolorで塗ることを考える
def dfs(v,color):
  colors[v-1]=color
  for i in es[v-1]:
    if i[1]%2==0 and colors[i[0]-1]==0:
      dfs(i[0],color)
    elif i[1]%2==1 and colors[i[0]-1]==0:
      dfs(i[0],(-1)*color)
  return colors

    
a=dfs(1,-1)
for i in a:
  if i==-1:
    print(0)
  else:
    print(1)

Submission status: Runtime Error

Solution code:
#二部グラフになっている時の色の塗り分けを聞いている
n=int(input())
es=[[] for i in range(n)]
for i in range(n-1):
  u,v,w=list(map(int,input().split()))
  es[u-1].append([v,w])
  es[v-1].append([u,w])
  
colors=[0 for i in range(n)]
#深さ優先探索　頂点vをcolorで塗ることを考える
def dfs(v,color):
  colors[v-1]=color
  for i in es[v-1]:
    if i[1]%2==0 and colors[i[0]-1]==0:
      colors[i[0]-1]=color
      return dfs(i[0],color)
    elif i[1]%2==1 and colors[i[0]-1]==0:
      colors[i[0]-1]=-color
      return dfs(i[0],(-1)*color)

dfs(1,1)
for i in colors:
  if i==-1:
    print(0)
  else:
    print(1)

Submission status: Runtime Error

Solution code:
#二部グラフになっている時の色の塗り分けを聞いている
n=int(input())
es=[[] for i in range(n)]
for i in range(n-1):
  u,v,w=list(map(int,input().split()))
  es[u-1].append([v,w])
  es[v-1].append([u,w])
  
colors=[0 for i in range(n)]
#深さ優先探索　頂点vをcolorで塗ることを考える
def dfs(v,color):
  colors[v-1]=color
  for i in es[v-1]:
    if i[1]%2==0 and colors[i[0]-1]==0:
      colors[i[0]-1]=color
      return dfs(i[0],color)
    elif i[1]%2==1 and colors[i[0]-1]==0:
      colors[i[0]-1]=-color
      return dfs(i[0],(-1)*color)

dfs(1,1)
print(colors)
for i in colors:
  if i==-1:
    print(0)
  else:
    print(1)

Submission status: Runtime Error

Solution code:
n = int(input())

tree_list = [-1]*n
info_list = []
for _ in range(n-1):
  info_list.append(list(map(int, input().split())))



def _color(t_list, i):
  if i==n-1:
    return True
  u = info_list[i][0]
  v = info_list[i][1]
  w = info_list[i][2]
  if t_list[u-1]!=-1 and t_list[v-1]!=-1 and t_list[u-1]!=t_list[v-1]:
    return False
  if t_list[u-1]!=-1 and t_list[u-1]==t_list[v-1]:
    if _color(t_list, i+1):
      return True
  if t_list[u-1]==-1 and t_list[v-1]!=-1:
    u,v = v,u
  if t_list[u-1]!=-1 and t_list[v-1]==-1:
    if w%2==0:
      t_list[v-1]=t_list[u-1]
      if _color(t_list, i+1):
        return True
    elif t_list[u-1]==0:
      t_list[v-1]=1
      if _color(t_list, i+1):
        return True
    else:
      t_list[v-1]=0
      if _color(t_list, i+1):
        return True
  if w%2==0:
    t_list[u-1]=0
    t_list[v-1]=0
    if _color(t_list, i+1):
      return True
    t_list[u-1]=1
    t_list[v-1]=1
    if _color(t_list, i+1):
      return True
    
  t_list[u-1]=0
  t_list[v-1]=1
  if _color(t_list, i+1):
    return True
  
  t_list[u-1]=1
  t_list[v-1]=0
  if _color(t_list, i+1):
    return True
  
  return False

_color(tree_list, 0)
for t in tree_list:
  print(t)

Submission status: Runtime Error

Solution code:
n = int(input())

tree_list = [-1]*n
info_list = []
for _ in range(n-1):
  info_list.append(list(map(int, input().split())))



def _color(t_list, i):
  if i==n-1:
    return True
  u = info_list[i][0]
  v = info_list[i][1]
  w = info_list[i][2]
  print(u,v,w,t_list, t_list[u-1], t_list[v-1])
  if t_list[u-1]!=-1 and t_list[v-1]!=-1 and t_list[u-1]!=t_list[v-1]:
    return False
  if t_list[u-1]!=-1 and t_list[u-1]==t_list[v-1]:
    if _color(t_list, i+1):
      return True
  if t_list[u-1]==-1 and t_list[v-1]!=-1:
    u,v = v,u
  if t_list[u-1]!=-1 and t_list[v-1]==-1:
    if w%2==0:
      t_list[v-1]=t_list[u-1]
      if _color(t_list, i+1):
        return True
    elif t_list[u-1]==0:
      t_list[v-1]=1
      if _color(t_list, i+1):
        return True
    else:
      t_list[v-1]=0
      if _color(t_list, i+1):
        return True
  if w%2==0:
    t_list[u-1]=0
    t_list[v-1]=0
    if _color(t_list, i+1):
      return True
    t_list[u-1]=1
    t_list[v-1]=1
    if _color(t_list, i+1):
      return True
    
  t_list[u-1]=0
  t_list[v-1]=1
  if _color(t_list, i+1):
    return True
  
  t_list[u-1]=1
  t_list[v-1]=0
  if _color(t_list, i+1):
    return True
  
  return False

_color(tree_list, 0)
for t in tree_list:
  print(t)

Submission status: Runtime Error

Solution code:
N = int(input())
u, v, w = [0]*(N-1), [0]*(N-1), [0]*(N-1)
n = [-1] * N
# n[][0] 色　n[][1] 連結先一覧リスト n[][2] mod2 
for i in range(N):
    n[i] = [0]*4
    n[i][1] = []
    n[i][2] = []

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

# n[][3] 探索済フラグ
def dfs(num):
    n[num][3] = 1
    for i in range(len(n[num][1])):
        if n[n[num][1][i]][3] == 0:
            if n[num][2][i] == 0:
                n[n[num][1][i]][0] = n[num][0]
                dfs(n[num][1][i])
            else:
                n[n[num][1][i]][0] = -1*n[num][0] + 1
                dfs(n[num][1][i]) 

dfs(u[0]-1)

for i in n:
    print(i[0])

Submission status: Runtime Error

Solution code:
N = int(input())
u, v, w = [0]*(N-1), [0]*(N-1), [0]*(N-1)
n = [-1] * N 
# n[][0] 色　n[][1] 連結先一覧リスト(連結先奇数なら+ 偶数なら-がかかる1追加された値)
for i in range(N):
    n[i] = [0]*3
    n[i][1] = []


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

# n[][2] 探索済フラグ
def dfs(num):
    n[num][2] = 1
    for item in n[num][1]:
        if n[abs(item)-1][2] == 0:
            if item < 0:
                item = -1*item
                n[item-1][0] = n[num][0]
                dfs(item-1)
            else:
                n[item-1][0] = -1 * n[num][0] +1
                dfs(item-1)

dfs(u[0]-1)

for i in n:
    print(i[0])

Submission status: Runtime Error

Solution code:
N = int(input())
u, v, w = [0]*(N-1), [0]*(N-1), [0]*(N-1)
n = [-1] * N 
# n[][0] 色　n[][1] 連結先一覧リスト(連結先奇数なら+ 偶数なら-がかかる1追加された値)
for i in range(N):
    n[i] = [0]*3
    n[i][1] = []


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

# n[][2] 探索済フラグ
def dfs(num):
    n[num][2] = 1
    for item in n[num][1]:
        if n[abs(item)-1][2] == 0:
            if item < 0:
                item = -1*item
                n[item-1][0] = n[num][0]
                dfs(item-1)
            else:
                n[item-1][0] = -1 * n[num][0] +1
                dfs(item-1)

dfs(0)

for i in n:
    print(i[0])

Submission status: Runtime Error

Solution code:
N = int(input())
u, v, w = [0]*(N-1), [0]*(N-1), [0]*(N-1)
n = [-1] * N 
# n[][0] 色　n[][1] 連結先一覧リスト(連結先奇数なら+ 偶数なら-がかかる1追加された値)
for i in range(N):
    n[i] = [0]*2
    n[i][1] = []

visited = []

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

def dfs(num):
    visited.append(num)
    for item in n[num][1]:
        if not (abs(item)-1 in visited):
            if item < 0:
                item = -1*item
                n[item-1][0] = n[num][0]
                dfs(item-1)
            else:
                n[item-1][0] = -1 * n[num][0] +1
                dfs(item-1)

dfs(0)

for i in n:
    print(i[0])

Submission status: Runtime Error

Solution code:
def dfs(now, prev):
    for nxt, w in edge[now]:
        if nxt == prev:
            continue
        
        dist[nxt] = dist[now] + w
        dfs(nxt, now)


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

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

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

Submission status: Runtime Error

Solution code:
from collections import defaultdict
adj = defaultdict(list)
dis =[0 for i in range(1000000)]
def dfs(u , p):
	for i , w in adj[u]:
		if i==p:
			continue
		dis[i]=dis[u]+w
		dis[i]=dis[i]%2
		dfs(i , u)
def main():
	n = int(input())
	for i in range(n-1):
		x, y , w = map(int , input().split())
		w = w%2
		adj[x].append([y , w])
		adj[y].append([x, w])
	dfs(1, 0)
	for i in range(1,n+1):
		print(dis[i])
if __name__ =="__main__":
	main()

Submission status: Runtime Error

Solution code:
n=int(input())
path=dict()
for i in range(n-1):
  u,v,w=map(int,input().split())
  if u-1 in path:
    path[u-1].append([v-1,w])
  else:
    path[u-1] = [[v-1,w]]
  if v-1 in path:
    path[v-1].append([u-1,w])
  else:
    path[v-1] = [[u-1,w]]

color = [-1 for i in range(n)]
def dfs(bef,pos,v):
  if color[pos]>-1:
    return
  if bef:
  	color[pos] = ~(v%2)
  else:
    color[pos] = v%2
  #print(pos,color[pos])
  for nex, dist in path[pos]:
    dfs(color[pos],nex,v+dist)
  return
    
dfs(0,0,0)
for i in range(n):
  print(color[i])

Submission status: Runtime Error

Solution code:
#coding: utf-8
from collections import defaultdict

N = int(input())
G = {}
for i in range(1, N+1):
    G[i] = []

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

ret = [-1] * (N)

def dfs(node, d):
    if ret[node-1] != -1:
        return

    if d % 2 == 0:
        ret[node-1] = 0
    else:
        ret[node-1] = 1 

    for next_node in G[node]:
        dfs(next_node[0], d + next_node[1])

dfs(1, 0)
for i in range(N):
    print(ret[i])