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: Time Limit Exceeded

Solution code:
# D - Even Relation

# ある頂点からの距離の偶奇が一致する点を同じ色で塗る

from scipy.sparse.csgraph import dijkstra

N = int(input())

neighbor = [[0]*N for _ in range(N)]

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

dist_from_0 = dijkstra(neighbor, indices = 0)

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

Submission status: Time Limit Exceeded

Solution code:
# D - Even Relation

# ある頂点からの距離の偶奇が一致する点を同じ色で塗る

from scipy.sparse.csgraph import dijkstra

N = int(input())

neighbor = [[0]*N for _ in range(N)]

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

dist_from_0 = dijkstra(neighbor, indices = 0)

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import scipy.sparse as sparse
from collections import deque

n = int(input())


color_ls = {i:None for i in range(n)}
_map = sparse.lil_matrix((n, n))
already = set()
for i in range(n-1):
    u, v, w = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        root = u
    _map[u,v] = w
    _map[v,u] = w
    already.update({u, v})
color_ls[root] = 0
cur = root
already.clear()
que = deque()
while True:
    candidate = _map[cur].nonzero()[1]
    for cand in candidate:
        if cand in already:
            continue
        already.add(cand)
        que.append(cand)
        if _map[cur,cand] % 2 == 0:
            color_ls[cand] = 0 if color_ls[cur] == 0 else 1
        else:
            color_ls[cand] = 1 if color_ls[cur] == 0 else 0
    if len(que) == 0:
        break
    cur = que.popleft()

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

N=int(input())
out=[set() for i in range(N+1)]
reached=[False for i in range(N+1)]
same=[[False for j in range(N+1)] for i in range(N+1)]

for i in range(N-1):
    u,v,w=map(int,input().split())
    out[u].add(v)
    out[v].add(u)
    same[u][v]=same[v][u]=w%2==0

q=deque()
q.append(1)
reached[1]=True
same1=[False for i in range(N+1)];same1[1]=True

while q:
    u=q.popleft()
    for v in [v for v in out[u] if not reached[v]]:
        same1[v]=same1[u]==same[u][v]
        reached[v]=True
        q.append(v)

for i in range(1,N+1):
    print(0 if same1[i] else 1)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

N=int(input())
out=[set() for i in range(N+1)]
reached=[False for i in range(N+1)]
same=[[False for j in range(N+1)] for i in range(N+1)]

for i in range(N-1):
    u,v,w=map(int,input().split())
    out[u].add(v)
    out[v].add(u)
    same[u][v]=same[v][u]=w%2==0

q=deque()
q.append(1)
reached[1]=True
same1=[False for i in range(N+1)]; same1[1]=True

while q:
    u=q.popleft()
    for v in out[u]:
        if not reached[v]:
            same1[v]=same1[u]==same[u][v]
            reached[v]=True
            q.append(v)

for i in range(1,N+1):
    print(0 if same1[i] else 1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = [[int(x) for x in input().split()] for _ in range(N-1)]

dic = {}
dic_d = {}
visited = [0]*(N+1)
for i in range(N-1):
    if visited[L[i][0]] == 1:
        dic[L[i][0]].append(L[i][1])
        dic_d[L[i][0]].append(L[i][2])
    else:
        dic[L[i][0]] = [L[i][1]]
        dic_d[L[i][0]] = [L[i][2]]
        visited[L[i][0]] = 1
    if visited[L[i][1]] == 1:
        dic[L[i][1]].append(L[i][0])
        dic_d[L[i][1]].append(L[i][2])
    else:
        dic[L[i][1]] = [L[i][0]]
        dic_d[L[i][1]] = [L[i][2]]
        visited[L[i][1]] = 1

ans_list = ["0"]*(N+1)
q = [1]
D = [0]
while q:
    v = q.pop(0)
    d = D.pop(0)
    for i in range(len(dic[v])):
        if visited[dic[v][i]] == 1:
            q.append(dic[v][i])
            D.append((d+dic_d[v][i])%2)
            visited[dic[v][i]] = 2
            if (d+dic_d[v][i])%2 == 1:
                ans_list[dic[v][i]] = "1"
                
for i in range(1, N+1):
    print(ans_list[i])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = [[int(x) for x in input().split()] for _ in range(N-1)]

dic = {}
dic_d = {}
visited = [0]*(N+1)
for i in range(N-1):
    if visited[L[i][0]] == 1:
        dic[L[i][0]].append(L[i][1])
        dic_d[L[i][0]].append(L[i][2])
    else:
        dic[L[i][0]] = [L[i][1]]
        dic_d[L[i][0]] = [L[i][2]]
        visited[L[i][0]] = 1
    if visited[L[i][1]] == 1:
        dic[L[i][1]].append(L[i][0])
        dic_d[L[i][1]].append(L[i][2])
    else:
        dic[L[i][1]] = [L[i][0]]
        dic_d[L[i][1]] = [L[i][2]]
        visited[L[i][1]] = 1

ans_list = ["0"]*(N+1)
q = [1]
D = [0]
visited = [0]*(N+1)
while len(q) > 0:
    v = q.pop(0)
    d = D.pop(0)
    for i in range(len(dic[v])):
        if visited[dic[v][i]] == 0:
            q.append(dic[v][i])
            D.append(d+dic_d[v][i])
            visited[dic[v][i]] = 1
            if (d+dic_d[v][i])%2 == 1:
                ans_list[dic[v][i]] = "1"
                
for i in range(1, N+1):
    print(ans_list[i])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = [[int(x) for x in input().split()] for _ in range(N-1)]

dic = {}
visited = [0]*(N+1)
for i in range(N-1):
    if visited[L[i][0]] == 1:
        dic[L[i][0]].append([L[i][1], L[i][2]])
    else:
        dic[L[i][0]] = [[L[i][1], L[i][2]]]
        visited[L[i][0]] = 1
    if visited[L[i][1]] == 1:
        dic[L[i][1]].append([L[i][0], L[i][2]])
    else:
        dic[L[i][1]] = [[L[i][0], L[i][2]]]
        visited[L[i][1]] = 1

ans_list = ["0"]*(N+1)
q = [[1, 0]]
visited = [0]*(N+1)
while len(q) > 0:
    t = q.pop(0)
    v = t[0]
    d = t[1]
    for i in range(len(dic[v])):
        if visited[dic[v][i][0]] == 0:
            q.append([dic[v][i][0], d+dic[v][i][1]])
            visited[dic[v][i][0]] = 1
            if (d+dic[v][i][1])%2 == 1:
                ans_list[dic[v][i][0]] = "1"
                
for i in range(1, N+1):
    print(ans_list[i])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = [[int(x) for x in input().split()] for _ in range(N-1)]

dic = {}
for i in range(N-1):
    if L[i][0] in dic:
        dic[L[i][0]].append([L[i][1], L[i][2]])
    else:
        dic[L[i][0]] = [[L[i][1], L[i][2]]]
    if L[i][1] in dic:
        dic[L[i][1]].append([L[i][0], L[i][2]])
    else:
        dic[L[i][1]] = [[L[i][0], L[i][2]]]

ans_list = ["0"]*(N+1)
q = [[1, 0]]
visited = [0]*(N+1)
while len(q) > 0:
    t = q.pop(0)
    v = t[0]
    d = t[1]
    for i in range(len(dic[v])):
        if visited[dic[v][i][0]] == 0:
            q.append([dic[v][i][0], d+dic[v][i][1]])
            visited[dic[v][i][0]] = 1
            if (d+dic[v][i][1])%2 == 1:
                ans_list[dic[v][i][0]] = "1"
                
for i in range(1, N+1):
    print(ans_list[i])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = [[int(x) for x in input().split()] for _ in range(N-1)]

dic = {}
for i in range(N-1):
    if L[i][0] in dic:
        dic[L[i][0]].append([L[i][1], L[i][2]])
    else:
        dic[L[i][0]] = [[L[i][1], L[i][2]]]
    if L[i][1] in dic:
        dic[L[i][1]].append([L[i][0], L[i][2]])
    else:
        dic[L[i][1]] = [[L[i][0], L[i][2]]]

ans_list = ["0"]*(N+1)
q = [[1, 0]]
visited = []
while len(q) > 0:
    t = q.pop(0)
    v = t[0]
    d = t[1]
    for i in range(len(dic[v])):
        if dic[v][i][0] not in visited:
            q.append([dic[v][i][0], d+dic[v][i][1]])
            visited.append(dic[v][i][0])
            if (d+dic[v][i][1])%2 == 1:
                ans_list[dic[v][i][0]] = "1"
                
for i in range(1, N+1):
    print(ans_list[i])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
V = [[] for _ in range(N)]
V_dst = [0 for _ in range(N)]

for _ in range(N - 1):
	u, v, w = map(int, input().split())
	V[u - 1].append([v - 1, w])
	V[v - 1].append([u - 1, w])
	
visited = [False for _ in range(N)]
visited[0] = True
queue = [0]

while queue:
	cur = queue.pop(0)
	for adj, length in V[cur]:
		if not visited[adj]:
			visited[adj] = True
			V_dst[adj] = V_dst[cur] ^ length
			queue.append(adj)
			

for v in V_dst:
	if v % 2 == 0:
		print(0)
	else:
		print(1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
V = [[] for _ in range(N)]
V_dst = [0 for _ in range(N)]

for _ in range(N - 1):
	u, v, w = map(int, input().split())
	V[u - 1].append([v - 1, w])
	V[v - 1].append([u - 1, w])
	
visited = [False for _ in range(N)]
visited[0] = True
queue = [0]

while queue:
	cur = queue.pop(0)
	for adj, length in V[cur]:
		if not visited[adj]:
			visited[adj] = True
			V_dst[adj] = V_dst[cur] + length
			queue.append(adj)
			

for v in V_dst:
	if v % 2 == 0:
		print(0)
	else:
		print(1)

Submission status: Time Limit Exceeded

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

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

inf = float("inf")
E = [[inf for _ in range(N+1)] for _ in range(N+1)]
for i in range(N-1):
	t = w[i] % 2
	E[u[i]][v[i]] = t
	E[v[i]][u[i]] = t

flg = [inf for _ in range(N+1)]
flg[0] = 0

def dfs(q):
	cp = q.pop()
	next = [i for i in range(1,N+1) if E[cp][i] != inf]

	for np in next:
		if flg[np] == inf:
			if E[cp][np] == 0:
				flg[np] = flg[cp]
			else:
				flg[np] = flg[cp] * (-1)

			q.append(np)
	if len(q) == 0:
		return

	dfs(q)

flg[1] = 1
dfs([1])

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def solve():
  n = int(input())
  treepath = [[False]*n for _ in range(n)]
  treenode = [[]*n for _ in range(n)]
  treecolor = [-1]*n
  for i in range(n-1):
    u,v,w = (int(i) for i in input().split())
    u -= 1
    v -= 1
    treenode[u].append(v)
    treenode[v].append(u)
    treepath[u][v] = w%2
    treepath[v][u] = w%2
  query = deque([(0,0,0)])
  while query:
    a,b,c = query.popleft()
    if b%2 == 0:
      treecolor[a] = c
      nextc = c
    else:
      treecolor[a] = 1^c
      nextc = 1^c
    for nextnode in treenode[a]:
      if treecolor[nextnode] != -1:
        continue
      else:
        query.append((nextnode,treepath[a][nextnode],nextc))  
        treecolor[nextnode] = -2
  #print(treecolor)
  for i in treecolor:
    print(i)
solve()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def solve():
  n = int(input())
  treepath = [[False]*n for _ in range(n)]
  treenode = [[]*n for _ in range(n)]
  treecolor = [-1]*n
  for i in range(n-1):
    u,v,w = (int(i) for i in input().split())
    u -= 1
    v -= 1
    treepath[u][v] = w%2
    treepath[v][u] = w%2
    treenode[u].append(v)
    treenode[v].append(u)

  query = deque([(0,0,0)])
  while query:
    a,b,c = query.popleft()
    if b%2 == 0:
      treecolor[a] = c
      nextc = c
    else:
      treecolor[a] = 1^c
      nextc = 1^c
    for nextnode in treenode[a]:
      if treecolor[nextnode] != -1:
        continue
      else:
        query.append((nextnode,treepath[a][nextnode],nextc))  
  #print(treecolor)
  for i in treecolor:
    print(i)
solve()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
ls=[[int(s) for s in input().split()] for _ in range(n-1)]
ans=[-1]*(n+1)
ans[1]=0
position=1
e_num=n-1
pan=[0]
while e_num>0:
  for i in range(e_num):
    if ls[i][0]==position:
      next_pos=ls[i][1]
      d=ls[i][2]
      ls.pop(i)
      break
    elif ls[i][1]==position:
      next_pos=ls[i][0]
      d=ls[i][2]
      ls.pop(i)
      break
  if next_pos==0:
    position=pan[-1]
    pan.pop(-1)
  else:
    ans[next_pos]=(ans[position]+d)%2
    pan.append(position)
    position=next_pos
    next_pos=0
    e_num-=1
for i in range(1,n+1):
  print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter

N = int(input())
UVW = sorted([list(map(int, input().split())) for _ in range(N-1)], key=itemgetter(0))
graph = [[-1] * N for _ in range(N)]

for u, v, w in UVW:
    graph[u-1][v-1] = w % 2
    graph[v-1][u-1] = w % 2
d = [-1] * N
d[0] = 0
def f(i):
    next_list = []
    for j in range(N):
        if i == j:
            continue
        if d[j] == -1 and graph[i][j] != -1 :
            d[j] = (d[i] + graph[i][j]) % 2
            next_list.append(j)
    for n in next_list:
        f(n)    
f(0)
for x in d:
    print(x)

Submission status: Time Limit Exceeded

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
ps = [-1] * n


def dfs(s):
    global ds, colors, ps

    stack = []

    ds[s] = 0
    colors[s] = BLACK
    ps[s] = -1
    stack.append(ds[s])

    while stack:
        u = stack[-1]
        colors[u] = GRAY

        for v, w in g[u]:
            if colors[v] == WHITE:
                stack.append(v)
                ds[v] = ds[u] + w
                ps[v] = u
                break

        else:
            stack.pop()
            colors[u] = BLACK

    return ds, ps


dfs(0)

for d in ds:
    print(d % 2)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
INF = 100100100

def solve(M: list, d: list, visited: list, u: int = 0):
    global N
    visited[u] = True
    for i in range(N):
        if M[u][i] != INF and not visited[i]:
            d[i] = d[u] + M[u][i]
            solve(M, d, visited, i)


def main():
    global N
    N = int(input())
    M = [[INF]*N for _ in range(N)]
    for _ in range(N-1):
        u, v, w = map(int, input().split())
        M[u-1][v-1] = w
        M[v-1][u-1] = w
    visited = [False] * N
    d = [0] * N
    solve(M, d, visited)
    for i in d:
        if i%2 == 0:
            print(0)
        else:
            print(1)

if __name__ == '__main__': main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    N = int(input())
    G = [[] for _ in range(N)]
    for i in range(N-1):
        u, v, w = (int(i) for i in input().split())
        G[u-1].append((v-1,w))
        G[v-1].append((u-1,w))
    color = [-1] * N
    todo = []
    todo_append = todo.append
    todo_pop = todo.pop
    color[0] = 0
    todo_append(0)
    while todo:
        u = todo[-1]
        for next_v,w in G[u]:
            if color[next_v] != -1: 
                continue
            if w&1:
                color[next_v] = (color[u] + 1) % 2
            else:
                color[next_v] = color[u]
            todo_append(next_v)
            break
        else:
            todo_pop(-1)
    for c in color:
        print(c)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    N = int(input())
    G = [[] for i in range(N + 1)]
    for i in range(N-1):
        u, v, w = (int(i) for i in input().split())
        G[u].append((v,w))
        G[v].append((u,w))
    color = [-1] * (N + 1)
    todo = []
    todo_append = todo.append
    todo_pop = todo.pop
    cur = 0
    color[1] = cur
    todo_append(1)
    while todo:
        u = todo[-1]
        for next_v,w in G[u]:
            if color[next_v] != -1: 
                continue
            if w&1:
                color[next_v] = (color[u] + 1) % 2
            else:
                color[next_v] = color[u]
            todo_append(next_v)
            break
        else:
            todo_pop(-1)
    for i in range(1,N+1):
        print(color[i])
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    N = int(input())
    G = [[] for i in range(N + 1)]
    for i in range(N-1):
        u, v, w = (int(i) for i in input().split())
        G[u].append((v,w))
        G[v].append((u,w))
    def is_bipartite_by_dfs(G,v,color):
        todo = []
        todo_append = todo.append
        todo_pop = todo.pop
        cur = 0
        color[v] = cur
        todo_append(v)
        while todo:
            u = todo[-1]
            for next_v,w in G[u]:
                if color[next_v] != -1: 
                    continue
                if w&1:
                    color[next_v] = (color[u] + 1) % 2
                else:
                    color[next_v] = color[u]
                todo_append(next_v)
                break
            else:
                todo_pop(-1)
    color = [-1] * (N + 1) # 0: white, 1: black, -1: undefined
    is_bipartite_by_dfs(G,1,color)
    for i in range(1,N+1):
        print(color[i])
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    G = [[] for i in range(N + 1)]
    for i in range(N-1):
        u, v, w = (int(i) for i in input().split())
        G[u].append((v,w))
        G[v].append((u,w))
    def is_bipartite_by_dfs(G,v,color):
        todo = []
        cur = 0
        color[v] = cur
        todo.append(v)
        while todo:
            u = todo[-1]
            for next_v,w in G[u]:
                if color[next_v] != -1: 
                    continue
                if w&1:
                    color[next_v] = (color[u] + 1) % 2
                else:
                    color[next_v] = color[u]
                todo.append(next_v)
                break
            else:
                todo.pop()
    color = [-1] * (N + 1) # 0: white, 1: black, -1: undefined
    is_bipartite_by_dfs(G,1,color)
    for i in range(1,N+1):
        print(color[i])
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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])
        i -= 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: Time Limit Exceeded

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
dist = [0] * n

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

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

for c in color:
    print(c)

Submission status: Time Limit Exceeded

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
dist = [0] * n

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

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

for c in color:
    print(c)

Submission status: Time Limit Exceeded

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 and g[v][next_v] != 0:
            if 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: Time Limit Exceeded

Solution code:
#coding utf-8
N=int(input())
uvw=[[int(i) for i in input().split()] for i in range(N-1)]
ans=[-1 for i in range(N+1)]
ans[1]=0
uvw.sort()
 
while(len(uvw)>0):
  if ans[uvw[0][0]]>=0 or ans[uvw[0][1]]>=0:
    if ans[uvw[0][0]]>=0:
      if uvw[0][2]%2==0:
        ans[uvw[0][1]]=ans[uvw[0][0]]
      else:
        ans[uvw[0][1]]=1-ans[uvw[0][0]]
    else:
      if uvw[0][2]%2==0:
        ans[uvw[0][0]]=ans[uvw[0][1]]
      else:
        ans[uvw[0][0]]=1-ans[uvw[0][1]]
    del uvw[0]
  else:
    uvw.append(uvw[0])
    del uvw[0]  
  
for i in range(1,N+1):
  print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
#coding utf-8
N=int(input())
uvw=[[int(i) for i in input().split()] for i in range(N-1)]
ans=[-1 for i in range(N+1)]
ans[1]=0
uvw.sort()

while(len(uvw)>0):
  if ans[uvw[0][0]]>=0 or ans[uvw[0][1]]>=0:
    if ans[uvw[0][0]]>=0:
      if uvw[0][2]%2==0:
        ans[uvw[0][1]]=ans[uvw[0][0]]
      else:
        ans[uvw[0][1]]=1-ans[uvw[0][0]]
    else:
      if uvw[0][2]%2==0:
        ans[uvw[0][0]]=ans[uvw[0][1]]
      else:
        ans[uvw[0][0]]=1-ans[uvw[0][1]]
    del uvw[0]
  else:
    uvw.append(uvw[0])
    del uvw[0]  
  
for i in range(1,N+1):
  print(ans[i])

Submission status: Time Limit Exceeded

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 u,v,w in uvw:
        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: Time Limit Exceeded

Solution code:
N=int(input())

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

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

nlist=[]

def search(A,nlist):
	global d	
	t=[]
	for ne in nlist:
		for a,b,w in A:
			if a==ne and d[b]==-1:
				d[b]=d[a] if w%2==0 else (d[a]+1)%2
				t.append(b)
			if b==ne and d[a]==-1:
				d[a]=d[b] if w%2==0 else (d[b]+1)%2
				t.append(a)
	
	if t!=[]:
		search(A,t)

search(A,[1])
print(*d[1:],sep='\n')

Submission status: Time Limit Exceeded

Solution code:
def dijkstra(s,n,w,cost):
    #始点sから各頂点への最短距離
    #n:頂点数,　w:辺の数, cost[u][v] : 辺uvのコスト(存在しないときはinf)
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0
    
    while True:
        v = -1
        #まだ使われてない頂点の中から最小の距離のものを探す
        for i in range(n):
            if (not used[i]) and (v == -1):
               v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
               break
        used[v] = True
               
        for j in range(n):
               d[j] = min(d[j],d[v]+cost[v][j])
    return d

################################
n = int(input())

cost = [[float("inf") for i in range(n)] for i in range(n)] 
#cost[u][v] : 辺uvのコスト(存在しないときはinf この場合は10**10)
for i in range(n-1):
    x,y,z = map(int,input().split())
    cost[x-1][y-1] = z
    cost[y-1][x-1] = z
dks=dijkstra(0,n,n-1,cost)
for i in dks:
    if i%2==0:print(0)
    else:print(1)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
def main():
    N = int(input())
    X = [list(map(int,input().split())) for _ in range(N-1)]
    tree = [[] for _ in range(N+1)]
    for i in range(N-1):
        tree[X[i][0]-1].append(X[i][1])
        tree[X[i][1]-1].append(X[i][0])
    dif = [0 for _ in range(N)]
    queue = [1]
    while queue:
        i = queue.pop()
        for j in range (N-1):
            if X[j][0] == i and X[j][1] in tree[i-1]:
                dif[X[j][1]-1] = dif[i-1] + X[j][2]
                tree[X[j][1]-1].remove(i)
            if X[j][1] == i and X[j][0] in tree[i-1]:
                dif[X[j][0]-1] = dif[i-1] - X[j][2]
                tree[X[j][0]-1].remove(i)
        queue += tree[i-1]
        tree[i-1].clear()

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

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy
import sys

sys.setrecursionlimit(10 ** 7)

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

# 適当に最初の色を決める
# その点を親として、
# 親からの距離が偶数だったら親と同じ色
# 親からの距離が奇数だったら親と違う色

pool = [1]

result = [0 for i in range(N)]  # 頂点1は白くする

while pool:
    top = pool.pop()
    copy_uvw = copy.deepcopy(uvw)
    for e in copy_uvw:
        if top == e[0]:
            uvw.remove(e)
            pool.append(e[1])
            if e[2] % 2 == 0:
                # 親と同じ色を入れる
                result[e[1] - 1] = 0 if result[e[0] - 1] == 0 else 1
            else:
                # 親と違う色を入れる
                result[e[1] - 1] = 1 if result[e[0] - 1] == 0 else 0
        elif top == e[1]:
            uvw.remove(e)
            pool.append(e[0])
            if e[2] % 2 == 0:
                # 親と同じ色を入れる
                result[e[0] - 1] = 0 if result[e[1] - 1] == 0 else 1
            else:
                # 親と違う色を入れる
                result[e[0] - 1] = 1 if result[e[1] - 1] == 0 else 0

for i in result:
    print(i)

Submission status: Time Limit Exceeded

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

ans=[0]*(N+1)
his=[False for i in range(N+1)]
his[0]=True
his[1]=True
idx=1


def search(A,his,ans,idx):
	if all(his):
		return
	for a in A:
		if idx in a[:2]:
			next_idx=a[(a[:2].index(idx)+1)%2]
			if his[next_idx]==False:
				ans[next_idx]=ans[idx]
				if a[2]!=0:
					ans[next_idx]+=1
					ans[next_idx]%=2
				his[next_idx]=True
				search(A,his,ans,next_idx)
				his[next_idx]=False

search(A,his,ans,idx)
print(*ans[1:],sep='\n')

Submission status: Time Limit Exceeded

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

#print(edge)
#print(length)

def bfs(u,v):
  for e in edge[u]:
    if e != v:
      ans[e] = (length[e][u] + ans[u]) % 2
      bfs(e,u)

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

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(20000000)
input=sys.stdin.readline
N=int(input())

G = [[-1 for i in range(N)] for j in range(N)]
color = [0]*N

for i in range(N-1):
    e,f,g = list(map(int, input().split()))
    G[e-1][f-1]=g%2
    G[f-1][e-1]=g%2

def dfs(v,p,c):
    color[v]=c
    for i in range(N):
        if i==p:
            continue
        elif G[v][i] ==0 :
            dfs(i,v,c)
        elif G[v][i] ==1 :
            dfs(i,v,1-c)
        
dfs(0,-1,0)

for k in color:
    print(k)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(20000000)
N=int(input())

G = [[-1 for i in range(N)] for j in range(N)]
color = [0]*N

for i in range(N-1):
    e,f,g = list(map(int, input().split()))
    G[e-1][f-1]=g%2
    G[f-1][e-1]=g%2

def dfs(v,p,c):
    color[v]=c
    for i in range(N):
        if i==p:
            continue
        elif G[v][i] ==0 :
            dfs(i,v,c)
        elif G[v][i] ==1 :
            dfs(i,v,1-c)
        
dfs(0,-1,0)

for k in color:
    print(k)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = [[] for _ in range(n+1)]
R = [[] for _ in range(n+1)]
for _ in range(n-1):
    u, v, w = map(int, input().split())
    w %= 2
    L[u] += [[u, v, w]]
    R[v] += [[v, u, w]]
P = [[] for _ in range(2*n+2)]
P[1] += (L[1]+R[1])
for i in range(1, 2*n):
    for _, v, w in P[i]:
        P[i+2-w] += (L[v]+R[v])
A = []
for p in P[1::2]: A += p
B = set([u for u, *_ in A])
for i in range(1, n+1): print(1 if i in B else 0)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
T = []
for _ in range(n-1):
    u, v, w = map(int, input().split())
    T += [[u, v, w%2]]
P = [0]+[-1]*(n-1)
while T:
    T_sub = []
    for t in T:
        u, v, w = t
        pu, pv = P[u-1], P[v-1]
        if (pu < 0) & (pv < 0):
            T_sub += [t]
        elif (pu >= 0) & (pv < 0):
            P[v-1] = (pu+w)%2
        else:
            P[u-1] = (pv+w)%2
    T = T_sub
for p in P: print(p)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
T = []
for _ in range(n-1):
    T += [list(map(int, input().split()))]
P = [0]+[-1]*(n-1)
while T:
    T_sub = []
    for t in T:
        u, v, w = t
        pu, pv = P[u-1], P[v-1]
        if (pu < 0) & (pv < 0):
            T_sub += [t]
        elif (pu >= 0) & (pv < 0):
            P[v-1] = (pu+(w%2))%2
        else:
            P[u-1] = (pv+(w%2))%2
    T = T_sub
for p in P: print(p)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
T = []
for _ in range(n-1):
    T += [list(map(int, input().split()))]
P = [0]+[-1]*(n-1)
T_sub = []
while T:
    for t in T:
        u, v, w = t
        pu, pv = P[u-1], P[v-1]
        if (pu < 0) & (pv < 0):
            T_sub += [t]
        elif (pu >= 0) & (pv < 0):
            P[v-1] = (pu+(w%2))%2
        else:
            P[u-1] = (pv+(w%2))%2
    T, T_sub = T_sub, []
for p in P: print(p)