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:
from collections import deque
N=int(input())
uvw=[[0,0,0] for _ in range(N-1)]
for i in range(N-1):
  u,v,w=list(map(int,input().split()))
  uvw[i]=[u,v,w]

d=deque()
d.append(1)
visited=[None]*N
visited[0]=0
def dfs(d,visited):
  l1=[]
  l2=[]
  s=d.pop()
  tmp=[]
  for i in range(len(uvw)):
    if uvw[i][0]==s:
      l1.append(i)
    if uvw[i][1]==s:
      l2.append(i)
  if l1!=[]:
    for i in l1:
      d.append(uvw[i][1])
      visited[uvw[i][1]-1]=(uvw[i][2]+visited[s-1])%2
      tmp.append([uvw[i][0],uvw[i][1],uvw[i][2]])
  if l2!=[]:
    for j in l2:
      d.append(uvw[j][0])
      visited[uvw[j][0]-1]=(uvw[j][2]+visited[s-1])%2
      tmp.append([uvw[j][0],uvw[j][1],uvw[j][2]])
  for lst in tmp:
    uvw.remove(lst)
  if len(d)==0 or (None not in visited):
    return visited
  return dfs(d,visited)

ans=dfs(d,visited)
for i in range(N):
  print(ans[i])

Submission status: Runtime Error

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

uvw =sorted(uvw,key = lambda x:(x[0],x[1]))
tree={}
tree[uvw[0][0]]=0  

for i in range(n-1):
  # treeと一致しているやつ
  if uvw[i][0] in tree:
    left = uvw[i][1]
    right = uvw[i][0]
  else:
    left = uvw[i][0]
    right = uvw[i][1]    
      
  if uvw[i][2]%2==0:
    tree[left] = tree[right]
  else:
    tree[left] = abs(tree[right]-1) 

if sum(tree.values())==0:
  tree[uvw[0][0]]=1

for item in sorted(tree.items()):
  print(item[1])

Submission status: Runtime Error

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

uvw =sorted(uvw,key = lambda x:(x[0],x[1]))
tree={}
tree[uvw[0][0]]=0  

for i in range(n-1):
  # treeと一致しているやつ
  if uvw[i][0] in tree:
    left = uvw[i][1]
    right = uvw[i][0]
  else:
    left = uvw[i][0]
    right = uvw[i][1]    
      
  if uvw[i][2]%2==0:
    tree[left] = tree[right]
  else:
    tree[left] = abs(tree[right]-1) 

if sum(tree.values())==0:
  tree[1]=1

for item in sorted(tree.items()):
  print(item[1])

Submission status: Runtime Error

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

ans=[None]*(n+1)
check=[None]*(n+1)
check[1]=1


for i in range(1,n+1):
    for j in edge[i]:
        if ans[j[0]] is None:
            ans[j[0]]=check[i]*j[1]
            check[j[0]]=check[i]*j[1]
            
for i in ans[1:]:
    print((i+1)//2)

Submission status: Runtime Error

Solution code:
def dfs(v):
    for u, w in e[v]:
        if c[u] != -1:
            continue
        c[u] = (c[v] + w) % 2
        dfs(u)


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

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

dfs(0)

print(*c, sep='\n')

Submission status: Runtime Error

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

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

for u, v, w in uvw:
    u -= 1
    v -= 1
    g[u].append([v, w])
    g[v].append([u, w])


WHITE = 0
GRAY = 1
BLACK = 2

ds = [0] * n
colors = [WHITE] * n


def dfs(u):
    if colors[u] == BLACK:
        return

    colors[u] = GRAY

    for v, w in g[u]:
        if colors[v] == WHITE:
            ds[v] = ds[u] + w
            dfs(v)

    colors[u] = BLACK


dfs(0)

for d in ds:
    print(d % 2)

Submission status: Runtime Error

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

for i in range(n-1):
    s, g, d = map(int, input().split())
    edge[s-1].append((g, d%2))
    edge[g-1].append((s, d%2))

ans = [None for _ in range(n)]

def set_color(x, col):
    ans[x-1] = col
    print(ans, edge[x-1])
    for g, d in edge[x-1]:
        if ans[g-1] == None:
            set_color(g, d^col)

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

Submission status: Runtime Error

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

for i in range(n-1):
    s, g, d = map(int, input().split())
    edge[s-1].append((g, d%2))
    edge[g-1].append((s, d%2))

ans = [None for _ in range(n)]

def set_color(x, col):
    ans[x-1] = col
    print(ans, edge[x-1])
    for g, d in edge[x-1]:
        if ans[g-1] == None:
            set_color(g, d^col)

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

Submission status: Runtime Error

Solution code:
import networkx as nx
graph = nx.Graph() #無向グラフを作成
directed_graph = nx.DiGraph() #有向グラフを作成

n = int(input())
for i in range(n-1):
    tree = list(map(int, input().split()))
    graph.add_edge(tree[0], tree[1])
    graph.edges[tree[0], tree[1]]['range'] = tree[2]
for i in range(n):
    graph.nodes[i+1]['color'] = 0

print(0)
for i in range(1,n):
    path = nx.shortest_path(graph, source=1, target=i+1)
    graph.nodes[i+1]['color'] = sum([graph.edges[path[i], path[i+1]]['range'] for i in range(len(path)-1)])%2
    print(graph.nodes[i+1]['color'])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1000000)

global color 
global adlist
color = [0 for _ in range(100001)] 

def make_adlist(N, uvw):
  global adlist
  adlist = [[] for _ in range(N)]
  for u, v, w in uvw:
    adlist[u-1] += [[v-1, w]]
  return adlist 

def dfs(v, c, dis):
  global color
  color[v] = c
  for next_v, w in adlist[v]:
    if color[next_v] == c:
      return False
    if color[next_v] == 0 and (dis+w) % 2 != 0:
      return dfs(next_v, -c, dis+w)
    elif color[next_v] == 0 and (dis+w) % 2 == 0:
      return dfs(next_v, c, dis+w)
    elif color[next_v] == -c and (dis+w) % 2 == 0:
      return True 
    else:
      return False

  return True


def main():
  N = int(input())
  uvw = [list(map(int, input().split())) for _ in range(N-1)]
  for i in range(N):
    if color[i] == 0:
      dfs(i, 1, 0)
  for c in color[:N]:
    if c == 1:
      print(1)
    else:
      print(0)


if __name__ == "__main__":
  main()

Submission status: Runtime Error

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

BLACK = 1
WHITE = 0
UNKNOWN = -1

import sys
if n == 1:
    print(BLACK)
    sys.exit(0)

colors = [UNKNOWN] * (n + 1)

graph = [[] for _ in range(n + 1)]
for e in edges:
    graph[e[0]].append((e[1], e[2]))
    graph[e[1]].append((e[0], e[2]))

def dfs(graph, colors, current_point, current_color):
    colors[current_point] = current_color

    for next_point_info in graph[current_point]:
        if colors[next_point_info[0]] != UNKNOWN:
            continue
        if (next_point_info[1] % 2 == 0):
            next_color = current_color
        else:
            next_color = 1 - current_color
        dfs(graph, colors, next_point_info[0], next_color)

dfs(graph, colors, 1, BLACK)
for c in colors[1:]:
    print(c)

Submission status: Runtime Error

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

BLACK = 1
WHITE = 0
UNKNOWN = -1

if n == 1:
    print(BLACK)

colors = [UNKNOWN] * (n + 1)

graph = [[] for _ in range(n + 1)]
for e in edges:
    graph[e[0]].append((e[1], e[2]))
    graph[e[1]].append((e[0], e[2]))

def dfs(graph, colors, current_point, current_color):
    colors[current_point] = current_color

    for next_point_info in graph[current_point]:
        if colors[next_point_info[0]] != UNKNOWN:
            continue
        if (next_point_info[1] % 2 == 0):
            next_color = current_color
        else:
            next_color = 1 - current_color
        dfs(graph, colors, next_point_info[0], next_color)

dfs(graph, colors, 1, BLACK)
for c in colors[1:]:
    print(c)

Submission status: Runtime Error

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

graph = [[] for _ in range(n + 1)]
for e in edges:
    graph[e[0]].append((e[1], e[2]))
    graph[e[1]].append((e[0], e[2]))

BLACK = 1
WHITE = 0
UNKNOWN = -1
colors = [UNKNOWN] * (n + 1)

def dfs(graph, colors, current_point, current_color):
    colors[current_point] = current_color

    for next_point_info in graph[current_point]:
        if colors[next_point_info[0]] != UNKNOWN:
            continue
        if (next_point_info[1] % 2 == 0):
            next_color = current_color
        else:
            next_color = 1 - current_color
        dfs(graph, colors, next_point_info[0], next_color)

dfs(graph, colors, 1, BLACK)
for c in colors[1:]:
    print(c)

Submission status: Runtime Error

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

T = [[] for _ in range(N)]
for i in range(N-1):
    u, v = A[i][0] - 1, A[i][1] - 1
    d = A[i][2] % 2 
    T[u].append([v, d])
    T[v].append([u, d])

visited = [True] + [False] * (N-1)
ans = [0] * N
def find(i):
    if visited[i]:
        for t in T[i]:
            if visited[t[0]]:
                pass
            else:
                ans[t[0]] = t[1]
                visited[t[0]] = True
                find(t[0])

find(0)                
            
for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:
import sys
from collections import Counter, deque, defaultdict
from math import factorial
import heapq, bisect
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10*5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

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

G = [[] for i in range(n)]
count = 0
for edge in edges:
    if edge[2] == 1:
        G[edge[0]].append(edge[1])
        G[edge[1]].append(edge[0])
        count +=1 

for i in range(len(edges)):
    if edges[i][2] == 1:
        edges.pop(i)

def sort_edge(edges):
    edge1 = []
    edge2 = []
    for i in range(len(edges)):
        if len(G[edges[i][0]]) > 0 or len(G[edges[i][1]]) > 0:
            edge1.append(edges[i])
        else:
            edge2.append(edges[i])
    edges = edge1 + edge2
    return edges

edges = sort_edge(edges)

if count == 0:
    for i in range(n):
        print(0)
    sys.exit()


for i in range(len(edges)):
    if len(G[edges[0][0]]) != 0:
        G[edges[0][1]].append(G[edges[0][0]][0])
        G[G[edges[0][0]][0]].append(edges[0][1])
    else:
        G[edges[0][0]].append(G[edges[0][1]][0])
        G[G[edges[0][1]][0]].append(edges[0][0])
    edges.pop(0)
    edges = sort_edge(edges)

color = [-1]*n

def dfs(v,c):
    color[v] = c
    for v_next in G[v]:
        if color[v_next] == -1:
            dfs(v_next,(c+1)%2)

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

Submission status: Runtime Error

Solution code:
import sys
from collections import Counter, deque, defaultdict
from math import factorial
import heapq, bisect
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10*5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

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

G = [[] for i in range(n)]
count = 0
for edge in edges:
    if edge[2] == 1:
        G[edge[0]].append(edge[1])
        G[edge[1]].append(edge[0])
        count +=1 

if count == 0:
    for i in range(n):
        print(0)
    sys.exit()


for edge in edges:
    if edge[2] == 0:
        if len(G[edge[0]]) != 0:
            G[edge[1]].append(G[edge[0]][0])
            G[G[edge[0]][0]].append(edge[1])
        else:
            G[edge[0]].append(G[edge[1]][0])
            G[G[edge[1]][0]].append(edge[0])

color = [-1]*n

def dfs(v,c):
    color[v] = c
    for v_next in G[v]:
        if color[v_next] == -1:
            dfs(v_next,(c+1)%2)

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

Submission status: Runtime Error

Solution code:
n = int(input())
tree = [list(map(int, input().split())) for _ in range(n-1)]
color = [0] * (n+1)
color[1] = 1
#print(tree)
tree.sort(key=lambda x: x[1])
tree.sort(key=lambda x: x[0])
#print(tree)
i = 0
while i < n-1:
    if color[tree[i][0]] == 0 and color[tree[i][1]] == 0:
        tree.append(tree[i])
        n += 1
    elif color[tree[i][1]] == 0:
        if tree[i][2] % 2 == 0:
            color[tree[i][1]] = color[tree[i][0]]
        else:
            color[tree[i][1]] = color[tree[i][0]] * (-1)
    elif color[tree[i][0]] == 0:
        if tree[i][2] % 2 == 0:
            color[tree[i][0]] = color[tree[i][1]]
        else:
            color[tree[i][0]] = color[tree[i][1]] * (-1)
    i += 1
 
for i in range(1, n+1):
  if color[i] < 0:
    print(0)
  else:
  	print(1)

Submission status: Runtime Error

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

ans = [-1 for i in range(n+1)]
def ki(a,k,ans):

    if ans[a] == -1:
        ans[a] = k
        for x,y in path[a]:
            ki(x,k+y,ans)

ki(1,0,ans)

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

Submission status: Runtime Error

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

ans = [-1 for i in range(n+1)]
def ki(a,k,ans):
    if ans[a] == -1:
        ans[a] = k
        for x,y in path[a]:
            ki(x,k+y,ans)

ki(1,0,ans)

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

Submission status: Runtime Error

Solution code:
from collections import defaultdict
import queue

def getlist():
	return list(map(int, input().split()))

class Graph(object):
    def __init__(self):
        self.graph = defaultdict(list)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, a, b, w):
        self.graph[a].append([b, w])

    def get_nodes(self):
        return self.graph.keys()

class BFS(object):
    def __init__(self, graph, N):
        self.g = graph.graph
        self.dist = [0] * (N + 1)
        self.visit = [0] * (N + 1)
        self.Q = queue.Queue()
        self.Q.put(1)

        while not self.Q.empty():
            v = self.Q.get()
            for i, w in self.g[v]:
            	if self.visit[i] == 0:
            		self.dist[i] = self.dist[v] + w

            	self.Q.put(i)
            	self.visit[i] = 1

    def weight(self):
        return self.dist

N = int(input())
G = Graph()
for i in range(N - 1):
	u, v, w = getlist()
	G.add_edge(u, v, w)
XX = BFS(G, W, N)
ans = XX.weight()

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

Submission status: Runtime Error

Solution code:
n = int(input())

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

color = [-1] * n
dist = [0] * n

import queue
def dfs(v):
    color[v] = dist[v] % 2
    for next_v in g[v]:
        if color[next_v[0]] == -1 and next_v[1] != 0:
            dist[next_v[0]] = dist[v] + next_v[1]
            dfs(next_v[0])

# 0を始点として各点までの距離を求める
dfs(0)

for c in color:
    print(c)

Submission status: Runtime Error

Solution code:
n = int(input())

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

color = [-1] * n
dist = [0] * n

def dfs(v):
    for next_v in g[v]:
        if color[next_v[0]] == -1 and next_v[1] != 0:
            dist[next_v[0]] = dist[v] + next_v[1]
            color[next_v[0]] = dist[next_v[0]] % 2
            dfs(next_v[0])

# 0を始点として各点までの距離を求める
dfs(0)

for c in color:
    print(c)

Submission status: Runtime Error

Solution code:
5
2 5 2
2 3 10
1 3 8
3 4 2

Submission status: Runtime Error

Solution code:
n = int(input())

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

color = [-1] * n
dist = [0] * n

def dfs(v):
    for next_v in g[v]:
        if color[next_v[0]] == -1 and next_v[1] != 0:
            dist[next_v[0]] = dist[v] + next_v[1]
            color[next_v] = dist[next_v[0]] % 2
            dfs(next_v)

# 0を始点として各点までの距離を求める
dfs(0)

for c in color:
    print(c)

Submission status: Runtime Error

Solution code:
n = int(input())

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

color = [-1] * n

def dfs(v, cur = 0):
    color[v] = cur

    for next_v in range(n):
        if color[next_v] == -1:
            if g[v][next_v] != 0 and g[v][next_v] % 2 == 0:
                dfs(next_v, cur)
            else:
                dfs(next_v, 1 - cur)

dfs(0)

for c in color:
    print(c)

Submission status: Runtime Error

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


class UnionFind:
    __slots__ = ['id', 'sz']

    def __init__(self, n):
        self.id = [-1] * n
        self.sz = n

    def root(self, x):
        if self.id[x] < 0:
            return x
        else:
            self.id[x] = self.root(self.id[x])
            return self.id[x]

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

    def union(self, x, y):
        s1, s2 = self.root(x), self.root(y)
        if s1 != s2:
            if self.id[s1] <= self.id[s2]:
                self.id[s1] += self.id[s2]
                self.id[s2] = s1
            else:
                self.id[s2] += self.id[s1]
                self.id[s1] = s2
            self.sz -= 1
            return True
        return False


n = int(input())
uvw = [list(map(int, input().split())) for i in range(n-1)]
groop = [-1]*n
groop[0] = 1
l = [0, 1]
uf = UnionFind(n)
uvw.sort()
while uvw:

    nextlist = []
    for i in range(n-1):
        u, v, w = uvw[i]
        if groop[u-1] >= 0:
            if w % 2:
                groop[v-1] = l[l.index(groop[u-1])-1]
            else:
                groop[v-1] = groop[u-1]
        elif groop[v-1] >= 0:
            if w % 2:
                groop[u-1] = l[l.index(groop[v-1])-1]
            else:
                groop[u-1] = groop[v-1]
        else:
            nextlist.append((u, v, w))
    uvw = copy.deepcopy(nextlist)
for g in groop:
    print(1 if g else 0)

Submission status: Runtime Error

Solution code:
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

def dfs(x):
    for p in g[x]:
        # print(p[0])
        if color[p[0]] != -1:
            continue
        if p[1] % 2: #odd
            if color[x]: #pre=white
                color[p[0]] = 0
            else:
                color[p[0]] = 1
        else:
            if color[x]: #pre=white
                color[p[0]] = 1
            else:
                color[p[0]] = 0
        dfs(p[0])
n = inp()
g = [[] for i in range(n)]
#0:黒 1:白 -1:未訪問
color = [-1] * n

for i in range(n-1):
    u,v,w = inpl()
    g[u-1].append([v-1,w])
    g[v-1].append([u-1,w])
# print(g)
color[0] = 0
dfs(0)
for i in range(1,n):
    if color[i] != -1:
        continue
    dfs(i)
for i in range(n):
    print(color[i])

Submission status: Runtime Error

Solution code:
def clr(u):
    for x in G[u]:
        v,w = x[0],x[1]
        if hist[v]==0:
            if w%2==0:
                C[v] = C[u]
            else:
                C[v] = 1-C[u]
            hist[v]=1
            clr(v)      
N = int(input())
G = {i:[] for i in range(1,N+1)}
for _ in range(N-1):
    u,v,w = map(int,input().split())
    G[u].append((v,w))
    G[v].append((u,w))
C = [0 for i in range(N+1)]
hist = [0 for i in range(N+1)]
hist[1] = 1
clr(1)
for i in range(1,N+1):
    print(C[i])

Submission status: Runtime Error

Solution code:
def col(i):
    for x in G[i]:
        v,w = x[0],x[1]
        if hist[v]==0:
            if w%2==0:
                C[v]=C[i]
            else:
                C[v]=1-C[i]
            hist[v] = 1
            col(v)
N = int(input())
G = {i:[] for i in range(1,N+1)}
for _ in range(N-1):
    u,v,w = map(int,input().split())
    G[u].append((v,w))
    G[v].append((u,w))
C = {i:0 for i in range(1,N+1)}
hist = [0 for i in range(N+1)]
hist[1] = 1
col(1)
for i in range(1,N+1):
    print(C[i])

Submission status: Runtime Error

Solution code:
n = int(input())
edges = {}
for i in range(n-1):
  u, v, w = map(int, input().split())
  if u in edges.keys():
    edges[u].append([v, w])
  else:
    edges[u] = [[v, w]]
  if v in edges.keys():
    edges[v].append([u, w])
  else:
    edges[v] = [[u, w]]
    
colors = [0 for i in range(n + 1)]
def search(node, d):
  if colors[node] == 0:
    if d % 2 == 0:
      colors[node] += 1
    else:
      colors[node] -= 1
    for edge in edges[node]:
      if edge[0] != node:
        search(edge[0], d+edge[1])
search(1, 0)

for i in range(1, n+1):
  if colors[i] == 1:
    print(1)
  else:
    print(0)

Submission status: Runtime Error

Solution code:
# ABC126D - Even Relation
def dfs(v: int, p: int) -> None:
    for u, w in T[v]:
        if u != p:
            D[u] = D[v] + w
            dfs(u, v)


def main():
    global T, D
    N, *A = map(int, open(0).read().split())
    T = [[] for _ in range(N + 1)]
    for i in range(0, (N - 1) * 3, 3):
        v, u, w = A[i : i + 3]
        T[v] += [(u, w)]
        T[u] += [(v, w)]
    D = [0] * (N + 1)  # D[i] := distance from vertex 1 (root)
    dfs(1, -1)
    ans = [i % 2 for i in D[1:]]  # same parity of distance -> same group
    print(*ans, sep="\n")


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]"):
    res = [0 for i in range(N)]
    G = [[] for i in range(N)]
    for i, j, k in zip(u, v, w):
        # print(i, j, k)
        i -= 1
        j -= 1
        # print(i, j, k)
        G[i].append((j, k))
        G[j].append((i, k))

    # v を c に塗る。 p は v の親。
    def dfs(v, p, c):
        res[v] = c
        for e in G[v]:
            if e[0] == p:
                continue
            if e[1] & 1:
                dfs(e[0], v, 1 - c)
            else:
                dfs(e[0], v, c)

    dfs(0, -1, 1)

    for x in res:
        print(x)

    return


# Generated by 1.1.5 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:
def dfs(p,v,c):
    ans[v]=c
    for i in E[v]:
        if i[0]==p:
            continue
        if i[1]%2==1:
            dfs(v,i[0],1-c)
        else:
            dfs(v,i[0],c)
N= int(input())
ans = [100]*(N+1)
memo = {i:False for i in range(1,N+1)}
E = {i: []for i in range(1,N+1)}
for i in range(N-1):
    u,v,w = list(map(int,input().split()))
    E[v].append((u,w))
    E[u].append([v,w])
dfs(-1,1,1)
for i in range(1,N+1):
    print(ans[i])

Submission status: Runtime Error

Solution code:
class Edge:
    def __init__(self, u, v, w):
        self.u = u
        self.v = v
        self.w = w

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

for _ in range(n - 1):
    u, v, w_ = tuple([int(i) for i in input().split(' ')])
    u -= 1
    v -= 1
    g[u].append(Edge(v, u, w_))
    g[v].append(Edge(u, v, w_))

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

def dfs(i, p, c):
    ans[i] = c
    for en in g[i]:
        if en.u == p:
            continue
        if en.w % 2 == 0:
            dfs(en.u, i, c)
        else:
            dfs(en.u, i, c ^ 1)
import sys

sys.setrecursionlimit(10000)

dfs(0, -1, 0)

print('\n'.join([str(i) for i in ans]))

Submission status: Runtime Error

Solution code:
class Edge:
    def __init__(self, u, v, w):
        self.u = u
        self.v = v
        self.w = w

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

for _ in range(n - 1):
    u, v, w_ = tuple([int(i) for i in input().split(' ')])
    u -= 1
    v -= 1
    g[u].append(Edge(v, u, w_))
    g[v].append(Edge(u, v, w_))

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

def dfs(i, p, c):
    ans[i] = c
    for en in g[i]:
        if en.u == p:
            continue
        if en.w % 2 == 0:
            dfs(en.u, i, c)
        else:
            dfs(en.u, i, c ^ 1)

dfs(0, -1, 0)

print('\n'.join([str(i) for i in ans]))

Submission status: Runtime Error

Solution code:
N=int(input())
tree_dict={i:-1 for i in range(N)}
tree_map=[[] for i in range(N)]
start=set([i for i in range(N)])

edge=[]

#数字の若いほうから見ていくとする
for i in range(N-1):
    u,v,w = map(int, input().split())
    tree_map[u-1].append([v-1,w])

def fill(index, now_color, dist):
    for v,w in tree_map[index]:
        
        if tree_dict[v] == -1:
#             if w%2==0:
#                 tree_dict[v] = now_color
#                 fill(v, tree_dict[v], w)
#             else:
#                 tree_dict[v] = -1*now_color
#                 fill(v, tree_dict[v],w)
            tree_dict[v] = (dist+w)%2
            fill(v, tree_dict[v],w)

now_color=1
tree_dict[start]=now_color
fill(0,now_color, 0)

for i in tree_dict.values():
    if i==-1:
        print(0)
    else:
        print(1)

Submission status: Runtime Error

Solution code:
#21:30
n = int(input())
peer = [[] for _ in range(n)]
for _ in range(n-1):
  a,b,x = map(int,input().split())
  a -= 1
  b -= 1
  peer[a].append([b,x])
  peer[b].append([a,x])
ans = [-1 for _ in range(n)]
now = [0]
while now != []:
  last = now
  now = []
  for x in last:
    for yz in peer[x]:
      y = yz[0]
      z = yz[1]
      if ans[y] == -1:
        ans[y] = (ans[x] + z) % 2
        now.append(y)
  #print(now)
for i in range(n):
  print(ans[now])

Submission status: Runtime Error

Solution code:
#21:30
n = int(input())
peer = [[] for _ in range(n)]
for _ in range(n-1):
  a,b,x = map(int,input().split())
  a -= 1
  b -= 1
  peer[a].append([b,x])
  peer[b].append([a,x])
ans = [-1 for _ in range(n)]
now = [0]
while now != []:
  last = now
  now = []
  for x in last:
    for yz in peer[x]:
      y = yz[0]
      z = yz[1]
      if ans[y] == -1:
        ans[y] = (ans[x] + z) % 2
        now.append(y)
  #print(now)
for i in range(n):
  print(now[i])

Submission status: Runtime Error

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

ans = [-1]*N

def dfs(node, dist):
    for next_node, size in Tree[node]:
        if ans[next_node] == -1:
            ans[next_node] = ((dist+size)%2)
            dfs(next_node, dist+size)
dfs(0, 0)
print(*ans, sep='\n')

Submission status: Runtime Error

Solution code:
n=int(input())
a=[[] for i in range(n)]
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))
b=[None for i in range(n)]
def k(v,dis):
    b[v]=dis
    for next_v,w in a[v]:
        if b[next_v]!=None:
            continue
        if w%2==0:
            k(next_v,dis)
        else:
            k(next_v,1-dis)
k(0,0)
for i in range(n):
    print(b[i])

Submission status: Runtime Error

Solution code:
N = int(input())
graph = [[] for _ in range(N)]

for i in range(N-1):
    u, v, w = map(int, input().split())
    graph[u-1].append((v-1, w))
    graph[v-1].append((u-1, w))
# これによって木が定義できた
# print(graph)

flag = [None for _ in range(N)]  # まだどの頂点も探索していない


def k(v, distance):  # distanceは0or1
    flag[v] = distance  # 最初は0

    for next_v, w in graph[v]:
        if flag[next_v] == 1 or flag[next_v] == 0:  # 到達済み（親）であればcontinue
            continue
        if (w % 2) == 0:  # そのノードへの距離が偶数であればそのまま（最初は0なので）
            k(next_v, distance)
        else:
            k(next_v, 1-distance)


k(0, 0)  # 親を強制的に決めてよい, この場合は0のノードを親にしている
for i in range(N):
    print(int(flag[i]))

Submission status: Runtime Error

Solution code:
def k(v, distance):  # distanceは0or1
    flag[v] = distance  # 最初は0

    for next_v, w in graph[v]:
        if flag[next_v] == 1 or flag[next_v] == 0:  # 到達済み（親）であればcontinue
            continue
        if w % 2 == 0:  # そのノードへの距離が偶数であればそのまま（最初は0なので）
            k(next_v, distance)
        else:
            k(next_v, 1-distance)


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

for i in range(N-1):
    u, v, w = map(int, input().split())
    graph[u-1].append([v-1, w])
    graph[v-1].append([u-1, w])
# これによって木が定義できた
# print(graph)

flag = [None for _ in range(N)]  # まだどの頂点も探索していない

k(0, 0)  # 親を強制的に決めてよい, この場合は0のノードを親にしている
for i in range(N):
    print(int(flag[i]))

Submission status: Runtime Error

Solution code:
n=int(input())
a=[[] for i in range(n)]
for i in range(n-1):
    u,v,w=map(int,input().split())
    a[u-1].append((v-1,w%2))
    a[v-1].append((u-1,w%2))
b=[None for i in range(n)]
def k(v,dis):
    b[v]=dis
    for next_v,w in a[v]:
        if b[next_v]!=None:
            continue
        if w%2==0:
            k(next_v,dis)
        else:
            k(next_v,1-dis)
k(0,0)
for i in range(n):
    print(b[i])

Submission status: Runtime Error

Solution code:
n = int(input())
# 隣接リスト
link = [[] for i in range(n)]
for i in range(n-1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    w %= 2
    link[u].append((v,w))
    link[v].append((u,w))

ans = [0]*n
visited = [False for i in range(n)]

def dfs(now, prev):

    for next,w in link[now]:
        if next != prev:
            if visited[next]:
                continue
            else:
                visited[next] = True
                ans[next] = ans[now] + w
                dfs(next, now)
dfs(0,-1)
for i in ans:
    print(i)