Problem:


Score : 400 points

Problem Statement

We have a tree with N vertices numbered 1 to N.
The i-th edge in the tree connects Vertex u_i and Vertex v_i, and its length is w_i.
Your objective is to paint each vertex in the tree white or black (it is fine to paint all vertices the same color) so that the following condition is satisfied:

For any two vertices painted in the same color, the distance between them is an even number.

Find a coloring of the vertices that satisfies the condition and print it. It can be proved that at least one such coloring exists under the constraints of this problem.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq u_i < v_i \leq N

1 \leq w_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
u_1 v_1 w_1
u_2 v_2 w_2
.
.
.
u_{N - 1} v_{N - 1} w_{N - 1}

Output

Print a coloring of the vertices that satisfies the condition, in N lines.
The i-th line should contain 0 if Vertex i is painted white and 1 if it is painted black.

If there are multiple colorings that satisfy the condition, any of them will be accepted.

Sample Input 1

3
1 2 2
2 3 1

Sample Output 1

0
0
1

Sample Input 2

5
2 5 2
2 3 10
1 3 8
3 4 2

Sample Output 2

1
0
1
0
1



Sample input:
3
1 2 2
2 3 1


Sample output:
0
0
1

Submission status: Runtime Error

Solution code:
n = int(input())
try:
  for i in range(100000)
  tree = [int(i) for i in input().split()]
except:
  print(tree)

Submission status: Runtime Error

Solution code:
n=int(input())
point=[]
answer=[]
already=[]
for i in range(n):
    point.append([])
    answer.append(0)
    already.append(0)
for i in range(n-1):
    u,v,w=map(int,input().split())
    point[u-1].append((v-1,w))
    point[v-1].append((u-1,w))
def dfs(now,c):
    answer[now]=c
    already[now]=1
    for i in point[now]:
        if already[i[0]]==1:
            pass
        else:
            if i[1]%2==0:
                dfs(i[0],c)
            else:
                if c==0:
                    dfs(i[0],1)
                else:
                    dfs(i[0],0)
dfs(0,0)
for i in answer:
    print(i)

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())

listX =[]
while True:
    try:
        listX.append(list(map(int,input().split())))
    except:
        break;

table = np.zeros((N,N))
point = np.zeros(N)
for i in range(N-1):
    x = listX[i][0]
    y = listX[i][1]
    table[x-1,y-1] = listX[i][2]
    table[y-1,x-1] = listX[i][2]

for j in range(0,N):
    for k in range(j+1,N):
        point[k] += table[j,k]

output = (point%2==0)
output = list(map(int,output))

outstr = str(output[0])

for i in range(1,N):
    outstr += "\n"+str(output[i])

print(outstr)

Submission status: Runtime Error

Solution code:
n=int(input())
mat=[[]*n for _ in range(n)]
group=[0]*n
group[0]=1
for i in range(n-1):
    u, v, w = map(int, input().split())
    mat[u-1].append((v-1, w))
    mat[v-1].append((u-1, w))
def C(kw, p, l):
    global group
    if kw[1]%2==1:
        p*=-1
    group[kw[0]]=p
    for kwi in mat[kw[0]]:
        if kwi[0]!=l:
            C(kwi, p, kw[0])
for kwi in mat[0]:
    C(kwi, 1, 0)
for gi in group:
    if gi>0:
        print(1)
    else:
        print(0)

Submission status: Runtime Error

Solution code:
n = int(input())
U, V = 0, 0
for i in range(n):
    u,v,w = map(int, input().split())
    if w % 2 == 0:
        U = u-1
        V = v-1
        break

for i in range(n):
    if i in (U, V):
        print(1)
    else:
        print(0)

Submission status: Runtime Error

Solution code:
read = input
n = int(read())
v = [[] for i in range(n+1)] # 邻接
used = [0 for i in range(n+1)] # 节点 1~n
def add(f,t,w):
    v[f].append((t, w))
    
for i in range(n-1):
    f,t,w = map(int, read().split())
    add(f,t,w)
    add(t,f,w)
# 从1出发，贪心即可，1画1，理论不会有回环
def dfs(f, s, fa=-1):
    if s % 2 == 0: 
        used[f] = 1
    for t, w in v[f]:
#         print('%d->%d->%d'%(fa, f, t,), s, w)
        if t == fa:continue
        dfs(t, w + s, f)
dfs(1,0)
for i in used[1:]:
    print(used[i])

Submission status: Runtime Error

Solution code:
N = int(input())
arr = [None]*N
visited = [False]*N
for i in range(N-1):
    u, v, w = map(int, input().split())
    if i==0:
        arr[u-1] = 0
        arr[v-1] = w
    else:
        if visited[u-1]:
            arr[v-1] = arr[u-1] + w
        else:
            arr[u-1] = arr[v-1] + w
    visited[u-1] = True
    visited[v-1] = True

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

Submission status: Runtime Error

Solution code:
n = int(input())
U, V = 0, 0
for i in range(n):
    u,v,w = map(int, input().split())
    if w % 2 == 0:
        U = u
        V = v
        break

for i in range(n):
    if i in (U, V):
        print(1)
    else:
        print(0)

Submission status: Runtime Error

Solution code:
N = int(input())

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

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

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

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

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

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

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

Submission status: Runtime Error

Solution code:
def dfs(i, V, E):
    if i in E.keys():
        for k in E[i].keys():
            if V[k] >= 0:
                continue
            if E[i][k] % 2:
                V[k] = 1 - V[i]
            else:
                V[k] = V[i]
            V = dfs(k, V, E)
    return V

n = int(input())
E = {}
for i in range(n - 1):
    ui, vi, wi = map(int, input().split())
    if ui in E.keys():
        E[ui][vi] = wi
    else:
        E[ui] = {vi: wi}
    if vi in E.keys():
        E[vi][ui] = wi
    else:
        E[vi] = {ui: wi}
V = [-1 for _ in range(n + 1)]
V[1] = 0
V = dfs(1, V, E)
for vi in V[1:]:
    print(vi)

Submission status: Runtime Error

Solution code:
n = int(read())
v = [[] for i in range(n+1)] # 邻接
used = [0 for i in range(n+1)] # 节点 1~n
def add(f,t,w):
    v[f].append((t, w))
    
for i in range(n-1):
    f,t,w = map(int, read().split())
    add(f,t,w)
    add(t,f,w)
# 从1出发，贪心即可，1画1，理论不会有回环
def dfs(f, s, fa=-1):
    if s % 2 == 0: 
        used[f] = 1
    for t, w in v[f]:
#         print('%d->%d->%d'%(fa, f, t,), s, w)
        if t == fa:continue
        dfs(t, w + s, f)
dfs(1,0)
for i in used[1:]:
    print(used[i])

Submission status: Runtime Error

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

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

def dfs(vertex, distance_from_start):
	global colors

	colors[vertex] = distance_from_start % 2

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


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

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

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

dfs(0, 0)

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

Submission status: Runtime Error

Solution code:
from collections import deque
import sys

def parse(f):
    N = int(next(f))
    return N, deque([[int(s) for s in next(f).split()] for _ in range(N-1)])

def main(N, uvw):
    if N == 1:
        return [0]
    tree = [None for _ in range(N+1)]
    u, v, w = uvw.popleft()
    tree[u] = 0
    if w % 2 == 0:
        tree[v] = 0
    else:
        tree[v] = 1
    while uvw:
        u, v, w = uvw.popleft()
        if tree[u] is None and tree[v] is None:
            uvw.push([u, v, w])
        elif tree[u] is None and tree[v] is not None:
            u, v = v, u
            if w % 2 == 0:
                tree[v] = tree[u]
            else:
                tree[v] = 1 - tree[u]
        elif tree[u] is not None and tree[v] is None:
            if w % 2 == 0:
                tree[v] = tree[u]
            else:
                tree[v] = 1 - tree[u]
        else:
            return [0] * N

    return tree[1:]

print('\n'.join(map(str, main(*parse(sys.stdin)))))

Submission status: Runtime Error

Solution code:
import queue
import sys

input = sys.stdin.readline

N = int(input())
nodes = queue.Queue()
for i in range(N - 1):
    nodes.put(list(map(int, input().split())))

B, W = set(), set()
u, v, w = nodes.get()
if w % 2 == 0:
    B.add(u)
    B.add(v)
else:
    B.add(u)
    W.add(v)

while nodes.qsize() != 0:
    u, v, w = nodes.get()
    if w % 2 == 0:
        if u in B or v in B:
            B.add(u)
            B.add(v)
        elif u in W or v in W:
            W.add(u)
            W.add(v)
        else:
            nodes.put(u, v, w)
    else:
        if u in B or v in W:
            B.add(u)
            W.add(v)
        elif u in W or v in B:
            W.add(u)
            B.add(v)
        else:
            nodes.put(u, v, w)

ans = [0] * N
for x in W:
    ans[x - 1] = 1
for x in ans:
    print(x)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())

X = [0] * M
Y = [0] * M
Z = [0] * M
for i in range(M):
    X[i], Y[i], Z[i] = map(int, input().split())

X1 = list(set(X))

print(N-len(X1))

Submission status: Runtime Error

Solution code:
import numpy as np
words = lambda t : list(map(t, input().split()))



def search(i, visited, ns, ans) :
    #print(i+1)
    visited[i] = True
    for next, weight in ns[i]:
        if visited[next] == False:
            if weight % 2 == 0:
                ans[next] = ans[i]
            else:
                ans[next] = (ans[i]+1) % 2
            search(next, visited, ns, ans)


def dfs(ns, ans):
    visited = [False] * len(ns)
    search(0, visited, ns, ans)

def main():
    n = int(input())
    ns = [[] for _ in range(n)]
    for _ in range(n-1):
        (st,end,weight) = map(int,input().split())
        ns[st-1].append((end - 1,weight))
        ns[end-1].append((st-1,weight))
    ans = [0] * n
    dfs(ns, ans)
    for a in ans:
        print(a)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

import sys
input = sys.stdin.readline

class Unionfind:

    __slots__ = ['nodes','size']

    def __init__(self, n):
        self.nodes = list(range(n))
        self.size = [1]*n

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

    def unite(self, x, y):
        x = self.root(x); y = self.root(y)
        if x == y:
            return
        if self.size[x] < self.size[y]:
            x, y = y, x
        self.size[x] += self.size[y]
        self.nodes[y] = x

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

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

uf = Unionfind(N)

for u,v,w in edge:
    u -= 1; v -= 1
    if w % 2 == 1:
        continue
    uf.unite(u,v)

if len(set(uf.nodes)) == 1:
    for _ in range(N):
        print(0)
    exit()

for i in range(N):
    uf.root(i)

w,b = list(set(uf.nodes))

for i in range(N):
    if uf.nodes[i] == w:
        uf.nodes[i] = 0
    elif uf.nodes[i] == b:
        uf.nodes[i] = 1

for v in uf.nodes:
    print(v)

Submission status: Runtime Error

Solution code:
import sys,collections as cl,bisect as bs
Max = sys.maxsize
def l():
	return list(map(int,input().split()))
def m():
	return map(int,input().split())
def s(x):
	a = []
	aa = x[0]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def jo(x):
	return " ".join(map(str,x))

def on(x,n,nn,k):
	global a
	for i in range(len(x[n])):
		if not x[n][i][0] == nn:
			if (k + x[n][i][1]) % 2 == 0:
				a[x[n][i][0]] = 0
				on(x,x[n][i][0],n,(k + x[n][i][1]) % 2)
			else:
				on(x,x[n][i][0],n,(k + x[n][i][1]) % 2)


n = int(input())

a = [1 for i in range(n)]
a[0] = 0
b = [[] for i in range(n)]

for i in range(n-1):
	z,x,c = m();
	b[z-1].append([x-1,c])
	b[x-1].append([z-1,c])

on(b,0,0,0)

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

Submission status: Runtime Error

Solution code:
import math
inp_s = input()
N, K = inp_s.split(" ")
N = int(N)
K = int(K)

sum = 0
for i in range(1,N+1):
    if K/i <= 1:
        sum += 1
    else:
        necessary = int(math.log2(K/i)) + 1
        sum += 1/pow(2, necessary)
print(sum/N)

Submission status: Runtime Error

Solution code:
N = int(input())
con = [set() for _ in range(N)]
for i in range(N-1):
    u, v, w = map(int, input().split(" "))
    con[u-1].add((v-1, w))
    con[v-1].add((u-1, w))

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

def solve(x):
    for i, w in con[x]:
        if dp[i] < 0:
            if w % 2 == 0:
                dp[i] = dp[x]
            else:
                if dp[x] == 1:
                    dp[i] = 0
                else:
                    dp[i] = 1
            solve(i)
    return

solve(0)
if sum(dp) == 0:
    dp[-1] = 1
for i in range(N):
    print(dp[i])

Submission status: Runtime Error

Solution code:
def dfs(i, V, E):
    for k in E[i].keys():
        if V[k] >= 0:
            continue
        if E[i][k] % 2:
            V[k] = 1 - V[i]
        else:
            V[k] = V[i]
        V = dfs(k, V, E)
    return V

n = int(input())
E = {}
for i in range(n - 1):
    ui, vi, wi = map(int, input().split())
    if ui in E.keys():
        E[ui][vi] = wi
    else:
        E[ui] = {vi: wi}
    if vi in E.keys():
        E[vi][ui] = wi
    else:
        E[vi] = {ui: wi}
V = [-1 for _ in range(n + 1)]
V[1] = 0
V = dfs(1, V, E)
for vi in V[1:]:
    print(vi)

Submission status: Runtime Error

Solution code:
from collections import defaultdict, namedtuple

def main():
    n = int(input())
    edges = defaultdict(list)

    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))
    
    vertexes = [None for _ in range(n)]
    vertexes[0] = 0
    traverse(0, edges, vertexes)

    for v in vertexes:
        print(v)

def traverse(u, edges, vertexes):
    for e in edges[u]:
        v, w = e
        if vertexes[v] is not None:
            continue

        if w % 2 == 0:
            vertexes[v] = vertexes[u]
        else:
            vertexes[v] = 1 - vertexes[u]
        traverse(v, edges, vertexes)

main()

Submission status: Runtime Error

Solution code:
from collections import deque
import sys

from collections import deque

def parse(f):
    N = int(next(f))
    return N, deque([[int(s) for s in next(f).split()] for _ in range(N-1)])

def main(N, uvw):
    tree = [None for _ in range(N+1)]
    u, v, w = uvw.popleft()
    tree[u] = 0
    if w % 2 == 0:
        tree[v] = 0
    else:
        tree[v] = 1
    while uvw:
        u, v, w = uvw.popleft()
        if tree[u] is None and tree[v] is None:
            uvw.push([u, v, w])
        elif tree[u] is None and tree[v] is not None:
            u, v = v, u
            if w % 2 == 0:
                tree[v] = tree[u]
            else:
                tree[v] = 1 - tree[u]
        elif tree[u] is not None and tree[v] is None:
            if w % 2 == 0:
                tree[v] = tree[u]
            else:
                tree[v] = 1 - tree[u]
        else:
            return [0] * N

    return tree[1:]

print('\n'.join(map(str, main(*parse(sys.stdin)))))

Submission status: Runtime Error

Solution code:
N = int(input())

edges = [{} for i in range(N)]
colors = [0 for i in range(N)]

def dfs(s, dist):
    for key, val in edges[s].items():
        d = dist + val
        if d % 2 == 0:
            colors[key] = 0 
        else :
            colors[key] = 1
        
        if s in edges[key]:
            del edges[key][s]

        dfs(key, d)
    return

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

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

Submission status: Runtime Error

Solution code:
N = int(input())

data = [[] for _ in range(N+1)]

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

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


ans = [0] * (N+1)
went = [False] * (N+1)

went[0] = True
went[1] = True
before = 1

def f(before):
    for i, c in data[before]:
        if not went[i]:
            went[i] = True
            if c % 2 == 0:
                ans[i] = ans[before]
            else:
                ans[i] = 1 - ans[before]
            return f(i)
    return

f(1)

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

Submission status: Runtime Error

Solution code:
# coding: utf-8
import numpy as np
from functools import lru_cache
import time



def main(): 
    if True:
        N = int(input())
        A = np.asarray([list(map(int, input().split())) for i in range(N-1)])
    else:
        pass
    global res
    res = np.full(N,-1)
    paint(A,1,0)
    for r in res:
        print(r)

def paint(A,n,color):
    global res
    if res[n-1] != -1:
        return
    res[n-1] = color

    for a in A[A[:,0]==n]:
        tmpcolor = color if a[2] % 2 == 0 else color * -1 + 1
        paint(A,a[1],tmpcolor)

    for a in A[A[:,1]==n]:
        tmpcolor = color if a[2] % 2 == 0 else color * -1 + 1
        paint(A,a[0],tmpcolor)

if __name__ =='__main__':
    main()

Submission status: Runtime Error

Solution code:
N = int(input())

colors = [0] * (N+1)
vertices = defaultdict(bool)
edges = defaultdict(dict)
for i in range(1, N):
    u, v, w = map(int, input().split())
    edges[u][v] = w % 2 == 0
    edges[v][u] = w % 2 == 0
    vertices[u] = False
    vertices[v] = False

queue = [(u, v)]
while len(queue) > 0:
    u, v = queue.pop(0)
    vertices[u] = True
    if vertices[v]:
        continue
    vertices[v] = True
    if colors[u] == colors[v] and not edges[u][v]:
        colors[v] = 1 - colors[u]

    for nv in edges[v].keys():
        if not vertices[nv]:
            queue.append((v, nv))

for i in range(1, N+1):
    print(colors[i])

Submission status: Runtime Error

Solution code:
n = int(input())

g = [[] for i in range(n+1)]
d = [-1 for i in range(n+1)]

for i in range(n-1):
  s, e, w = [int(j) for j in input().split()]
  w = w % 2
  g[s].append([e, w])
  g[e].append([s, w])

def dfs(x, di):
  d[x] = di
  for i, w in g[x]:
    if d[i] == -1:
      dfs(i, (di + w) % 2)

dfs(1, 0)

for i in d[1:]:
  print(i % 2)

Submission status: Runtime Error

Solution code:
n = int(input())
ans = [-1]*n
data =[[] for i in range(n+1)]

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

ans[0] = 1
def color(i):
    for v,w in data[i]:
        if ans[v] == -1:
            if w % 2 == 0:
                ans[v] = ans[i]
            else:
                ans[v] = 1 - ans[i]
            color(v)
    return

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

Submission status: Runtime Error

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

diff = lambda n: 1 if n==0 else 0

def dfs(prev,curr):
    for node,d in edges[curr]:
        if node==prev:
            continue
        colors[node] = colors[curr] if d%2==0 else diff(colors[curr])
        dfs(curr,node)

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

Submission status: Runtime Error

Solution code:
import sys,collections as cl,bisect as bs
Max = sys.maxsize
def l():
	return list(map(int,input().split()))
def m():
	return map(int,input().split())
def s(x):
	a = []
	aa = x[0]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def jo(x):
	return " ".join(map(str,x))

def on(x,n,nn,k):
	global a
	for i in range(len(x[n])):
		if not x[n][i][0] == nn:
			if (k + x[n][i][1]) % 2 == 0:
				a[x[n][i][0]] = 0
				on(x,x[n][i][0],n,k + x[n][i][1] % 2)
			else:
				on(x,x[n][i][0],n,k + x[n][i][1] % 2)


n = int(input())

a = [1 for i in range(n)]
a[0] = 0
b = [[] for i in range(n)]

for i in range(n-1):
	z,x,c = m();
	b[z-1].append([x-1,c])
	b[x-1].append([z-1,c])

on(b,0,0,0)

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

Submission status: Runtime Error

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

import sys
input = sys.stdin.readline

class Unionfind:

    __slots__ = ['nodes','size']

    def __init__(self, n):
        self.nodes = list(range(n))
        self.size = [1]*n

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

    def unite(self, x, y):
        x = self.root(x); y = self.root(y)
        if x == y:
            return
        if self.size[x] < self.size[y]:
            x, y = y, x
        self.size[x] += self.size[y]
        self.nodes[y] = x

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

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

uf = Unionfind(N)

for u,v,w in edge:
    u -= 1; v -= 1
    if w % 2 == 1:
        continue
    uf.unite(u,v)

if len(set(uf.nodes)) == 1:
    for _ in range(N):
        print(0)
    exit()

w,b = list(set(uf.nodes))

for i in range(N):
    if uf.nodes[i] == w:
        uf.nodes[i] = 0
    elif uf.nodes[i] == b:
        uf.nodes[i] = 1

for v in uf.nodes:
    print(v)

Submission status: Runtime Error

Solution code:
N = int(input())

edges = [{} for i in range(N)]
colors = [0 for i in range(N)]

def dfs(s, dist):
    for key, val in edges[s].items():
        d = dist + val
        if d % 2 == 0:
            colors[key] = 0 
        else :
            colors[key] = 1
        
        del edges[key][s]
        dfs(key, d)
    return

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

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

Submission status: Runtime Error

Solution code:
N = int(input())

data = [[] for _ in range(N)]
ans = [None] * N

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

def f(i, color):
    for t, w in data[i]:
        if ans[t] is not None:
            continue
        ans[t] = color^w
        f(t, color^w)

f(0, 0)

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

Submission status: Runtime Error

Solution code:
n = int(input())
ans = [-1]*n
data =[[] for i in range(n)]

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

ans[0] = 0
def color(i):
    for v,w in data[i]:
        if ans[v]== -1:
            if w%2 == 0:
                ans[v] = ans[i]
            else:
                ans[v] = 1 - ans[i]
            color(v)
    return

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

Submission status: Runtime Error

Solution code:
from collections import defaultdict


class Solution:
    def main(self):
        N = int(input())
        self.hashmap = defaultdict(set)
        for i in range(N-1):
            u, v, w = map(int, input().split())
            self.hashmap[u-1].add((v-1, w))
            self.hashmap[v-1].add((u-1, w))
        # print(self.hashmap)
        self.colors = [None] * N
        for i in range(N):
            try:
                self.dfs(i, 0)
                print(self.colors[i])
            except IndexError:
                print('Error')
                return

    def dfs(self, i, color):
        if self.colors[i] is None:
            self.colors[i] = color
            for node, weight in self.hashmap[i]:
                if weight % 2 == 0:
                    self.dfs(node, color)
                else:
                    self.dfs(node, 1 ^ color)


if __name__ == '__main__':
    Solution().main()

Submission status: Runtime Error

Solution code:
from collections import deque
import sys

def parse(f):
    N = int(next(f))
    return N, deque([[int(s) for s in next(f).split()] for _ in range(N-1)])

def main(N, uvw):
    tree = [None for _ in range(N+1)]
    u, v, w = uvw.popleft()
    tree[u] = 0
    if w % 2 == 0:
        tree[v] = 0
    else:
        tree[v] = 1
    while uvw:
        u, v, w = uvw.popleft()
        if tree[u] is None and tree[v] is None:
            uvw.push([u, v, w])
            continue
        elif tree[u] is None and tree[v] is not None:
            u, v = v, u
        elif tree[u] is not None and tree[v] is None:
            pass
        else:
            raise RuntimeError()
        if w % 2 == 0:
            tree[v] = tree[u]
        else:
            tree[v] = 1 - tree[u]
    return tree[1:]


print('\n'.join(map(str, main(*parse(sys.stdin)))))

Submission status: Runtime Error

Solution code:
from collections import defaultdict

N = int(input())

D = defaultdict(list) 
S = defaultdict(lambda: -1) #STEPD
R = defaultdict(int) #ROOT
for i in range(N-1):
    u,v,w = map(int,input().split())
    D[u].append(v)
    D[v].append(u)
    R["{}-{}".format(u,v)] = w
    
T = [(1,0)]
S[n] = 0
while T:
    n,w = T.pop()
    for d in D[n]:
        wr = R["{}-{}".format(n,d)]
        if S[d]>=0:continue
        else: S[d] = (w+wr)%2
        T.append((d,(w+wr)%2))
        
for k in sorted(S.keys()):
    print(S[k])

Submission status: Runtime Error

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

B = [[] for i in range(n+1)]

for i in range(len(A)):
    B[A[i][0]].append([A[i][1],A[i][2]])
    B[A[i][1]].append([A[i][0],A[i][2]])

root = 0
for i in range(len(B)):
    root = max(root, len(B[i]))

ANS = [-1 for i in range(n+1)]
ANS[root]=0
#print(B)
def nuru(k):
    col = ANS[k]
    for j in range(len(B[k])):
        if ANS[B[k][j][0]] == -1:
            ANS[B[k][j][0]] = (col + B[k][j][1])%2
            nuru(B[k][j][0])

nuru(root)

#print(ANS)

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

Submission status: Runtime Error

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

color = [-1] * N
def fill(n, val):
    if color[n] != -1:
        return
    color[n] = val
    for nei in nlist[n]:
        fill(nei[0], (val + nei[1]) % 2)
fill(0, 0)

for c in color:
    print(c)

Submission status: Runtime Error

Solution code:
n = int(input())

g = [[] for i in range(n+1)]
d = [-1 for i in range(n+1)]

for i in range(n-1):
  s, e, w = [int(j) for j in input().split()]
  g[s].append([e, w])
  g[e].append([s, w])

def dfs(x, di):
  d[x] = di
  for i, w in g[x]:
    if d[i] == -1:
      dfs(i, di + w)

dfs(1, 0)

for i in d[1:]:
  print(i % 2)

Submission status: Runtime Error

Solution code:
N = int(input())
edge = sorted([tuple(map(int, input().split())) for _ in range(N)])
color = [0]*N

for u, v, w in edge:
    if w % 2 == 1 and color[u-1] == color[v-1]:
        color[v-1] ^= 1
    elif w % 2 == 0 and color[u-1] != color[v-1]:
        color[v-1] ^= 1

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

Submission status: Runtime Error

Solution code:
n = int(input())

g = [[] for i in range(n+1)]
d = [-1 for i in range(n+1)]

for i in range(n-1):
  s, e, w = [int(j) for j in input().split()]
  g[s].append([e, w])
  g[e].append([s, w])

def dfs(x, di):
  print("{} {}".format(x, di))
  d[x] = di
  for i, w in g[x]:
    if d[i] == -1:
      dfs(i, di + w)

dfs(1, 0)

for i in d[1:]:
  print(i % 2)

Submission status: Runtime Error

Solution code:
import numpy as np

N=int(input())
A=[]
for i in range(N-1):
    A.append([j for j in map(int,input().split())])
    

ans=np.zeros(N)
for item in A:
    u=int(item[0])
    v=int(item[1])
    w=int(item[2])
    if u%2!=0:
        if ans[u]==0:
            ans[v]=1
        else:
            ans[v]=0
for k in range(N):
    print(int(ans[k]))

Submission status: Runtime Error

Solution code:
# /usr/bin/python
# -*- coding: utf-8 -*-
import math
from collections import defaultdict


N = int(input())
cn = [-1 for i in range(N)]
cn[0] = 0
route = defaultdict(list)
wn = [[0 for j in range(N+1)] for i in range(N+1)]

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

nxt = set([1])
vnum = 0
while vnum<N and len(nxt)>0:
  p = nxt.pop()
  for q in route[p]:
    if cn[q-1] > 0:
      continue
    else:
      vnum += 1
      nxt.add(q)
      cn[q-1] = (cn[p]+wn[p][q]) % 2

for c in cn:
  print(c)

Submission status: Runtime Error

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

B = [[] for i in range(n+1)]

for i in range(len(A)):
    B[A[i][0]].append([A[i][1],A[i][2]])
    B[A[i][1]].append([A[i][0],A[i][2]])

root = 0
for i in range(len(B)):
    root = max(root, len(B[i]))

ANS = [0 for i in range(n+1)]

def nuru(k):
    col = ANS[k]
    for j in range(len(B[k])):
        ANS[B[k][j][0]] = (col + B[k][j][1])%2
        nuru(j)

nuru(root)

#print(ANS)

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

Submission status: Runtime Error

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

E = [[] for i in range(n + 1)]

for u, v, w in U:
    E[u].append((v, w))
    E[v].append((u, w))

visited = [False] * (n + 1)
color = [0] * (n + 1)

def dfs(v, w):
    if visited[v]:
        return None
    visited[v] = True
    color[v] = 1 if w % 2 == 1 else 0
    for u, weight in E[v]:
        dfs(u, w + weight)

    return None

dfs(1, 0)

for c in color[1:]:
    print(c)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

class Solution:
    def main(self):
        N = int(input())
        self.hashmap = defaultdict(set)
        for i in range(N-1):
            u, v, w = map(int, input().split())
            self.hashmap[u-1].add((v-1, w))
            self.hashmap[v-1].add((u-1, w))
        self.colors = [None] * N
        for i in range(N):
            self.dfs(i, 0)
            print(self.colors[i])

    def dfs(self, i, color):
        if self.colors[i] is None:
            self.colors[i] = color
            for node, weight in self.hashmap[i]:
                if weight % 2 == 0:
                    self.dfs(node, color)
                else:
                    self.dfs(node, 1 ^ color)


if __name__ == '__main__':
    Solution().main()

Submission status: Runtime Error

Solution code:
n = int(input())
color = [-1] * n
neighbor = [[] for _ in range(n)]
for _ in range(n - 1):
    u, v, w = tuple(map(int, input().split()))
    neighbor[u - 1].append((v-1, w))
    neighbor[v - 1].append((u-1, w))
def col(u, c):
    print(u, c)
    color[u] = c
    for v, w in neighbor[u]:
        if color[v] < 0:
            col(v, (c + w) % 2)
col(0, 0)
for i in range(n):
    print(color[i])