Problem p02749 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n = int(input())

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

lst = [[], [], []]
for i in range(1, n+1):
    lst[i % 3].append(i)

p = [0]*n

depth = [-1]*n
depth[0] = 0

q = [0]

cnt = [1, 0]

while q:
    v = q.pop()
    for w in g[v]:
        if depth[w] != -1:
            continue
        depth[w] = depth[v] + 1
        cnt[depth[w] % 2] += 1
        q.append(w)


ans = [0]*n

if cnt[0] <= n//3:
    ans[0] = lst[0].pop()
    q = [0]
    while q:
        v = q.pop()
        for w in g[v]:
            if ans[w] != 0:
                continue
            if depth[w] % 2 == 0:
                ans[w] = lst[0].pop()
            else:
                if lst[1]:
                    ans[w] = lst[1].pop()
                elif lst[2]:
                    ans[w] = lst[2].pop()
                else:
                    ans[w] = lst[0].pop()
            q.append(w)
elif cnt[1] <= n//3:
    ans[0] = lst[1].pop()
    q = [0]
    while q:
        v = q.pop()
        for w in g[v]:
            if ans[w] != 0:
                continue
            if depth[w] % 2 != 0:
                ans[w] = lst[0].pop()
            else:
                if lst[1]:
                    ans[w] = lst[1].pop()
                elif lst[2]:
                    ans[w] = lst[2].pop()
                else:
                    ans[w] = lst[0].pop()
            q.append(w)
else:
    ans[0] = lst[2].pop()
    q = [0]
    while q:
        v = q.pop()
        for w in g[v]:
            if ans[w] != 0:
                continue
            if depth[w] % 2 != 0:
                if lst[1]:
                    ans[w] = lst[1].pop()
                else:
                    ans[w] = lst[0].pop()
            else:
                if lst[2]:
                    ans[w] = lst[2].pop()
                else:
                    ans[w] = lst[0].pop()
            q.append(w)

print(*ans)

Buggy submission (verdict: Runtime Error):
from collections import deque
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n = int(input())

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

lst = [[], [], []]
for i in range(1, n+1):
    lst[i % 3].append(i)

p = [0]*n

depth = [-1]*n
depth[0] = 0

q = [0]

cnt = [1, 0]

while q:
    v = q.pop()
    for w in g[v]:
        if depth[w] != -1:
            continue
        depth[w] = depth[v] + 1
        cnt[depth[w] % 2] += 1
        q.append(w)


ans = [0]*n

if cnt[0] <= n//3:
    ans[0] = lst[0].pop()
    q = [0]
    while q:
        v = q.pop()
        for w in g[v]:
            if ans[w] != 0:
                continue
            if depth[w] % 2 == 0:
                ans[w] = lst[0].pop()
            else:
                if lst[1]:
                    ans[w] = lst[1].pop()
                else:
                    ans[w] = lst[2].pop()
            q.append(w)
elif cnt[1] <= n//3:
    ans[0] = lst[1].pop()
    q = [0]
    while q:
        v = q.pop()
        for w in g[v]:
            if ans[w] != 0:
                continue
            if depth[w] % 2 != 0:
                ans[w] = lst[0].pop()
            else:
                if lst[1]:
                    ans[w] = lst[1].pop()
                else:
                    ans[w] = lst[2].pop()
            q.append(w)
else:
    ans[0] = lst[2].pop()
    q = [0]
    while q:
        v = q.pop()
        for w in g[v]:
            if ans[w] != 0:
                continue
            if depth[w] % 2 != 0:
                if lst[1]:
                    ans[w] = lst[1].pop()
                else:
                    ans[w] = lst[0].pop()
            else:
                if lst[2]:
                    ans[w] = lst[2].pop()
                else:
                    ans[w] = lst[0].pop()
            q.append(w)

print(*ans)