Buggy python submission (verdict: Runtime Error):
N = int(input())
UVW = [list(map(int, input().split())) for _ in range(N - 1)]

UVW.sort(key = lambda x: (x[0], x[1]))
color = [2 for _ in range(N)]
color[0] = 0

def coloring(l):
  for i in range(len(l)):
    uvw = UVW[i]
    if uvw[2] % 2 == 0:
      if color[uvw[0] - 1] != 2:
        color[uvw[1] - 1] = color[uvw[0] - 1]
      elif color[uvw[1] - 1] != 2:
        color[uvw[0] - 1] = color[uvw[1] - 1]
    else:
      if color[uvw[0] - 1] != 2:
        color[uvw[1] - 1] = color[uvw[0] - 1] ^ 1
      elif color[uvw[1] - 1] != 2:
        color[uvw[0] - 1] = color[uvw[1] - 1] ^ 1

coloring(UVW)
tmp = []
cnt = 0
for i in range(N):
  if color[i] == 2:
    cnt += 1
    for uvw in UVW:
      if uvw[0] == i + 1 or uvw[1] == i + 1:
        tmp.append(uvw)
if cnt > 0:
  color[tmp[0][0] - 1] = 0
  coloring(tmp)

for c in color:
  print(c)

Fixed submission:
N = int(input())
to = [[] for _ in range(N)]
cost = [[] for _ in range(N)]

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

ans = [-1 for _ in range(N)]
queue = []
queue.append(0)
while len(queue) > 0:
  v = queue.pop(0)
  for i in range(len(to[v])):
    u = to[v][i]
    w = cost[v][i]
    if ans[u] != -1:
      continue
    ans[u] = (ans[v] + w) % 2
    queue.append(u)

for a in ans:
  print(a)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
UVW = [list(map(int, input().split())) for _ in range(N - 1)]

UVW.sort(key = lambda x: x[0])
color = [2 for _ in range(N)]

while len(UVW) > 0:
  uvw = UVW.pop(0)
  if len(UVW) == N - 2:
    if uvw[2] % 2 == 0:
      color[uvw[0] - 1] = 0
      color[uvw[1] - 1] = 0
    else:
      color[uvw[0] - 1] = 0
      color[uvw[1] - 1] = 1
  if uvw[2] % 2 == 0:
    if color[uvw[0] - 1] != 2:
      color[uvw[1] - 1] = color[uvw[0] - 1]
    elif color[uvw[1] - 1] != 2:
      color[uvw[0] - 1] = color[uvw[1] - 1]
    else:
      UVW.append(uvw)
  else:
    if color[uvw[0] - 1] != 2:
      color[uvw[1] - 1] = color[uvw[0] - 1] ^ 1
    elif color[uvw[1] - 1] != 2:
      color[uvw[0] - 1] = color[uvw[1] - 1] ^ 1
    else:
      UVW.append(uvw)

for c in color:
  print(c)

Fixed submission:
N = int(input())
to = [[] for _ in range(N)]
cost = [[] for _ in range(N)]

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

ans = [-1 for _ in range(N)]
queue = []
queue.append(0)
while len(queue) > 0:
  v = queue.pop(0)
  for i in range(len(to[v])):
    u = to[v][i]
    w = cost[v][i]
    if ans[u] != -1:
      continue
    ans[u] = (ans[v] + w) % 2
    queue.append(u)

for a in ans:
  print(a)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
UVW = [list(map(int, input().split())) for _ in range(N - 1)]

UVW.sort(key = lambda x: x[0])
color = [2 for _ in range(N)]

while len(UVW) > 0:
  uvw = UVW.pop()
  if len(UVW) == N - 2:
    if uvw[2] % 2 == 0:
      color[uvw[0] - 1] = 0
      color[uvw[1] - 1] = 0
    else:
      color[uvw[0] - 1] = 0
      color[uvw[1] - 1] = 1
  if uvw[2] % 2 == 0:
    if color[uvw[0] - 1] != 2:
      color[uvw[1] - 1] = color[uvw[0] - 1]
    elif color[uvw[1] - 1] != 2:
      color[uvw[0] - 1] = color[uvw[1] - 1]
    else:
      UVW.append(uvw)
  else:
    if color[uvw[0] - 1] != 2:
      color[uvw[1] - 1] = color[uvw[0] - 1] ^ 1
    elif color[uvw[1] - 1] != 2:
      color[uvw[0] - 1] = color[uvw[1] - 1] ^ 1
    else:
      UVW.append(uvw)

for c in color:
  print(c)

Fixed submission:
N = int(input())
to = [[] for _ in range(N)]
cost = [[] for _ in range(N)]

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

ans = [-1 for _ in range(N)]
queue = []
queue.append(0)
while len(queue) > 0:
  v = queue.pop(0)
  for i in range(len(to[v])):
    u = to[v][i]
    w = cost[v][i]
    if ans[u] != -1:
      continue
    ans[u] = (ans[v] + w) % 2
    queue.append(u)

for a in ans:
  print(a)