Buggy python submission (verdict: Runtime Error):
n=int(input())
l=[list(map(int,input().split())) for i in range(n-1)]
connection=[[] for i in range(n)]
for i in range(n-1):
  connection[l[i][0]-1].append(l[i][1]-1)
  connection[l[i][1]-1].append(l[i][0]-1)

def bfs(v):
  distance=[-1]*n
  distance[v]=0
  next=connection[v]
  next2=set()
  visited=[-1]*n
  visited[v]=1
  visitct=1
  ct=0
  while len(next)!=0 and visitct!=n:
    ct+=1
    for i in range(len(next)):
      if visited[next[i]]==-1:
        distance[next[i]]=ct
        visited[next[i]]=1
        visitct+=1
        for j in range(len(connection[next[i]])):
          if visited[connection[next[i]][j]]==-1:
            next2.add(connection[next[i]][j])
    next=list(next2)
    next2=set()
  return distance

b=bfs(0)
m=b.index(max(b))
b=bfs(m)
ans=[0]*n
ans[m]=1
c=[0]*n
c[0]=1
x=4
for i in range(n):
  if b[i]%3==1:
    ans[i]=x
    c[x-1]=1
    x+=3
    if x>=n:
      break

ct=0
for i in range(n):
  if ans[i]==0:
    while c[ct]!=0 and ct<n:
      ct+=1
    c[ct]=1
    ans[i]=ct+1
for i in range(n):
  ans[i]=str(ans[i])
print(' '.join(ans))

Fixed submission:
import sys
n=int(input())
l=[list(map(int,input().split())) for i in range(n-1)]
connection=[[] for i in range(n)]
for i in range(n-1):
  connection[l[i][0]-1].append(l[i][1]-1)
  connection[l[i][1]-1].append(l[i][0]-1)

def bfs(v):
  distance=[-1]*n
  distance[v]=0
  next=connection[v]
  next2=set()
  visited=[-1]*n
  visited[v]=1
  visitct=1
  ct=0
  while len(next)!=0 and visitct!=n:
    ct+=1
    for i in range(len(next)):
      if visited[next[i]]==-1:
        distance[next[i]]=ct
        visited[next[i]]=1
        visitct+=1
        for j in range(len(connection[next[i]])):
          if visited[connection[next[i]][j]]==-1:
            next2.add(connection[next[i]][j])
    next=list(next2)
    next2=set()
  return distance

b=bfs(0)
ct0=0;ct1=0
for i in range(n):
  b[i]%=2
  if b[i]==0:
    ct0+=1
  else:
    ct1+=1

ans=[0]*n
x1=1;x2=2;x3=3

if ct0>=n//3+1 and ct1>=n//3+1:
  for i in range(n):
    if b[i]==0:
      if x1<=n:
        ans[i]=x1
        x1+=3
      else:
        ans[i]=x3
        x3+=3
    else:
      if x2<=n:
        ans[i]=x2
        x2+=3
      else:
        ans[i]=x3
        x3+=3

elif ct0<=n//3:
  for i in range(n):
    if b[i]==0:
      ans[i]=x3
      x3+=3
    else:
      if x1<=n:
        ans[i]=x1
        x1+=3
      elif x2<=n:
        ans[i]=x2
        x2+=3
      else:
        ans[i]=x3
        x3+=3

elif ct1<=n//3:
  for i in range(n):
    if b[i]==1:
      ans[i]=x3
      x3+=3
    else:
      if x1<=n:
        ans[i]=x1
        x1+=3
      elif x2<=n:
        ans[i]=x2
        x2+=3
      else:
        ans[i]=x3
        x3+=3

for i in range(n):
  ans[i]=str(ans[i])
print(' '.join(ans))

Buggy python submission (verdict: Runtime Error):
n=int(input())
l=[list(map(int,input().split())) for i in range(n-1)]
connection=[[] for i in range(n)]
for i in range(n-1):
  connection[l[i][0]-1].append(l[i][1]-1)
  connection[l[i][1]-1].append(l[i][0]-1)

def bfs(v):
  distance=[-1]*n
  distance[v]=0
  next=connection[v]
  next2=set()
  visited=[-1]*n
  visited[v]=1
  visitct=1
  ct=0
  while len(next)!=0 and visitct!=n:
    ct+=1
    for i in range(len(next)):
      if visited[next[i]]==-1:
        distance[next[i]]=ct
        visited[next[i]]=1
        visitct+=1
        for j in range(len(connection[next[i]])):
          if visited[connection[next[i]][j]]==-1:
            next2.add(connection[next[i]][j])
    next=list(next2)
    next2=set()
  return distance

b=bfs(0)
ans=[0]*n
ans[0]=1
c=[0]*n
c[0]=1
x=4
for i in range(n):
  if b[i]%3==1:
    ans[i]=x
    c[x-1]=1
    x+=3
    if x>=n:
      break

ct=0
for i in range(n):
  if ans[i]==0:
    while c[ct]!=0 and ct<n:
      ct+=1
    c[ct]=1
    ans[i]=ct+1
for i in range(n):
  ans[i]=str(ans[i])
print(' '.join(ans))

Fixed submission:
import sys
n=int(input())
l=[list(map(int,input().split())) for i in range(n-1)]
connection=[[] for i in range(n)]
for i in range(n-1):
  connection[l[i][0]-1].append(l[i][1]-1)
  connection[l[i][1]-1].append(l[i][0]-1)

def bfs(v):
  distance=[-1]*n
  distance[v]=0
  next=connection[v]
  next2=set()
  visited=[-1]*n
  visited[v]=1
  visitct=1
  ct=0
  while len(next)!=0 and visitct!=n:
    ct+=1
    for i in range(len(next)):
      if visited[next[i]]==-1:
        distance[next[i]]=ct
        visited[next[i]]=1
        visitct+=1
        for j in range(len(connection[next[i]])):
          if visited[connection[next[i]][j]]==-1:
            next2.add(connection[next[i]][j])
    next=list(next2)
    next2=set()
  return distance

b=bfs(0)
ct0=0;ct1=0
for i in range(n):
  b[i]%=2
  if b[i]==0:
    ct0+=1
  else:
    ct1+=1

ans=[0]*n
x1=1;x2=2;x3=3

if ct0>=n//3+1 and ct1>=n//3+1:
  for i in range(n):
    if b[i]==0:
      if x1<=n:
        ans[i]=x1
        x1+=3
      else:
        ans[i]=x3
        x3+=3
    else:
      if x2<=n:
        ans[i]=x2
        x2+=3
      else:
        ans[i]=x3
        x3+=3

elif ct0<=n//3:
  for i in range(n):
    if b[i]==0:
      ans[i]=x3
      x3+=3
    else:
      if x1<=n:
        ans[i]=x1
        x1+=3
      elif x2<=n:
        ans[i]=x2
        x2+=3
      else:
        ans[i]=x3
        x3+=3

elif ct1<=n//3:
  for i in range(n):
    if b[i]==1:
      ans[i]=x3
      x3+=3
    else:
      if x1<=n:
        ans[i]=x1
        x1+=3
      elif x2<=n:
        ans[i]=x2
        x2+=3
      else:
        ans[i]=x3
        x3+=3

for i in range(n):
  ans[i]=str(ans[i])
print(' '.join(ans))

Buggy python submission (verdict: Runtime Error):
import sys
import collections
n=int(input())
l=[list(map(int,input().split())) for i in range(n-1)]
connection=[[] for i in range(n)]
for i in range(n-1):
  connection[l[i][0]-1].append(l[i][1]-1)
  connection[l[i][1]-1].append(l[i][0]-1)
def bfs(v):
  distance=[-1]*n
  distance[v]=0
  next=connection[v]
  next2=set()
  visited=[-1]*n
  visited[v]=1
  visitct=1
  ct=0
  while len(next)!=0 and visitct!=n:
    ct+=1
    for i in range(len(next)):
      if visited[next[i]]==-1:
        distance[next[i]]=ct
        visited[next[i]]=1
        visitct+=1
        for j in range(len(connection[next[i]])):
          if visited[connection[next[i]][j]]==-1:
            next2.add(connection[next[i]][j])
    next=list(next2)
    next2=set()
  return distance

b=bfs(0)
for i in range(n):
  b[i]%=3

c=collections.Counter(b)
v=list(c.values())
if max(v)-min(v)>=2:
  print(-1)
  sys.exit()

ans=[]
l1=[]
l2=[]
l3=[]
for i in range(1,n+1):
  if i%3==1:
    l1.append(i)
  elif i%3==2:
    l2.append(i)
  else:
    l3.append(i)
ct1=0
ct2=0
ct3=0

if n%3==0:
  for i in range(n):
    if b[i]%3==0:
      ans.append(l1[ct1])
      ct1+=1
    if b[i]%3==1:
      ans.append(l2[ct2])
      ct2+=1
    if b[i]%3==2:
      ans.append(l3[ct3])
      ct3+=1

if n%3==1:
  x=v.index(max(v))
  for i in range(n):
    if b[i]%3==x:
      ans.append(l1[ct1])
      ct1+=1
    if b[i]%3==(x+1)%3:
      ans.append(l2[ct2])
      ct2+=1
    if b[i]%3==(x+2)%3:
      ans.append(l3[ct3])
      ct3+=1

if n%3==2:
  x=v.index(min(v))
  for i in range(n):
    if b[i]%3==(x+1)%3:
      ans.append(l1[ct1])
      ct1+=1
    if b[i]%3==(x+2)%3:
      ans.append(l2[ct2])
      ct2+=1
    if b[i]%3==x:
      ans.append(l3[ct3])
      ct3+=1

for i in range(n):
  ans[i]=str(ans[i])
print(' '.join(ans))

Fixed submission:
import sys
n=int(input())
l=[list(map(int,input().split())) for i in range(n-1)]
connection=[[] for i in range(n)]
for i in range(n-1):
  connection[l[i][0]-1].append(l[i][1]-1)
  connection[l[i][1]-1].append(l[i][0]-1)

def bfs(v):
  distance=[-1]*n
  distance[v]=0
  next=connection[v]
  next2=set()
  visited=[-1]*n
  visited[v]=1
  visitct=1
  ct=0
  while len(next)!=0 and visitct!=n:
    ct+=1
    for i in range(len(next)):
      if visited[next[i]]==-1:
        distance[next[i]]=ct
        visited[next[i]]=1
        visitct+=1
        for j in range(len(connection[next[i]])):
          if visited[connection[next[i]][j]]==-1:
            next2.add(connection[next[i]][j])
    next=list(next2)
    next2=set()
  return distance

b=bfs(0)
ct0=0;ct1=0
for i in range(n):
  b[i]%=2
  if b[i]==0:
    ct0+=1
  else:
    ct1+=1

ans=[0]*n
x1=1;x2=2;x3=3

if ct0>=n//3+1 and ct1>=n//3+1:
  for i in range(n):
    if b[i]==0:
      if x1<=n:
        ans[i]=x1
        x1+=3
      else:
        ans[i]=x3
        x3+=3
    else:
      if x2<=n:
        ans[i]=x2
        x2+=3
      else:
        ans[i]=x3
        x3+=3

elif ct0<=n//3:
  for i in range(n):
    if b[i]==0:
      ans[i]=x3
      x3+=3
    else:
      if x1<=n:
        ans[i]=x1
        x1+=3
      elif x2<=n:
        ans[i]=x2
        x2+=3
      else:
        ans[i]=x3
        x3+=3

elif ct1<=n//3:
  for i in range(n):
    if b[i]==1:
      ans[i]=x3
      x3+=3
    else:
      if x1<=n:
        ans[i]=x1
        x1+=3
      elif x2<=n:
        ans[i]=x2
        x2+=3
      else:
        ans[i]=x3
        x3+=3

for i in range(n):
  ans[i]=str(ans[i])
print(' '.join(ans))