Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**5+1)

def cur(p):
    for i in x[p]:
        if rank[i]==-1:
            rank[i]=rank[p]+1
            par[i]=p
            cur(i)


n=int(input())
x=[[] for i in range(n)]

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

rank=[-1 for i in range(n)]
rank[0]=0
par=[-1 for i in range(n)]

cur(0)
M=max(rank)
x=[[] for i in range(M+1)]

q=[0,0]
r=[n//3,n//3,n//3]
if n%3>0: r[1]+=1
if n%3>1: r[2]+=1
tmp1,tmp2,tmp3=1,2,3

for i in range(n):
    q[rank[i]%2]+=1
    x[rank[i]].append(i)
ans=[-1 for i in range(n)]


if q[0]>r[0]+r[1] or q[1]>r[0]+r[2]:
    if q[0]>r[0]+r[2] or q[1]>r[0]+r[1]:
        x=1
        for i in range(10**20):
            x+=1
        print(-1)
        sys.exit()
    else:
        tmp1,tmp2=2,1


for i in range(M+1):
    if i%2==0:
        for p in x[i]:
            if tmp1<=n:
                ans[p]=tmp1
                tmp1+=3
            else:
                ans[p]=tmp3
                tmp3+=3
    else:
        for p in x[i]:
            if tmp2<=n:
                ans[p]=tmp2
                tmp2+=3
            else:
                ans[p]=tmp3
                tmp3+=3


for i in range(n-1):
    print(ans[i],end=" ")
print(ans[n-1])

Fixed submission:
import sys
sys.setrecursionlimit(10**5+1)

def cur(p):
    for i in x[p]:
        if rank[i]==-1:
            rank[i]=rank[p]+1
            par[i]=p
            cur(i)


n=int(input())
x=[[] for i in range(n)]

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

rank=[-1 for i in range(n)]
rank[0]=0
par=[-1 for i in range(n)]

cur(0)
M=max(rank)
x=[[] for i in range(M+1)]

q=[0,0]
r=[n//3,n//3,n//3]
if n%3>0: r[1]+=1
if n%3>1: r[2]+=1
tmp1,tmp2,tmp3=1,2,3

for i in range(n):
    q[rank[i]%2]+=1
    x[rank[i]].append(i)
ans=[-1 for i in range(n)]

"""
if q[0]>r[0]+r[1] or q[1]>r[0]+r[2]:
    if q[0]>r[0]+r[2] or q[1]>r[0]+r[1]:
        tmp1,tmp2=2,1
"""

if q[0]<=r[0]+r[1] and q[1]<=r[0]+r[2]:
    for i in range(n):
        if rank[i]%2==0:
            if tmp1<=n:
                ans[i]=tmp1
                tmp1+=3
            else:
                ans[i]=tmp3
                tmp3+=3
        else:
            if tmp2<=n:
                ans[i]=tmp2
                tmp2+=3
            else:
                ans[i]=tmp3
                tmp3+=3

elif q[0]<=r[0]:
    for i in range(n):
        if rank[i]%2==0:
            ans[i]=tmp3
            tmp3+=3
        else:
            if tmp1<=n:
                ans[i]=tmp1
                tmp1+=3
            elif tmp2<=n:
                ans[i]=tmp2
                tmp2+=3
            else:
                ans[i]=tmp3
                tmp3+=3

elif q[1]<=r[0]:
    for i in range(n):
        if rank[i]%2==1:
            ans[i]=tmp3
            tmp3+=3
        else:
            if tmp1<=n:
                ans[i]=tmp1
                tmp1+=3
            elif tmp2<=n:
                ans[i]=tmp2
                tmp2+=3
            else:
                ans[i]=tmp3
                tmp3+=3

    

for i in range(n-1):
    print(ans[i],end=" ")
print(ans[n-1])

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**5+1)

def cur(p):
    for i in x[p]:
        if rank[i]==-1:
            rank[i]=rank[p]+1
            par[i]=p
            cur(i)


n=int(input())
x=[[] for i in range(n)]

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

rank=[-1 for i in range(n)]
rank[0]=0
par=[-1 for i in range(n)]

cur(0)
M=max(rank)
x=[[] for i in range(M+1)]

q=[0,0]
r=[n//3,n//3,n//3]
if n%3>0: r[1]+=1
if n%3>1: r[2]+=1
tmp1,tmp2,tmp3=1,2,3

for i in range(n):
    q[rank[i]%2]+=1
    x[rank[i]].append(i)
ans=[-1 for i in range(n)]


if q[0]>r[0]+r[1] or q[1]>r[0]+r[2]:
    if q[0]>r[0]+r[2] or q[1]>r[0]+r[1]:
        print(-1)
        sys.exit()
    else:
        tmp1,tmp2=2,1


for i in range(M+1):
    if i%2==0:
        for p in x[i]:
            if tmp1<=n:
                ans[p]=tmp1
                tmp1+=3
            else:
                ans[p]=tmp3
                tmp3+=3
    else:
        for p in x[i]:
            if tmp2<=n:
                ans[p]=tmp2
                tmp2+=3
            else:
                ans[p]=tmp3
                tmp3+=3


for i in range(n-1):
    print(ans[i],end=" ")
print(ans[n-1])

Fixed submission:
import sys
sys.setrecursionlimit(10**5+1)

def cur(p):
    for i in x[p]:
        if rank[i]==-1:
            rank[i]=rank[p]+1
            par[i]=p
            cur(i)


n=int(input())
x=[[] for i in range(n)]

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

rank=[-1 for i in range(n)]
rank[0]=0
par=[-1 for i in range(n)]

cur(0)
M=max(rank)
x=[[] for i in range(M+1)]

q=[0,0]
r=[n//3,n//3,n//3]
if n%3>0: r[1]+=1
if n%3>1: r[2]+=1
tmp1,tmp2,tmp3=1,2,3

for i in range(n):
    q[rank[i]%2]+=1
    x[rank[i]].append(i)
ans=[-1 for i in range(n)]

"""
if q[0]>r[0]+r[1] or q[1]>r[0]+r[2]:
    if q[0]>r[0]+r[2] or q[1]>r[0]+r[1]:
        tmp1,tmp2=2,1
"""

if q[0]<=r[0]+r[1] and q[1]<=r[0]+r[2]:
    for i in range(n):
        if rank[i]%2==0:
            if tmp1<=n:
                ans[i]=tmp1
                tmp1+=3
            else:
                ans[i]=tmp3
                tmp3+=3
        else:
            if tmp2<=n:
                ans[i]=tmp2
                tmp2+=3
            else:
                ans[i]=tmp3
                tmp3+=3

elif q[0]<=r[0]:
    for i in range(n):
        if rank[i]%2==0:
            ans[i]=tmp3
            tmp3+=3
        else:
            if tmp1<=n:
                ans[i]=tmp1
                tmp1+=3
            elif tmp2<=n:
                ans[i]=tmp2
                tmp2+=3
            else:
                ans[i]=tmp3
                tmp3+=3

elif q[1]<=r[0]:
    for i in range(n):
        if rank[i]%2==1:
            ans[i]=tmp3
            tmp3+=3
        else:
            if tmp1<=n:
                ans[i]=tmp1
                tmp1+=3
            elif tmp2<=n:
                ans[i]=tmp2
                tmp2+=3
            else:
                ans[i]=tmp3
                tmp3+=3

    

for i in range(n-1):
    print(ans[i],end=" ")
print(ans[n-1])