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:
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
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()))]
T.sort()
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
        elif (pv >= 0) & (pu < 0):
            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()))]
T.sort()
P = [0]+[-1]*(n-1)
while T:
    for t in T:
        u, v, w = t
        pu, pv = P[u-1], P[v-1]
        if (pu >= 0) & (pv < 0):
            P[v-1] = (pu+(w%2))%2
            T.remove(t)
        if (pv >= 0) & (pu < 0):
            P[u-1] = (pv+(w%2))%2
            T.remove(t)
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()))]
T.sort()
P = [0]+[-1]*(n-1)
b = n-1
while b > 0:
    for u, v, w in T:
        pu, pv = P[u-1], P[v-1]
        if (pu >= 0) & (pv < 0):
            P[v-1] = (pu+(w%2))%2
            b -= 1
        if (pv >= 0) & (pu < 0):
            P[u-1] = (pv+(w%2))%2
            b -= 1
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()))]
T.sort()
P = [0]+[-1]*(n-1)
b = n-1
while b > 0:
    for u, v, w in T:
        pu, pv = P[u-1], P[v-1]
        if (pu >= 0) & (pv < 0):
            P[v-1] = (pu+(w%2))%2
            b -= 1
        if (pv >= 0) & (pu < 0):
            P[u-1] = (pv+(w%2))%2
            b -= 1
for p in P: print(p)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
uvw = []
for i in range(n-1):
    uvw.append(list(map(int, input().split(' '))))
result = [0]*(n+1)
result[uvw[0][1]] = uvw[0][2]
s = uvw[0][0]
del uvw[0]
n_visit = []
horyuu = []
count = n-2
while count > 0:
    for i in range(len(uvw)):
        if uvw[i][1] != s and uvw[i][0] != s:
            if result[uvw[i][0]] == 0 and result[uvw[i][1]] != 0:
                result[uvw[i][0]] =  result[uvw[i][1]] + uvw[i][2]
                count -= 1
            elif result[uvw[i][0]] != 0 and result[uvw[i][1]] == 0:
                result[uvw[i][1]] =  result[uvw[i][0]] + uvw[i][2]
                count -= 1
        else:
            if uvw[i][1] != s and uvw[i][0] == s:
                result[uvw[i][1]] = uvw[i][2]
                count -= 1
            elif uvw[i][0] != s and uvw[i][1] == s:
                result[uvw[i][0]] = uvw[i][2]
                count -= 1
            else:
                horyuu.append(uvw[i])
    uvw = horyuu
    horyuu = []

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
uvw = []
for i in range(n-1):
    uvw.append(list(map(int, input().split(' '))))
uvw.sort()
result = [0]*(n+1)
result[uvw[0][1]] = uvw[0][2]
s = uvw[0][0]
del uvw[0]
n_visit = []
horyuu = []
count = n-2
while count > 0:
    for i in range(len(uvw)):
        if uvw[i][1] != s and uvw[i][0] != s:
            if result[uvw[i][0]] == 0 and result[uvw[i][1]] != 0:
                result[uvw[i][0]] =  result[uvw[i][1]] + uvw[i][2]
                count -= 1
            elif result[uvw[i][0]] != 0 and result[uvw[i][1]] == 0:
                result[uvw[i][1]] =  result[uvw[i][0]] + uvw[i][2]
                count -= 1
        else:
            if uvw[i][1] != s and uvw[i][0] == s:
                result[uvw[i][1]] = uvw[i][2]
                count -= 1
            elif uvw[i][0] != s and uvw[i][1] == s:
                result[uvw[i][0]] = uvw[i][2]
                count -= 1
            else:
                horyuu.append(uvw[i])
    uvw = horyuu
    horyuu = []

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
uvw = []
for i in range(n-1):
    uvw.append(list(map(int, input().split(' '))))
uvw.sort()
result = [0]*(n+1)
result[uvw[0][1]] = uvw[0][2]
s = uvw[0][0]
del uvw[0]
n_visit = []
horyuu = []
count = n-2
while count > 0:
    for i in range(len(uvw)):
        if result[uvw[i][0]] == 0 and result[uvw[i][1]] != 0 and uvw[i][0] != s:
            result[uvw[i][0]] =  result[uvw[i][1]] + uvw[i][2]
            count -= 1
        elif result[uvw[i][0]] != 0 and result[uvw[i][1]] == 0 and uvw[i][1] != s:
            result[uvw[i][1]] =  result[uvw[i][0]] + uvw[i][2]
            count -= 1
        else:
            if uvw[i][1] != s and uvw[i][0] == s:
                result[uvw[i][1]] = uvw[i][2]
                count -= 1
            elif uvw[i][0] != s and uvw[i][1] == s:
                result[uvw[i][0]] = uvw[i][2]
                count -= 1
            else:
                horyuu.append(uvw[i])
    uvw = horyuu
    horyuu = []

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
uvw = []
for i in range(n-1):
    uvw.append(list(map(int, input().split(' '))))
result = [0]*(n+1)
result[uvw[0][1]] = uvw[0][2]
s = uvw[0][0]
del uvw[0]
n_visit = []
horyuu = []
count = n-2
while count > 0:
    for i in range(len(uvw)):
        if result[uvw[i][0]] == 0 and result[uvw[i][1]] != 0 and uvw[i][0] != s:
            result[uvw[i][0]] =  result[uvw[i][1]] + uvw[i][2]
            count -= 1
        elif result[uvw[i][0]] != 0 and result[uvw[i][1]] == 0 and uvw[i][1] != s:
            result[uvw[i][1]] =  result[uvw[i][0]] + uvw[i][2]
            count -= 1
        else:
            if uvw[i][1] != s and uvw[i][0] == s:
                result[uvw[i][1]] = uvw[i][2]
                count -= 1
            elif uvw[i][0] != s and uvw[i][1] == s:
                result[uvw[i][0]] = uvw[i][2]
                count -= 1
            else:
                horyuu.append(uvw[i])
    uvw = horyuu
    horyuu = []

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

Submission status: Time Limit Exceeded

Solution code:

N = int(input())

uvw = []
Nlis = [-1] * N
Ndic = {}

for i in range(N-1):

    u,v,w = map(int,input().split())
    
    if u not in Ndic:
        Ndic[u] = [[v,w]]

    else:
        Ndic[u].append([v,w])

    if v not in Ndic:
        Ndic[v] = [[u,w]]

    else:
        Ndic[v].append([u,w])


ends = set([1])
Q = [1]
Nlis[0] = 0

while len(Q) > 0 and N != 1:

    now = Q.pop(0)

    for nw in Ndic[now]:

        if nw[0] not in ends:

            Q.append(nw[0])
            ends.add(nw[0])

            if nw[1] % 2 == 0:
                Nlis[nw[0]-1] =  Nlis[now-1]

            else:
                Nlis[nw[0]-1] = (Nlis[now-1] + 1) % 2

            Ndic[nw[0]].remove([now,nw[1]])

for i in Nlis:
    print(i)

Submission status: Time Limit Exceeded

Solution code:

N = int(input())

uvw = []
Nlis = [-1] * N
Ndic = {}

for i in range(N-1):

    u,v,w = map(int,input().split())
    
    if u not in Ndic:
        Ndic[u] = [[v,w]]

    else:
        Ndic[u].append([v,w])

    if v not in Ndic:
        Ndic[v] = [[u,w]]

    else:
        Ndic[v].append([u,w])


ends = set([1])
Q = [1]
Nlis[0] = 0

while len(Q) > 0 and N != 1:

    now = Q.pop(0)

    for nw in Ndic[now]:

        if nw[0] not in ends:

            Q.append(nw[0])
            ends.add(nw[0])

            if nw[1] % 2 == 0:
                Nlis[nw[0]-1] =  Nlis[now-1]

            else:
                Nlis[nw[0]-1] = (Nlis[now-1] + 1) % 2

for i in Nlis:
    print(i)

Submission status: Time Limit Exceeded

Solution code:

N = int(input())

uvw = []
Nlis = [-1] * N
Ndic = {}

for i in range(N-1):

    u,v,w = map(int,input().split())
    
    if u not in Ndic:
        Ndic[u] = [[v,w]]

    else:
        Ndic[u].append([v,w])

    if v not in Ndic:
        Ndic[v] = [[u,w]]

    else:
        Ndic[v].append([u,w])


ends = set([1])
Q = [1]
Nlis[0] = 0

while len(Q) > 0:

    now = Q.pop(0)

    for nw in Ndic[now]:

        if nw[0] not in ends:

            Q.append(nw[0])
            ends.add(nw[0])

            if nw[1] % 2 == 0:
                Nlis[nw[0]-1] =  Nlis[now-1]

            else:
                Nlis[nw[0]-1] = (Nlis[now-1] + 1) % 2

for i in Nlis:
    print(i)

Submission status: Time Limit Exceeded

Solution code:

N = int(input())

uvw = []
Nlis = [-1] * N
Ndic = {}

for i in range(N-1):

    u,v,w = map(int,input().split())
    
    if u not in Ndic:
        Ndic[u] = [[v,w]]

    else:
        Ndic[u].append([v,w])

    if v not in Ndic:
        Ndic[v] = [[u,w]]

    else:
        Ndic[v].append([u,w])


ends = [1]
Q = [1]
Nlis[0] = 0

while len(Q) > 0:

    now = Q.pop(0)

    for nw in Ndic[now]:

        if nw[0] not in ends:

            Q.append(nw[0])
            ends.append(nw[0])

            if nw[1] % 2 == 0:
                Nlis[nw[0]-1] =  Nlis[now-1]

            else:
                Nlis[nw[0]-1] = (Nlis[now-1] + 1) % 2

for i in Nlis:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
import sys
N = int(input().strip())
tree = [[float('inf')]*N for _ in range(N)]
for line in sys.stdin:
  u,v,w = map(int, line.strip().split(' '))
  tree[u-1][v-1] = w
  tree[v-1][u-1] = w
result = [None]*N
result[0] = 0
queue = [0]

def scan(i):
  for j in range(N):
    if not result[j] == None:
      continue
    if tree[i][j] == float('inf'):
      continue
    elif tree[i][j] % 2 == 0:
      result[j] = result[i]
      queue.append(j)
    else:
      result[j] = result[i]^1
      queue.append(j)

for _ in range(N):
  scan(queue.pop())

for i in result:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
from scipy.sparse.csgraph import dijkstra

N = int(input())

G = [[float('inf')] * N for i in range(N)]

if N == 1:
    print(1)
else:
    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

    # G2 = csgraph_from_dense(G)
    d = dijkstra(G, indices=0)

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

Submission status: Time Limit Exceeded

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

ans = [[] for i in range(n)]
u = 0
ans[u] = 0
queue = [u] #キュー
check = [False]*n #訪問済み
check[u] = True
l = 1
while True:
    if l == 0:
        break
    u = queue.pop(0)
    l -= 1
    for v in ar[u]:
        if not check[v[0]]:
            check[v[0]] = True
            ans[v[0]] = ans[u] + v[1]
            queue.append(v[0])
            l+=1

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

Submission status: Time Limit Exceeded

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

ans = [[] for i in range(n)]
u = 0
ans[u] = 0
queue = [u] #キュー
check = [False]*n #訪問済み
check[u] = True
l = 1
while True:
    if l == 0:
        break
    u = queue.pop(0)
    l -= 1
    for v in ar[u]:
        if not check[v[0]]:
            check[v[0]] = True
            ans[v[0]] = ans[u] + v[1]
            queue.append(v[0])
            l+=1

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

Submission status: Time Limit Exceeded

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

ans = [[] for i in range(n)]
u = 0
ans[u] = 0
queue = [u] #キュー
check = [False]*n #訪問済み
check[u] = True

while True:
    if len(queue) == 0:
        break
    u = queue.pop(0)
    for v in ar[u]:
        if not check[v[0]]:
            check[v[0]] = True
            ans[v[0]] = ans[u] + v[1]
            queue.append(v[0])

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

Submission status: Time Limit Exceeded

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

ans = [[] for i in range(n)]
u = 0
ans[u] = 0
queue = [u] #キュー
check = [False]*n #訪問済み
check[u] = True

while True:
    if len(queue) == 0:
        break
    u = queue.pop(0)
    for v in ar[u]:
        if not check[v[0]]:
            check[v[0]] = True
            ans[v[0]] = ans[u] + v[1]
            queue.append(v[0])

for i in range(len(ans)):
    print(ans[i]%2)

Submission status: Time Limit Exceeded

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

ans = [[] for i in range(n)]
u = 0
ans[u] = 0
queue = [u] #キュー
check = [u] #訪問済み

while True:
    if len(queue) == 0:
        break
    u = queue[0]
    queue.pop(0)
    for v in ar[u]:
        if not v[0] in check:
            check.append(v[0])
            ans[v[0]] = (ans[u] + v[1])%2
            queue.append(v[0])

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ar = [[-1 for i in range(n)] for j in range(n)]
ar2 = [[] for i in range(n)]
for i in range(n-1):
    ipt = list(map(int,input().split()))
    ar[ipt[0]-1][ipt[1]-1] = ipt[2]%2
    ar[ipt[1]-1][ipt[0]-1] = ipt[2]%2
    ar2[ipt[0]-1].append(ipt[1]-1)
    ar2[ipt[1]-1].append(ipt[0]-1)

ans = [[] for i in range(n)]
u = 0
ans[u] = 0
queue = [u] #キュー
check = [u] #訪問済み

while True:
    if len(queue) == 0:
        break
    u = queue[0]
    queue.pop(0)
    for v in ar2[u]:
        if not v in check:
            check.append(v)
            ans[v] = (ans[u] + ar[u][v])%2
            queue.append(v)

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ar = [[-1 for i in range(n)] for j in range(n)]
ar2 = [[] for i in range(n)]
for i in range(n-1):
    ipt = list(map(int,input().split()))
    ar[ipt[0]-1][ipt[1]-1] = ipt[2]%2
    ar[ipt[1]-1][ipt[0]-1] = ipt[2]%2
    ar2[ipt[0]-1].append(ipt[1]-1)
    ar2[ipt[1]-1].append(ipt[0]-1)

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

def draw(u,k):
    for v in ar2[u]:
        if v!=k:
            ans[v] = (ans[u] + ar[u][v])%2
            draw(v,u)

draw(0,0)
for i in range(len(ans)):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ar = [[-1 for i in range(n)] for j in range(n)]
for i in range(n-1):
    ipt = list(map(int,input().split()))
    ar[ipt[0]-1][ipt[1]-1] = ipt[2]%2
    ar[ipt[1]-1][ipt[0]-1] = ipt[2]%2

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

def draw(u,k):
    for v,d in enumerate(ar[u]):
        if d >= 0 and v!=k:
            ans[v] = (ans[u] + d)%2
            draw(v,u)

draw(0,0)
for i in range(len(ans)):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
  return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
  read_all = [tuple(map(int, input().split())) for _ in range(N)]
  return map(list,zip(*read_all))

#################

N = I()
u,v,w = Line(N-1)

c = []
for i in range(N-1):
  c.append([u[i]-1, v[i]-1, w[i]])
  c.append([v[i]-1, u[i]-1, w[i]])

from heapq import heappop, heappush
inf=float('inf')
d=[inf for i in range(N)]
d[0]=0
prev=[None]*N
p=dict()
for i,j,k in c: p[i]=p.get(i,[])+[(j,k)]

q=[]
heappush(q,(d[0],0))
while q:
  du, u = heappop(q)
  if d[u]<du: continue
  for v,weight in p.get(u,[]):
    alt=du+weight
    if d[v]>alt:
      d[v]=alt
      prev[v]=u
      heappush(q, (alt,v))

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
sys.setrecursionlimit(10**5)
max = 10**5+5

def dk(start,node,edge):
  ans = [0] * node
  q = [start]
  
  while(len(q) != 0):
    i = q.pop()
    for j, cost in edge[i]:
      ans[j] = (ans[i] + cost)%2
      q.append(j)
  return ans

#入力
N = int(input())
#エッジ
E = [[] for _ in [0]*(max)]

for i in range(N-1):
  a,b,w = (int(x) for x in input().split())
  E[a-1].append((b-1, w))
  E[b-1].append((a-1, w))


ans = dk(0, N, E)

#出力
print(*ans, sep = "\n")

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10000000)
n=int(input())
graph=[[] for i in range(n)]
node=[[-1]*n for i in range(n)]
color=[0]*n
visited=[False]*n
for i in range(n-1):
    a,b,c=map(int,input().split())
    graph[a-1].append(b-1);node[a-1][b-1]=c%2
    graph[b-1].append(a-1);node[b-1][a-1]=c%2
search=[[0,i] for i in graph[0]]
while search!=[]:
  a=search.pop(0)
  if visited[a[1]]==True:continue
  search=search+[[a[1],i] for i in graph[a[1]]]
  color[a[1]]=color[a[0]]^node[a[0]][a[1]]
  visited[a[1]]=True
for i in color:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
graph=[[] for i in range(n)]
node=[[-1]*n for i in range(n)]
color=[-1]*n
for i in range(n-1):
    a,b,c=map(int,input().split())
    graph[a-1].append([b-1,c%2])
    graph[b-1].append([a-1,c%2])
def coloring(pre,c):
  color[pre]=c
  for i,j in graph[pre]:
    if color[i]<0:
      coloring(i,color[pre]^j)
coloring(0,1)
for i in color:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

N = int(input())

node = [[] for i in range(N)]
colour = [-1] * N
colour[0] = 0
checked = []

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

stack = deque()
stack.append(0)

while stack:
  now = stack.pop()
  for n in node[now]:
    if not n[0] in checked:
      colour[n[0]] = (colour[now] + n[1]) % 2
      stack.append(n[0])
  checked.append(now)
  
for i in range(N):
  print(colour[i])

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
graph=[[] for i in range(n)]
node=[[-1]*n for i in range(n)]
color=[0]*n
visited=[False]*n
for i in range(n-1):
    a,b,c=map(int,input().split())
    graph[a-1].append(b-1);node[a-1][b-1]=c%2
    graph[b-1].append(a-1);node[b-1][a-1]=c%2
search=[[0,i] for i in graph[0]]
while search!=[]:
  a=search.pop(0)
  if visited[a[1]]==True:continue
  search=search+[[a[1],i] for i in graph[a[1]]]
  color[a[1]]=color[a[0]]^node[a[0]][a[1]]
  visited[a[1]]=True
for i in color:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
graph=[[] for i in range(n)]
node=[[-1]*n for i in range(n)]
color=[0]*n
visited=[False]*n
for i in range(n-1):
    a,b,c=map(int,input().split())
    graph[a-1].append(b-1);node[a-1][b-1]=c%2
    graph[b-1].append(a-1);node[b-1][a-1]=c%2
search=[[0,i] for i in graph[0]]
while search!=[]:
  a=search.pop(0)
  if visited[a[1]]==True:continue
  search=search+[[a[1],i] for i in graph[a[1]]]
  color[a[1]]=color[a[0]]^node[a[0]][a[1]]
  visited[a[1]]=True
for i in color:
  print(i)

Submission status: Time Limit Exceeded

Solution code:
def paint(point, color):
    colors[point] = color

    # その点とつながる点で、まだ色が塗られていない点にルールに従い色を塗る
    for i in reration[point]:
        if not colors[i]:  # 塗られてない点
            # print(str(i)+"塗る")
            nextcolor = color*((-1)**hen[point][i])
            paint(i, nextcolor)


N = int(input())
hen = [[0 for _ in range(N)] for i in range(N)]
reration = [[] for i in range(N)]  # その点がつながる点
colors = ["" for i in range(N)]  # 色(作業中は簡単のため1,-1の2つに塗り分ける)
for i in range(N-1):
    U, V, W = map(int, input().split())
    hen[U-1][V-1] = W % 2
    hen[V-1][U-1] = W % 2
    reration[U-1].append(V-1)
    reration[V-1].append(U-1)

# print(hen)
# print(reration)
for i in range(N):
    if not colors[i]:
        paint(i, 1)
for co in colors:
    print((co+1)//2)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
graph = [[] for _ in range(n + 1)]

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

que = [(1, 0)]

colors = [None] * (n + 1)
while que:
    now, color = que.pop(0)
    if colors[now] is not None:
        continue
    colors[now] = color

    for b, w in graph[now]:
        que.append((b, (color + (w & 1)) & 1))
print("\n".join(map(str, colors[1:])))

Submission status: Time Limit Exceeded

Solution code:
import sys
from queue import Queue


def main():
    input = sys.stdin.readline
    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])

    color = [None] * N
    v_que = Queue()
    v_que.put(0)
    
    c = True
    c_que = Queue()
    c_que.put(c)

    while not v_que.empty():
        v = v_que.get()
        c = c_que.get()
        if color[v] is not None:
            continue
        color[v] = c
        for e in tree[v]:
            v_que.put(e[0])
            c_que.put(c if e[1] % 2 == 0 else not c)

    for c in color:
        print(int(c))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

queue = [0]
V = [0]*N
idx = 1

while queue:
	u = queue.pop()
	for i in G[u]:
		v, w = i
		if v not in V:
			V[idx] = v
			idx += 1
			queue.append(v)
			dis[v] = dis[u] + w
		if idx == N:
			break
	if idx == N:
		break

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

Submission status: Time Limit Exceeded

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

queue = [0]
V = [0]*N
idx = 1

while queue:
	u = queue.pop()
	for i in G[u]:
		v, w = i
		if v not in V:
			V[idx] = v
			idx += 1
			queue.append(v)
			dis[v] = dis[u] + w

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

dic = [[] for i in range(N)]
for i in range(N-1):
  inf = input().split()
  u = int(inf[0])
  v = int(inf[1])
  w = int(inf[2])
  dic[u-1] +=[(v-1, w)]
  dic[v-1] +=[(u-1, w)]

dist = [-1]*N
stack = []
stack.append(0)
dist[0] = 0

while stack:
  label = stack.pop(-1)
  for i, c in dic[label]:
    if dist[i] == -1:
      dist[i] = dist[label]+c
      stack += [i]

ls = [0]
print(0)
for i in range(1,N):
  j = i-1
  for j in range(i):
    if (dist[j]+dist[i])%2==1:
      print((ls[j]+1)%2)
      ls += [(ls[j]+1)%2]
      break
  else:
    print(ls[i-1])
    ls += [ls[i-1]]

Submission status: Time Limit Exceeded

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

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

    # edges[u-1].append([v-1,w])
    # edges[v-1].append([u-1,w])


dist = [None]*N
dist[0] = 0
cnt = 1 # 点1個は確定済み
while cnt < N:
    for edge in edges:
        if dist[edge[0]] == None and dist[edge[1]] != None:
            dist[edge[0]] = dist[edge[1]] + edge[2]
            cnt += 1
        elif dist[edge[1]] == None and dist[edge[0]] != None:
            dist[edge[1]] = dist[edge[0]] + edge[2]
            cnt += 1

for item in dist:
    print(item%2)

Submission status: Time Limit Exceeded

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

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

    # edges[u-1].append([v-1,w])
    # edges[v-1].append([u-1,w])


dist = [None]*N
dist[0] = 0
cnt = 0
while len(edges) > 0:
    for edge in edges:
        if dist[edge[0]] == None and dist[edge[1]] != None:
            dist[edge[0]] = dist[edge[1]] + edge[2]
            edges.remove(edge)
        elif dist[edge[1]] == None and dist[edge[0]] != None:
            dist[edge[1]] = dist[edge[0]] + edge[2]
            edges.remove(edge)
    cnt += 1
    if cnt > N: break

for item in dist:
    print(item%2)

Submission status: Time Limit Exceeded

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

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

    # edges[u-1].append([v-1,w])
    # edges[v-1].append([u-1,w])


dist = [None]*N
dist[0] = 0

while len(edges) > 0:
    for edge in edges:
        if dist[edge[0]] == None and dist[edge[1]] != None:
            dist[edge[0]] = dist[edge[1]] + edge[2]
            edges.remove(edge)
        elif dist[edge[1]] == None and dist[edge[0]] != None:
            dist[edge[1]] = dist[edge[0]] + edge[2]
            edges.remove(edge)

for item in dist:
    print(item%2)

Submission status: Time Limit Exceeded

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

N = int(input())
edge = [[] for _ in range(N)]
sum_w = 0
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])
    sum_w += w

dist = [sum_w]*N
rest = [i for i in range(N)][::-1]

is_finish = False
dist[0] = 0
while len(rest) > 0:
    min_dist = sum_w+1
    min_ptr = -1
    for i in rest:
        if dist[i] < min_dist:
            min_dist = dist[i]
            min_ptr = i
    rest.remove(min_ptr)
    for i in edge[min_ptr]:
        if i[0] in rest:
            if i[0] < min_ptr:
                dist[i[0]] = dist[min_ptr] + i[1]
            elif i[0] > min_ptr:
                dist[i[0]] = dist[min_ptr] + i[1]
for item in dist:
    print(item%2)

Submission status: Time Limit Exceeded

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

N = int(input())
edge = [[] for _ in range(N)]
sum_w = 0
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])
    sum_w += w

dist = [sum_w]*N
rest = [i for i in range(N)]

is_finish = False
dist[0] = 0
while len(rest) > 0:
    min_dist = sum_w+1
    min_ptr = -1
    for i in rest:
        if dist[i] < min_dist:
            min_dist = dist[i]
            min_ptr = i
    rest.remove(min_ptr)
    for i in edge[min_ptr]:
        if i[0] in rest:
            if i[0] < min_ptr:
                dist[i[0]] = dist[min_ptr] + i[1]
            elif i[0] > min_ptr:
                dist[i[0]] = dist[min_ptr] + i[1]
for item in dist:
    print(item%2)

Submission status: Time Limit Exceeded

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

N = int(input())
edge = [[] for _ in range(N)]
sum_w = 0
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])
    sum_w += w

dist = [sum_w]*N
comp = [None]*N
# rest = [i for i in range(N)]

is_finish = False
dist[0] = 0
while not is_finish:
    min_dist = sum_w+1
    min_ptr = -1
    for i in range(N):
        if comp[i] == None and dist[i] < min_dist:
            min_dist = dist[i]
            min_ptr = i
    if min_ptr < 0:
        is_finish = True
    else:
        comp[min_ptr] = 1
        for i in edge[min_ptr]:
            if comp[i[0]] == None:
                if i[0] < min_ptr:
                    dist[i[0]] = dist[min_ptr] + i[1]
                elif i[0] > min_ptr:
                    dist[i[0]] = dist[min_ptr] + i[1]

for item in dist:
    print(item%2)

Submission status: Time Limit Exceeded

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

N = int(input())
edge = [[None]*N for _ in range(N)]
sum_w = 0
for i in range(N-1):
    u, v, w = map(int, input().split())
    edge[u-1][v-1] = w
    sum_w += w
dist = [sum_w]*N
comp = [None]*N

is_finish = False
dist[0] = 0
while not is_finish:
    min_dist = sum_w+1
    min_ptr = -1
    for i in range(N):
        if comp[i] == None and dist[i] < min_dist:
            min_dist = dist[i]
            min_ptr = i
    if min_ptr < 0:
        is_finish = True
    else:
        comp[min_ptr] = 1
        for i in range(N):
            if comp[i] == None:
                if i < min_ptr and edge[i][min_ptr] != None:
                    dist[i] = dist[min_ptr] + edge[i][min_ptr]
                elif min_ptr < i and edge[min_ptr][i] != None:
                    dist[i] = dist[min_ptr] + edge[min_ptr][i]

for item in dist:
    print(item%2)

Submission status: Time Limit Exceeded

Solution code:
#ABC126D-EvenRelation.py

import sys
import numpy as np

def main():
    N = int(sys.stdin.readline())
    UVW = [list(map(int, sys.stdin.readline().split())) for _ in range(N-1)]
    distanceList = [0] + [-1]*(N-1)

    while True:
        s = len(UVW)
        if s == 0:
            break
        for i in range(s):
            u, v, w = UVW[i]
            if distanceList[u-1] != -1:
                if distanceList[v-1] == -1:
                    distanceList[v-1] = w + distanceList[u-1]
                UVW.pop(i)
                break
            elif distanceList[v-1] != -1:
                if distanceList[u-1] == -1:
                    distanceList[u-1] = w + distanceList[v-1]
                UVW.pop(i)
                break

    for d in distanceList:
        if d % 2 == 0:
            print(0)
        else:
            print(1)



if __name__ == "__main__":
    main()