Problem p02749 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**7)

n = int(input())
T = [[] for _ in range(n)]
for _ in range(n-1):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  T[a].append(b)
  T[b].append(a)
L = [-1]*n
dist = [-1]*n
dist[0] = 0
F_1 = [i for i in range(1, n+1) if i%3==1]
F_2 = [i for i in range(1, n+1) if i%3==2]
F_0 = [i for i in range(1, n+1) if i%3==0]
def dfs(v):
  for nv in T[v]:
    if dist[nv] == -1:
      dist[nv] = dist[v] ^ 1
      dfs(nv)
dfs(0)
l0 = dist.count(0)
l1 = n - l0
if l0 <= n//3:
  for i in range(n):
    if dist[i] == 0:
      L[i] = F_0.pop()
    else:
      if F_1:
        L[i] = F_1.pop()
      elif F_2:
        L[i] = F_2.pop()
      else:
        L[i] = F_0.pop()
elif l1 <= n//3:
  for i in range(n):
    if dist[i]:
      L[i] = F_0.pop()
    else:
      if F_1:
        L[i] = F_1.pop()
      elif F_2:
        L[i] = F_2.pop()
      else:
        L[i] = F_0.pop()
else:
  for i in range(n):
    if dist[i]:
      if F_1:
        L[i] = F_1.pop()
      else:
        L[i] = F_0.pop()
    else:
      if F_2:
        L[i] = F_2.pop()
      else:
        L[i] = F_0.pop()
print(*L)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)

n = int(input())
T = [[] for _ in range(n)]
for _ in range(n-1):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  T[a].append(b)
  T[b].append(a)
L = [-1]*n
dist = [-1]*n
dist[0] = 0
F_1 = [i for i in range(1, n+1) if i%3==1]
F_2 = [i for i in range(1, n+1) if i%3==2]
F_0 = [i for i in range(1, n+1) if i%3==0]
def dfs(v):
  if dist[v]%3==1:
    if F_1:
      L[v] = F_1.pop()
    else:
      L[v] = F_0.pop()
  elif dist[v]%3==2:
    if F_2:
      L[v] = F_2.pop()
    else:
      L[v] = F_0.pop()
  else:
    L[v] = F_0.pop()
  for nv in T[v]:
    if dist[nv] == -1:
      dist[nv] = dist[v] + 1
      dfs(nv)
dfs(0)
print(*L)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)

n = int(input())
T = [[] for _ in range(n)]
for _ in range(n-1):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  T[a].append(b)
  T[b].append(a)
L = [-1]*n
dist = [-1]*n
F_1 = [i for i in range(1, n+1) if i%3==1]
F_2 = [i for i in range(1, n+1) if i%3==2]
F_0 = [i for i in range(1, n+1) if i%3==0]
def dfs_1(v):
  stack = [v]
  D = [-1]*n
  D[v] = 0
  while stack:
    i = stack.pop()
    for nv in T[i]:
      if D[nv] == -1:
        D[nv] = D[i] + 1
        stack.append(nv)
  return D.index(max(D))
s = dfs_1(0)
dist[s] = 0
def dfs_2():
  S = [s]
  while S:
    v = S.pop()
    if dist[v]%2:
      if F_1:
        L[v] = F_1.pop()
      else:
        L[v] = F_0.pop()
    else:
      if F_2:
        L[v] = F_2.pop()
      else:
        L[v] = F_0.pop()
    for nv in T[v]:
      if dist[nv] == -1:
        dist[nv] = dist[v] + 1
        S.append(nv)
dfs_2()
print(*L)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)

n = int(input())
T = [[] for _ in range(n)]
for _ in range(n-1):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  T[a].append(b)
  T[b].append(a)
L = [-1]*n
dist = [-1]*n
dist[0] = 0
F_1 = [i for i in range(1, n+1) if i%3==1]
F_2 = [i for i in range(1, n+1) if i%3==2]
F_0 = [i for i in range(1, n+1) if i%3==0]
def dfs(v):
  if dist[v]%2:
    if F_1:
      L[v] = F_1.pop()
    else:
      L[v] = F_0.pop()
  else:
    if F_2:
      L[v] = F_2.pop()
    else:
      L[v] = F_0.pop()
  for nv in T[v]:
    if dist[nv] == -1:
      dist[nv] = dist[v] + 1
      dfs(nv)
dfs(0)
print(*L)