Problem:


Score : 400 points

Problem Statement

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

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

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

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq u_i < v_i \leq N

1 \leq w_i \leq 10^9

Input

Input is given from Standard Input in the following format:

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

Output

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

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

Sample Input 1

3
1 2 2
2 3 1

Sample Output 1

0
0
1

Sample Input 2

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

Sample Output 2

1
0
1
0
1



Sample input:
3
1 2 2
2 3 1


Sample output:
0
0
1

Submission status: Accepted

Solution code:
n = int(input())
T = [[] for i in range(n)] #making the list
D = [-1] * n #making the undis pos
D[0] = 0 #first pos

#this mean Que
Q = [0] #insert fist pos to Que

for i in range(n - 1):
    u, v, w = map(int, input().split())
    T[u - 1].append([v - 1, w])
    T[v - 1].append([u - 1, w])
while Q:
    q = Q.pop()
    for adj, w in T[q]: ##BFS
        if D[adj] < 0: #uncertain is = -1
            D[adj] = (D[q] + w)%2
            Q.append(adj)
for d in D:
    print(d)

Submission status: Accepted

Solution code:
n = int(input())
T = [[] for i in range(n)]
for i in range(n - 1):
    u, v, w = map(int, input().split())
    T[u - 1].append([v - 1, w])
    T[v - 1].append([u - 1, w])
D = [-1] * n
D[0] = 0
Q = [0]
while Q:
    q = Q.pop()
    for adj, w in T[q]: ##BFS
        if D[adj] < 0: #uncertain is = -1
            D[adj] = D[q] + w
            Q.append(adj)
for d in D:
    print(d % 2)

Submission status: Accepted

Solution code:
N = int(input())
tree = [list() 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))
stack = [0]
ans = [0] * N
searched = [False] * N
while stack:
    node = stack.pop()
    searched[node] = True
    for v, w in tree[node]:
        if searched[v]:
            continue
        if w % 2 != 0:
            ans[v] = ans[node] ^ 1
        else:
            ans[v] = ans[node]
        stack.append(v)
print(*ans, sep='\n')

Submission status: Accepted

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

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


def dfs(edge, pd):
    for child, d in edges[edge]:
        if ans[child-1] != -1:
            continue
        cd = pd + d
        if cd % 2 == 0:
            ans[child-1] = 0
        else:
            ans[child-1] = 1
        dfs(child, cd)


def solve():
    dfs(1, 0)
    for i in range(N):
        print(ans[i])


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys
from collections import deque
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

def main():
	N = INT()
	UVW = [MAP() for _ in range(N-1)]
	
	G = [[] for _ in range(N)]
	for u, v, w in UVW:
		G[u-1].append([v-1, w])
		G[v-1].append([u-1, w])
		
	C = [-1] * N
	C[0] = 0
	queue = deque()
	for i in range(N):
		queue.append(i)
		while queue:
			v = queue.popleft()
			for child in G[v]:
				nv, dist = child
				if C[nv] != -1: continue
				if not dist % 2:
					# print('iro onaji', v, child)
					C[nv] = C[v]
				else:
					# print('iro kaeru', v, child)
					C[nv] = C[v] ^ 1
				queue.appendleft(nv)
	
	for c in C:
		print(c)

if __name__ == '__main__':
	main()

Submission status: Accepted

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
edges = [[] for _ in range(n)]

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

ans = [None]*n
ans[0] = 0

que = deque()
que.append(0)
while que:
    v = que.popleft()
    for u,c in edges[v]:
        if ans[u] != None:
            continue
        if c % 2 == 0:
            ans[u] = ans[v]
        else:
            ans[u] = (ans[v]+1) % 2
        que.append(u)
print(*ans)

Submission status: Accepted

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

N = int(input())

tree = {}
for i in range(1,N):
    u, v, w = map(int, input().split())
    w = w%2
    if u in tree:
        tree[u].append([v,w])
    else:
        tree[u] = [[v,w]]

    if v in tree:
        tree[v].append([u,w])
    else:
        tree[v] = [[u,w]]

iro = [-1]*(N)
def seachTree(current, parent):
    if len(tree[current]) == 0:
        return

    for elms in tree[current]:
        if elms[0] == parent:
            continue
        if elms[1] == 0:
            iro[elms[0]-1] = iro[current-1]
        else:
            iro[elms[0]-1] = not iro[current-1]

        seachTree(elms[0], current)

iro[0] = False
seachTree(1, 0)
for val in iro:
    print(int(val))

Submission status: Accepted

Solution code:
N = int(input())
edge = [set() for i in range(N)]
for i in range(N-1): #辺の情報と距離を記録
  u, v, w = map(int, input().split())
  edge[u-1].add((v-1, w))
  edge[v-1].add((u-1, w))

#幅優先検索
dist = [0] * N #距離
stack = [(0,-1)] #vrt, from
while stack: #stackが空になるまで
  vrt, _from = stack.pop()
  for _to, w in edge[vrt].copy():
    if _to == _from:
      continue
    stack.append((_to, vrt))
    dist[_to] = dist[vrt] + w
    
for i in range(N):
  print(dist[i]%2)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(pow(10, 7))

def dfs(v, c):
	color[v] = c
	for x in glaph[v]:
		u, w = x[0], x[1]
		if color[u] != -1:
			continue
		if w%2 == 0:
			dfs(u, c)
		else:
			dfs(u, 1-c)
			
n = int(input())
color = [-1]*n
glaph = [[]*n for _ in range(n)]
for _ in range(n-1):
	u,v,w = map(int, input().split())
	glaph[u-1].append((v-1, w))
	glaph[v-1].append((u-1, w))	

dfs(v=0, c=0)
for i in color:
	print(i)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(pow(10, 7))

def dfs(v, c, parent):
	color[v] = c
	for x in glaph[v]:
		u, w = x[0], x[1]
		if color[u] != -1:
			continue
		if w%2 == 0:
			dfs(u, c, v)
		else:
			dfs(u, 1-c, v)
			
n = int(input())
glaph = [[]*n for _ in range(n)]
color = [-1]*n

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

dfs(v=0, c=0, parent=0)
#for i in range(n):
#	print(i, glaph[i])
for i in color:
	print(i)

Submission status: Accepted

Solution code:
import heapq
import sys

input = sys.stdin.readline

def dijkstra_heap(s,edge,n):
    #始点sから各頂点への最短距離
    d = [float("inf")] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(pow(10, 7))


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


N = int(input())

G = [[] for _ in range(N)]
res = [0] * N
for _ in range(N-1):
    u, v, w = map(int, input().split())
    G[u-1].append([v-1, w])
    G[v-1].append([u-1, w])
dfs(0, -1, 1)
print('\n'.join(map(str, res)))

Submission status: Accepted

Solution code:
n = int(input())
tree = [list() for _ in range(n)]
ans = [1] * 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))

stack = [0]
judge = [False] * n
while stack:
    node = stack.pop()
    judge[node] = True
    for snode, distance in tree[node]:
        if judge[snode]:
            continue
        if distance % 2 != 0:
            ans[snode] = ans[node] ^ 1
        else:
            ans[snode] = ans[node]
        stack.append(snode)
print(*ans, sep='\n')

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    d = {}
    for _ in range(N-1):
        u, v, w = map(int, input().split())
        w %= 2
        if u in d:
            d[u].append((v, w))
        else:
            d[u] = [(v,w)]
        if v in d:
            d[v].append((u, w))
        else:
            d[v] = [(u,w)]
    cur = [1]
    nextp = []
    l = [0] * N
    v = set(range(1, N+1))
    while v:
        for i in cur:
            v.remove(i)
            for j, k in d[i]:
                if j in v:
                    l[j-1] = 1 - l[i-1] if k else l[i-1]
                    nextp.append(j)
        cur = nextp
        nextp = []
    return l
l = main()
for i in l:
    print(i)

Submission status: Accepted

Solution code:
from collections import Counter, deque

N = int(input())

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

udict ={}
uvdict = {}
lst = []
ans = [-1]*(N+1)
for u, v, w in uvw:
    if u in udict:udict[u].append(v)
    else:udict[u] =[v]
    if v in udict:udict[v].append(u)
    else:udict[v] =[u]
    lst.append(u)
    lst.append(v)
    uvdict[(u,v)] = w
cnt = Counter(lst)

cnt_max = cnt.most_common()[0][0]
nxt = deque()
nxt.append(cnt_max)
passed = [0]*(N+1)

d = [0]*(N+1)

while nxt!=deque([]):
    l1 = nxt.popleft()
    for l2 in udict[l1]:
        if passed[l2]==1:continue
        d[l2] = d[l1] + uvdict[min(l1, l2), max(l1, l2)]
        
        nxt.append(l2)
    passed[l1]=1
for i in d[1:]:
    print(i%2)

Submission status: Accepted

Solution code:
n=int(input())

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

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

import queue

q=queue.Queue()
q.put(0)

while not q.empty():
	now=q.get()

	for ne,weight in g[now]:
		if color[ne]!=-1:
			continue
		if weight%2==0:
			color[ne]=color[now]
		else:
			color[ne]=abs(color[now]-1)
		q.put(ne)

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
from collections import deque

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

# 親を探索(頂点0を根)
parent = [0] * N
for v,_ in edge[0]:
    parent[v] = 0
open_list = deque([0])
while open_list:
    now = open_list.popleft()
    for v,w in edge[now]:
        if v == parent[now]:
            continue
        open_list.append(v)
        parent[v] = now


# 根からの距離を探索
path = [0] * N
open_list = deque([0])
while open_list:
    now = open_list.popleft()
    for v,w in edge[now]:
        if v == parent[now]:
            continue
        open_list.append(v)
        path[v] = path[now] + w

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

Submission status: Accepted

Solution code:
from collections import deque

N=int(input())
a=[set() for v in range(N)] # 0-initial
for e in range(1,N):
    u,v,w=map(int,input().split())
    a[u-1].add((v-1,w))
    a[v-1].add((u-1,w))

d=[-1 for v in range(N)] # distance from 0

q=deque()
q.append(0);d[0]=0
while q:
    u=q.popleft()
    for v,w in a[u]:
        if d[v]==-1:
            d[v]=d[u]+w
            q.append(v)

for di in d:
    print(di%2)

Submission status: Accepted

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

stack = [0]
ans = [-1]*N
ans[0] = 0
while stack:
    x = stack.pop()
    for v,w in e_list[x]:
        if ans[v] != -1:
            continue
        else:
            ans[v] = ans[x]^w
            stack.append(v)
print(*ans,sep='\n')

Submission status: Accepted

Solution code:
#d
n=int(input())
tree=[[] for _ 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))

color=[0]*(n+1)
q = [(-1,0,0)]
while q:
    parent,x,l = q.pop()
    for y in tree[x]:
        if y[0] == parent:
            continue
        q.append((x,y[0],y[1]))
    if l%2==1:
        color[x+1]=1-color[parent+1]
    else:
        color[x+1]=color[parent+1]

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

Submission status: Accepted

Solution code:
from collections import deque
N=int(input())
ten=[0]*N
G=[[] for i 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])
que=deque()
que.append(0)
visited=[[0] for i in range(N)]
visited[0]=1
while que:
    v=que.pop()
    for u in G[v]:
        #print(u)
        if visited[u[0]]==1:
            continue
        if u[1]%2==0:
            ten[u[0]]=ten[v]
        else:
            if ten[v]==0:
                ten[u[0]]=1
                #print("a",ten[u[0]])
            else:
                ten[u[0]]=0
        #print(ten)
        visited[u[0]]=1
        que.appendleft(u[0])
for i in range(N):
    print(ten[i])

Submission status: Accepted

Solution code:
from collections import deque
N=int(input())
ten=[0]*N
G=[[] for i 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])
#print(G)
que=deque()
que.append(0)
visited=[[0] for i in range(N)]
visited[0]=1
while que:
    v=que.pop()
    for u in G[v]:
        #print(u)
        if visited[u[0]]==1:
            continue
        if u[1]%2==0:
            ten[u[0]]=ten[v]
        else:
            ten[u[0]]=1-ten[v]
        #print(ten)
        visited[u[0]]=1
        que.appendleft(u[0])
for i in range(N):
    print(ten[i])

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(100000)

N = int(input())
tree = [[] for i in range(N)]
color = [-1] * 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))


def dfs(u):
  for v, w in tree[u]:
    if color[v] == -1:
      color[v] = color[u] ^ (w % 2)
      dfs(v)


color[0] = 0
dfs(0)
print('\n'.join(map(str, color)))

Submission status: Accepted

Solution code:
from collections import deque

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

check = [-1] * (N + 1)
check[1] = 0
D = deque()
D.append(1)

while D:
  d=D.popleft()
  for a in A[d]:
    if check[a[0]]==-1:
      D.append(a[0])
      check[a[0]]=(check[d]+a[1])%2

for i in range(1,len(check)):
  print(check[i])