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:
#!/usr/bin/env python3

def main():
    n = int(input())
    adjs = [[] for i in range(n)]
    for i in range(n - 1):
        u, v, w = map(int, input().split())
        adjs[u - 1].append((v - 1, w))
        adjs[v - 1].append((u - 1, w))
    colors = [None for i in range(n)]
    visited = [False for i in range(n)]
    dfs(0, 0, adjs, visited, colors)
    for i in range(n):
        print(colors[i])

def dfs(i, color, adjs, visited, colors):
    visited[i] = True
    colors[i] = color
    for v, w in adjs[i]:
        if visited[v]:
            continue
        dfs(v, (color + w) % 2, adjs, visited, colors)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
N = int(input())

def dfs(v, u, w):
    if w % 2 == 0:
        iro[v] = 0
    else:
        iro[v] = 1
    for i in ki[v]:
        if i[1] == u: continue
        dfs(i[1], v, w + i[0])
    return

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

iro = [0] * N
dfs(0, 0, 0)
for i in iro:
    print(i)

Submission status: Runtime Error

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

def dfs(v, dist):
  for nv, e_dist in G[v]:
    if ans[nv] == -1:
      ans[nv] = (dist + e_dist) % 2
      dfs(nv, dist + e_dist)

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

ans = [-1] * N
dfs(0, 0)

print(*ans, sep="\n")

Submission status: Runtime Error

Solution code:
from collections import deque
n=int(input())
X=[[] for _ in range(n)]
for i in range(n-1):
    u,v,w=map(int,input().split())
    X[u-1].append([v-1,w])
    X[v-1].append([u-1,w])
C=[0]*n
q=deque([])
for i in range(len(X[0])):
    q.append([X[0][i]])
visited=[0]*n

while q:
    v,w=q.popleft()
    if visited[v]==1:
        continue
    visited[v]=1
    if w%2==0:
        C[v]=C[u]
    else:
        if C[u]==0:
            C[v]=1
        else:
            C[v]=0
    for pv,pw in X[v]:
        q.append([pv,pw])

for c in C:
    print(c)

Submission status: Runtime Error

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

for _ in range(n-1):
    x,y,z = map(int, input().split())
    if z % 2 == 0:
        ge[x-1].append(y-1)
        ge[y-1].append(x-1)
    else:
        go[x-1].append(y-1)
        go[y-1].append(x-1)

color = [0] * n

def dfs(v, c):
    color[v] = c
    for i in range(len(go[v])):
        if color[go[v][i]] == 0:
            dfs(go[v][i], -c)
    for i in range(len(ge[v])):
        if color[ge[v][i]] == 0:
            dfs(ge[v][i], c)

dfs(0, 1)

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

Submission status: Runtime Error

Solution code:
N = int(input())
edge = [[] for _ in range(N+1)]
for i in range(N-1):
  u,v,w = map(int, input().split())
  edge[u].append([v,w%2])
  edge[v].append([u,w%2])
color = [0]*(N+1)
visited = [False]*(N+1)
def dfs(u):
  for v,w in edge[u]:
    if visited[v]==False:
      #print(v)
      visited[v]=True
      color[v]=color[u]^w
      dfs(v)
  return 0
visited[1]=True
dfs(1)
ans = color[1:]
print(*ans,sep='\n')

Submission status: Runtime Error

Solution code:
N = int(input())
edge = [[] for _ in range(N+1)]
for i in range(N-1):
  u,v,w = map(int, input().split())
  edge[u].append([v,w%2])
  edge[v].append([u,w%2])
color = [0]*(N+1)
visited = [False]*(N+1)
def dfs(u):
  for v,w in edge[u]:
    if visited[v]==False:
      visited[v]=True
      color[v]=color[u]^w
      dfs(v)
  return 0
dfs(1)
ans = color[1:]
print(*ans,sep='\n')

Submission status: Runtime Error

Solution code:
n=int(input())
tree=[[]for _ in range(n+1)]
for _ in range(n-1):
    u,v,w=map(int,input().split())
    tree[u].append([v,w])
    tree[v].append([u,w])
d=[-1]*(n+1)
d[1]=0
q=[1]
while q:
    now=q.pop()
    for n_i,n_l in tree[now]:
        if d[n_i]==-1:
            d[n_i]=d[now]+[n_l]
            q.append(n_i)
for l in d[1:]:
    print(l%2)

Submission status: Runtime Error

Solution code:
def dfs(i):
    if all([v != -1 for v in visited]):
        return
    color = visited[i]
    for nextIdx, wgt in graph[i]:
        if visited[nextIdx] == -1:
            if wgt % 2 == 0:
                visited[nextIdx] = color
            else:
                visited[nextIdx] = int(not color)
            dfs(nextIdx)

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

visited = [-1] * n
visited[0] = 0
dfs(0)
for v in visited:
    print(v)

Submission status: Runtime Error

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

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

ret = [0] * N
def dfs(pv, v, dist):
    ret[v] = dist
    for nv, w in edge[v]:
        if nv == pv:
            continue
        dfs(v, nv, (dist+w)%2)

dfs(-1,0,0)
print(*ret, sep="\n")

Submission status: Runtime Error

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

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

ret = [0] * N
def dfs(pv, v, dist):
    ret[v] = dist
    for nv, w in edge[v]:
        if nv == pv:
            continue
        dfs(v, nv, (dist+w)%2)

dfs(-1,0,0)
print(ret)

Submission status: Runtime Error

Solution code:
#A
if False:
    N=int(input())
    E=[int(i) for i in input().split()]
    E.sort()
    s=sum(E)
    if E[-1]<s-E[-1]:
        print('Yes')
    else:
        print('No')
#B
#C
if False:
    N=int(input())
    S=[input() for _ in range(N)]
    march=list('MARCH')
    nam=[[] for _ in range(len(march))]
    for s in S:
        if s[0] in march:
            nam[march.index(s[0])].append(s)
    ans=0
    for i in range(5):
        for j in range(i+1,5):
            for k in range(j+1,5):
                ans+=len(nam[i])*len(nam[j])*len(nam[k])
    print(ans)

#D
N=int(input())
ver=[[] for _ in range(N)]
for _ in range(N-1):
    u,v,W=map(int,input().split())
    ver[u-1].append((v-1,W))
    ver[v-1].append((u-1,W))
his=[-1 for _ in range(N)]

def dfs(i,col):
    his[i]=col
    for j,w in ver[i]:
        if his[j]==-1:
            if w%2==0:
                dfs(j,col)
            else:
                dfs(j,(col+1)%2)

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

Submission status: Runtime Error

Solution code:
#A
#B
#C
if False:
    N=int(input())
    S=[input() for _ in range(N)]
    march=list('MARCH')
    nam=[[] for _ in range(len(march))]
    for s in S:
        if s[0] in march:
            nam[march.index(s[0])].append(s)
    ans=0
    for i in range(5):
        for j in range(i+1,5):
            for k in range(j+1,5):
                ans+=len(nam[i])*len(nam[j])*len(nam[k])
    print(ans)

#D
N=int(input())
ver=[[] for _ in range(N)]
for _ in range(N-1):
    u,v,W=map(int,input().split())
    ver[u-1].append((v-1,W))
    ver[v-1].append((u-1,W))
his=[-1 for _ in range(N)]

def dfs(i,col):
    his[i]=col
    for j,w in ver[i]:
        if his[j]==-1:
            if w%2==0:
                his[j]=col
                dfs(j,col)
            else:
                dfs(j,(col+1)%2)

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

Submission status: Runtime Error

Solution code:
#A
#B
#C
if False:
    N=int(input())
    S=[input() for _ in range(N)]
    march=list('MARCH')
    nam=[[] for _ in range(len(march))]
    for s in S:
        if s[0] in march:
            nam[march.index(s[0])].append(s)
    ans=0
    for i in range(5):
        for j in range(i+1,5):
            for k in range(j+1,5):
                ans+=len(nam[i])*len(nam[j])*len(nam[k])
    print(ans)

#D
N=int(input())
ver=[[] for _ in range(N)]
for _ in range(N-1):
    u,v,W=map(int,input().split())
    ver[u-1].append((v-1,W))
    ver[v-1].append((u-1,W))
his=[-1 for _ in range(N)]

def dfs(i,col):
    his[i]=col
    for j,w in ver[i]:
        if his[j]==-1:
            if w%2==0:
                his[j]=col
                dfs(j,col)
            else:
                dfs(j,(col+1)%2)

dfs(0,0)            
print(his)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(300000) #上限上げないと引っかかる
N = int(input())
tree = [[] for _ in range(N)]
for e in sys.stdin.read().rstrip():
    x,y,dist = e.split()
    x = int(x);y=int(y);dist= int(dist)
    x -=1
    y -=1
    if dist%2 == 0:
        tree[x].append(y)
        tree[y].append(x)#偶数の場合のみ処理




color = [-1]*N
#本体
def dfs(G,V,cur):
    color[V] = cur
    for next_V in G[V]:
        if (color[next_V] != -1):
            continue#これ入れないとスタックオーバーフロー　すでに行ったところはもう調べない
        dfs(G,next_V, cur)
    return True

for i in range(N):
    dfs(tree, 0, 0)
for j in range(N):
    print(color[j])

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
d = {}
graph = {i:[] for i in range(n)}
for _ in range(n-1):
  u,v,w = map(int,input().split())
  d[(u-1,v-1)] = w
  graph[u-1].append(v-1)
  graph[v-1].append(u-1)
  
  
color = [-1 for i in range(n)] 
color[0] = 0
from itertools import deque
queue =deque([])

while queue:
  p = queue.popleft()
  color_p = color[p]
  for i in range(graph[p]):
    if color[i] != -1:
      continue
    else:
      if d[(min(i,p),max(i,p))] %2:
        color[i] = 1 - color_p
      else:
        color[i] = color_p
      queue.push(i)

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

Submission status: Runtime Error

Solution code:
n = int(input())
graph = [[float('inf') for _ in range(n)] for _ in range(n)]
graph1 = {i:[] for i in range(n)}
for _ in range(n-1):
  u,v,w = map(int,input().slpit())
  graph[u-1][v-1] = w
  graph[v-1][u-1] = w
  graph1[u].append(v)
  graph1[v].append(u)
  
  
color = [-1 for i in range(n)] 
def dfs(i,j):#(child, parent)
  if color[i] != -1:
    return
  if graph[i][j] %2:
    color[i] = 1 -color[j]
  else:
    color[i] = color[j]
  for k in graph1[i]:
    if k ==j:
      continue
    dfs(k,i)
dsf(0,-1)

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

Submission status: Runtime Error

Solution code:
import sys
sys.getrecursionlimit(10**7)

def dfs(v,p,dist):
    for u,v in tree[v]:
        if u==p:
            continue
        else:
            dist[u]=dist[v]+w
            dfs(u,v,dist)
    
n =int(input())
tree = [[] for _ in range(n)]
for _ 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))
dist =[0]*N
dfs(0,-1,dist)

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

Submission status: Runtime Error

Solution code:
def dfs(v, p, dist):
    for u, w in tree[v]:
        if u == p:
            continue
        else:
            dist[u] = dist[v] + w
            dfs(u, v, dist)
 
 
n = int(input())
tree = [[] for _ in range(n)]
for _ 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))
 
dist = [0 for _ in range(n)]
dfs(0, -1, dist)
 
for i in dist:
    if 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):  # 辺の数はn-1
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    G[u].append([v, w])
    G[v].append([u, w])
ans = [-1] * n
ans[0] = 0
que = deque([0])
while que:
    tmp = que.popleft()  # 幅優先探索
    for u, w in G[tmp]:
        if ans[u] != -1:  # すでに訪問済みの時
            continue
        ans[u] = ans[tmp] + w  # 辺の長さを追加
        que.append(u)  # queに追加

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

Submission status: Runtime Error

Solution code:
N = int(input())
g = [[[]for i in range(2)] for j in range(N)]

for i in range(N-1):
    u,v,d = map(int,input().split())
    if d%2 == 0:
        g[u-1][0].append(v-1)
        g[v-1][0].append(u-1)
    else:
        g[u-1][1].append(v-1)
        g[v-1][1].append(u-1)

ans = [-1]*N
ans[0] = 0

stack = [0]
not_visit = [True for i in range(N)]
not_visit[0] = False

while stack:
    now = stack.pop()
    for h in range(2):
        for i in range(len(g[now][h])):
            if not_visit[g[now][h][i]]: #距離が偶数ならば
                not_visit[g[now][0][i]] = False
                ans[g[now][0][i]] = ans[now]
                stack.append(g[now][0][i])
        for i in range(len(g[now][h])):
            if not_visit[g[now][h][i]]: #距離が奇数ならば
                not_visit[g[now][1][i]] = False
                ans[g[now][1][i]] = (ans[now] + 1)%2
                stack.append(g[now][1][i])

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

Submission status: Runtime Error

Solution code:
import sys
import numpy
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra

def input():
    return sys.stdin.readline().rstrip('\n')

N = int(input())

# めちゃくちゃ遅くて TLE
#V = [[0]*N for _ in range(N)]

V = numpy.zeros((N,N), dtype=numpy.int8)

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

D = dijkstra(csr_matrix(V), directed=False, indices=0)
#print(D)

for d in D:
    if d % 2 == 0: #even
        print(0)
    else:
        print(1)

Submission status: Runtime Error

Solution code:
def dfs(v, p, c):
    for u, w in tree[v]:
        res[v] = c
        if u == p:
            continue
        else:
            if w % 2 == 0:
                dfs(u, v, c)
            else:
                dfs(u, v, c + 1)


n = int(input())
tree = [[] for _ in range(n)]
for _ 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])

res = [0 for _ in range(n)]
dfs(0, -1, 0)

for i in res:
    print(i)

Submission status: Runtime Error

Solution code:
def dfs(v, p, dist):
    for u, w in tree[v]:
        if u == p:
            continue
        else:
            dist[u] = dist[v] + w
            dfs(u, v, dist)


n = int(input())
tree = [[] for _ in range(n)]
for _ 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))

dist = [0 for _ in range(n)]
dfs(0, 0, dist)

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

Submission status: Runtime Error

Solution code:
#
import sys
sys.setrecursionlimit(10**7)

N=int(input())
Graph=[[] for _ in range(N)]
color=[-1]*N

for _ in range(N-1):
    a,b,c=map(int,input().split())
    a-=1
    b-=1
    Graph[a].append((b,c))
    Graph[b].append((a,c))

def dfs(v,cur=0):
    color[v]=cur
    for next_v,w in Graph[v]:
        if color[next_v]==-1:
            if w%2==1:
                cur=1-cur
        dfs(next_v,cur)
#探索
dfs(0)
for i in color:
    print(i)

Submission status: Runtime Error

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

def dfs(i,j):
    color[i] = j
    for w,l in E[i]:
        if color[w] == -1:
            tj = j
            if l % 2 != 0:
                tj = int(not tj)
            dfs(w,tj)

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

Submission status: Runtime Error

Solution code:
N=int(input())


R=[[] for _ in range(N)]


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

colors=[None]*N  
def set_color(vertex, color):
  colors[vertex] = color
  for v, w in edges[vertex]:
      if colors[v] == None:
        next_color = color
        if w % 2 == 1:
          next_color = (next_color+1)%2
        set_color(v, next_color)
set_color(0,0)
for c in colors:
  print(c)

Submission status: Runtime Error

Solution code:
N=int(input())


R=[[] for _ in range(N)]


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

colors=[None]*N  
def set_color(vertex, color):
  colors[vertex] = color
  for v, w in edges[vertex]:
      if colors[v] == None:
        next_color = color
        if w % 2 == 1:
          next_color = (next_color+1)%2
      set_color(v, next_color)
set_color(0,0)
for c in colors:
  print(c)

Submission status: Runtime Error

Solution code:
N=int(input())
import numpy as np

R=[[] for _ in range(N)]


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

colors=[None]*N  
def set_color(start, color):
  colors[start]=color
  for u,w in R[start]:
    if colors[u]==None:
      if w%2==0:
        next_color=color
        set_color(u,next_color)
      else:
        next_color=(color+1)%2
        set_color(u,next_color)
        
set_color(0,0)
for c in colors:
  print(c)

Submission status: Runtime Error

Solution code:
from collections import namedtuple

Edge = namedtuple('Edge', 'to cost')

N = int(input())

# index と頂点の番号を合わせたいのでサイズを N + 1 に
colors = [0] * (N + 1)
vertexes = [[] for _ in range(N + 1)]

for _ in range(N - 1):
  u, v, w = map(int, input().split())
  # 無向グラフなので双方向にリンクする
  vertexes[u].append(Edge(v, w))
  vertexes[v].append(Edge(u, w))


def dfs(id, color):
  colors[id] = color
  for edge in vertexes[id]:
    if edge.cost % 2 != 0:
      if colors[edge.to] == color:
        return False
      if colors[edge.to] == 0 and not dfs(edge.to, -color):
        return False
    elif colors[edge.to] == 0:
      dfs(edge.to, color)
  return True

if dfs(1, 1):
  for color in colors[1:]:
    print(1 if color == 1 else 0)

Submission status: Runtime Error

Solution code:
N=int(input())
G=[[] for i in range(N)]
for _ in range(N-1):
    u,v,w=map(int,input().split())
    G[u-1].append([v-1,w%2])
    G[v-1].append([u-1,w%2])
color=[0 for i in range(N)]
seen=[False for i in range(N)]
color[0]=1
def dps(i):
    seen[i]=True
    for g in G[i]:
        if seen[g[0]]:
            continue
        else:
            if g[1]==0:
                color[g[0]]=color[i]
            else:
                color[g[0]]=color[i]*-1
            dps(g[0])
dps(0)
for i in range(N):
    print(1 if color[i]==1 else 0)

Submission status: Runtime Error

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

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

def dfs(index):
    if ans[index]==-1:
        ans[index] = 0
    for i in root[index]:
        if ans[i[0]] == -1:
            ans[i[0]] = (ans[index]+i[1])%2
            dfs(i[0])

for i in range(N):
    dfs(i)

for i in ans:
    print(i)

Submission status: Runtime Error

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

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

def dfs(index):
    ans[index] = 0
    for i in root[index]:
        if ans[i[0]] == -1:
            ans[i[0]] = (ans[index]+i[1])%2
            dfs(i[0])

for i in range(N):
    dfs(i)

print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
G=[[]for i in range(N)]

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

ans=[-1]*N
#ルートでは親はいないから-1
def dfs(v,p,c):
    ans[v]=c
    for u,w in G[v]:
        if u==p:
            continue
        if w%2==0:
            dfs(u,v,c)
        else:
            dfs(u,v,1-c)
dfs(0,-1,1)
for i in range(N):
    print(ans[i])

Submission status: Runtime Error

Solution code:
N=int(input())
#G=[set() for _ in [0]*N]
G=[[]for i in range(N)]
#print(G)

for i in range(N-1):
    a,b,c=map(int,input().split())
    a-=1
    b-=1

    G[a].append((b,c))
    G[b].append((a,c))

#print(G)
#print(G)
ans=[-1]*N
#ルートでは親はいないから-1
#q=[(0,0,-1)]
"""
Q=[(0,0,-1)]
a,b,c=Q.pop()
print(a)
print(b)
print(c)
"""
"""
#print(q)
while q:
    #v：現在見ている点
    #p：vの親のノード
    #d：距離
    v,d,p=q.pop()
    if d%2==0:
        ans[v]=0
    else:
        ans[v]=1
    ans[v]=d
    for u,w in G[v]:
        #逆流を防ぐ
        if u==p:
            continue
        if w%2==0:
            pass
        else:
            d=1-d
        q.append((u,d,v))
    #print(q)
"""
def dfs(v,p,c):
    ans[v]=c
    #print(G[v])
    for u,w in G[v]:
        if u==p:
            continue
        if w%2==0:
            dfs(u,v,c)
        else:
            dfs(u,v,1-c)
dfs(0,-1,1)
print("\n".join(map(str,ans)))

Submission status: Runtime Error

Solution code:
N=int(input())
#G=[set() for _ in [0]*N]
G=[[]for i in range(N)]
#print(G)
u=[]
v=[]
w=[]
for i in range(N-1):
    a,b,c=map(int,input().split())
    a-=1
    b-=1

    G[a].append((b,c))
    G[b].append((a,c))

#print(G)
#print(G)
ans=[-1]*N
#ルートでは親はいないから-1
q=[(0,0,-1)]
"""
Q=[(0,0,-1)]
a,b,c=Q.pop()
print(a)
print(b)
print(c)
"""
"""
#print(q)
while q:
    #v：現在見ている点
    #p：vの親のノード
    #d：距離
    v,d,p=q.pop()
    if d%2==0:
        ans[v]=0
    else:
        ans[v]=1
    ans[v]=d
    for u,w in G[v]:
        #逆流を防ぐ
        if u==p:
            continue
        if w%2==0:
            pass
        else:
            d=1-d
        q.append((u,d,v))
    #print(q)
"""
def dfs(v,p,c):
    ans[v]=c
    #print(G[v])
    for u,w in G[v]:
        if u==p:
            continue
        if w%2==0:
            dfs(u,v,c)
        else:
            dfs(u,v,1-c)
dfs(0,-1,1)
print("\n".join(map(str,ans)))

Submission status: Runtime Error

Solution code:
N=int(input())
#G=[set() for _ in [0]*N]
G=[[]for i in range(N)]
#print(G)
u=[]
v=[]
w=[]
for i in range(N-1):
    a,b,c=map(int,input().split())
    a-=1
    b-=1
    
    G[a].append((b,c))
    G[b].append((a,c))

#print(G)
#print(G)
ans=[-1]*N
#ルートでは親はいないから-1
q=[(0,0,-1)]
"""
Q=[(0,0,-1)]
a,b,c=Q.pop()
print(a)
print(b)
print(c)
"""
"""
#print(q)
while q:
    #v：現在見ている点
    #p：vの親のノード
    #d：距離
    v,d,p=q.pop()
    if d%2==0:
        ans[v]=0
    else:
        ans[v]=1
    ans[v]=d
    for u,w in G[v]:
        #逆流を防ぐ
        if u==p:
            continue
        if w%2==0:
            pass
        else:
            d=1-d
        q.append((u,d,v))
    #print(q)
"""
def dfs(v,p,c):
    ans[v]=c
    print(G[v])
    for u,w in G[v]:
        if u==p:
            continue
        if w%2==0:
            dfs(u,v,c)
        else:
            dfs(u,v,1-c)
dfs(0,-1,1)
print("\n".join(map(str,ans)))

Submission status: Runtime Error

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

class Node:
    def __init__(self):
        self.children = []
        self.color = -1
    
    def append(self,target,weight):
        self.children.append((target,weight))
        target.children.append((self,weight))
 
    def setColor(self,color):
        self.color = color
        for child in self.children:
            if child[0].color != -1:
                continue
            if child[1] % 2 == 0:
                child[0].setColor(self.color)
            else:
                child[0].setColor(0 if self.color == 1 else 1)

def solve(N: int, u: "List[int]", v: "List[int]", w: "List[int]"):
    nodes = [Node() for i in range(N)]
    # print(u)
    for i in range(N-1):
        nodes[u[i]-1].append(nodes[v[i]-1],w[i])
    nodes[0].setColor(0)
    for n in nodes:
        print(n.color)
    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:
#126_D
n=int(input())
edges=[[] for _ in range(n)]
for _ in range(n-1):
    u,v,w=map(int,input().split())
    edges[u-1].append((v-1,w))
    edges[v-1].append((u-1,w))
visit=[False for _ in range(n)]
d=[0 for i in range(n)]

def dfs(i):
    visit[i]=True
    if all(visit[i] for i in range(n)):
        print(*d,sep='\n')
        exit()
    for v,w in edges[i]:
        if visit[v]==False:
            d[v]=(d[i]+w)%2
            dfs(v)
            
dfs(0)

Submission status: Runtime Error

Solution code:
N = int(input())
class Graph:
    def __init__(self):
        self.to = []
        self.w = []
graph = [Graph() for _ in range(N)]
colors = [-1 for _ in range(N)]
def dfs(v, dis = 0):
    if dis % 2 == 0:
        colors[v] = 0
    else:
        colors[v] = 1
    for node, w in zip(graph[v].to, graph[v].w):
        if colors[node] != -1:
            continue
        dfs(node, dis+w)
    return True

for _ in range(N-1):
    u, v, w = map(int, input().split())
    graph[u-1].to.append(v-1)
    graph[u-1].w.append(w)
    # if undirected graph, add this
    graph[v-1].to.append(u-1)
    graph[v-1].w.append(w)
dfs(0, 0)
for color in colors:
    print(color)

Submission status: Runtime Error

Solution code:
N = int(input())
class Graph:
    def __init__(self):
        self.to = []
        self.w = []
graph = [Graph() for _ in range(N)]
colors = [-1 for _ in range(N)]
def dfs(v, dis = 0):
    if dis % 2 == 0:
        colors[v] = 0
    else:
        colors[v] = 1
    for node, w in zip(graph[v].to, graph[v].w):
        if colors[node] != -1:
            continue
        dfs(node, dis+w)
    return True

for _ in range(N-1):
    u, v, w = map(int, input().split())
    graph[u-1].to.append(v-1)
    graph[u-1].w.append(w)
    # if undirected graph, add this
    graph[v-1].to.append(u-1)
    graph[v-1].w.append(w)
for v in range(N):
    if colors[v] != -1:
        continue
    dfs(v, 0)
for color in colors:
    print(color)

Submission status: Runtime Error

Solution code:
N = int(input())
class Graph:
    def __init__(self):
        self.to = []
        self.w = []
graph = [Graph() for _ in range(N)]
colors = [-1 for _ in range(N)]
def dfs(v, cur, dis = 0):
    if dis % 2 == 0:
        colors[v] = cur
    else:
        colors[v] = 1 - cur
    for node, w in zip(graph[v].to, graph[v].w):
        if colors[node] != -1:
            if colors[node] == cur and dis % 2 != 0:
                return False
            continue
        if not dfs(node, cur, dis+w):
            return False
    return True

for _ in range(N-1):
    u, v, w = map(int, input().split())
    graph[u-1].to.append(v-1)
    graph[u-1].w.append(w)
    # if undirected graph, add this
    graph[v-1].to.append(u-1)
    graph[v-1].w.append(w)
for v in range(N):
    if colors[v] != -1:
        continue
    dfs(v, 0, 0)
for color in colors:
    print(color)

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    N = int(input())
    UVW = [tuple(map(int, input().split())) for _ in range(N - 1)]

    ans = ['0'] * N
    fixed = set()
    for u, v, w in UVW:
        if w % 2 == 1:
            if u in fixed:
                ans[v] = '1'
            else:
                ans[u] = '1'
            fixed.add(u)
            fixed.add(v)
    print("\n".join(ans))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = [int(i) for i in input().split()][0]
uvws = [[int(i) for i in input().split()] for _ in range(N-1)]
colors = [0 for _ in range(N)]
verts = [[] for _ in range(N)]

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

def dfs(n, v, dist):
  for vert in verts[n-1]:
    nex, w = vert
    if nex != v:
      if (dist + w) % 2 != 0:
        colors[nex-1] = 1
      dfs(nex, n, dist + w)

dfs(1, 1, 0)
for color in colors:
  print(color)

Submission status: Runtime Error

Solution code:
N = [int(i) for i in input().split()][0]
uvws = [[int(i) for i in input().split()] for _ in range(N-1)]
array = [0 for _ in range(N)]


class Node:
  def __init__(self, index):
    self.index = index
    self.connection = []
    self.weights = []
    self.flag = 0
    
  
  def add_vertex(self, target, dist):
    self.connection.append(target)
    self.weights.append(dist)
    
  def breath_search(self, parent):
    for con, dis  in zip(self.connection, self.weights):
      if con != parent:
        if dis %2 == 0:
          tree[con-1].flag = self.flag
        else:
          tree[con-1].flag = (self.flag + 1)%2
       	tree[con-1].breath_search(self.index)
        
tree = [Node(i+1) for i in range(N)]
for uvw in uvws:
  u, v, w = uvw
  tree[u-1].add_vertex(v, w)
  tree[v-1].add_vertex(u, w)
    
tree[0].breath_search(-1)
for tr in tree:
  print(tr.flag)

Submission status: Runtime Error

Solution code:
N=int(input())
A=[list(map(int,input().split())) for _ in range(N-1)]
C=[0]*N
D=[[] for i in range(N)]
for u,v,w in A:
  u-=1
  v-=1
  D[u].append((v,w))
  D[v].append((u,w))

def dfs(n,v,k):
  for i in D[n]:
    if k%2!=0:
      C[n]=1
    if v==i[0]:
      continue
    dfs(i[0],n,k+i[1])       
        
dfs(0,0,0)

for i in C:
  print(i)

Submission status: Runtime Error

Solution code:
N=int(input())
A=[list(map(int,input().split())) for _ in range(N-1)]
C=[0]*N
D=[[] for i in range(N)]
for u,v,w in A:
  u-=1
  v-=1
  D[u].append((v,w))
  D[v].append((u,w))

def dfs(n,v,k):
  for i in D[n]:
    if k%2!=0:
      C[n]=1
    if v!=i[0]:
      dfs(i[0],n,k+i[1])       
        
dfs(0,0,0)

for i in C:
  print(i)

Submission status: Runtime Error

Solution code:
#import sys
#sys.setrecursionlimit(10 ** 7)

N = int(input())
visited = [False] * N
colors = [-1] * N
dist = [float('inf')] * N

def dfs(u):
    if visited[u]:
        return

    visited[u] = True
    for (v, w) in edges[u]:
        if visited[v]:
            continue
        if colors[v] != -1:
            continue

        dist[v] = dist[u] + w
        if dist[v] % 2 == 0:
            colors[v] = 0
        else:
            colors[v] = 1
        dfs(v)


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


colors[0] = 0
dist[0] = 0
dfs(0)
for c in colors:
    print(c)