Problem p02749 (python)

Fixed submission:
import sys
 
sys.setrecursionlimit(10 ** 6)
n = int(input())
 
edge = [[] for i in range(n)]
ans = [-1] * n
odd = []
even =[]
nums = [[],[],[]]
for i in range(1,n+1):
    nums[i % 3].append(i)
 
def dfs(x, last = -1, d = 0):
    if d % 2 == 0: even.append(x)
    else: odd.append(x)
    for nxt in edge[x]:
        if nxt == last:continue
        dfs(nxt, x, d + 1)
 
for i in range(n-1):
    a, b = map(int, input().split())
    a-=1
    b-=1
    edge[a].append(b)
    edge[b].append(a)
 
dfs(0)
 
zerocnt = len(nums[0])
 
if len(even) <= zerocnt:
    for even_node in even:
        ans[even_node] = nums[0].pop()
    rest = nums[0] + nums[1] + nums[2]
    for odd_node in odd:
        ans[odd_node] = rest.pop()
 
elif len(odd) <= zerocnt:
    for odd_node in odd:
        ans[odd_node] = nums[0].pop()
    rest = nums[0] + nums[1] + nums[2]
    for even_node in even:
        ans[even_node] = rest.pop()
 

else:
    for num in nums[1]:
        ans[even.pop()] = num
    for num in nums[2]:
        ans[odd.pop()] = num
    rest = nums[0]
    for even_node in even:
        ans[even_node] = rest.pop()
    for odd_node in odd:
        ans[odd_node] = rest.pop()
 
print(*ans)

Buggy submission (verdict: Runtime Error):
import sys
 
sys.setrecursionlimit(10 ** 6)
n = int(input())
 
edge = [[] for i in range(n)]
ans = [-1] * n
odd = []
even =[]
nums = [[],[],[]]
for i in range(1,n+1):
    nums[i % 3].append(i)
 
def dfs(x, last = -1, d = 0):
    if d % 2 == 0: even.append(x)
    else: odd.append(x)
    for nxt in edge[x]:
        if nxt == last:continue
        dfs(nxt, x, d + 1)
 
for i in range(n-1):
    a, b = map(int, input().split())
    a-=1
    b-=1
    edge[a].append(b)
    edge[b].append(a)
 
dfs(0)
 
# zerocnt = len(nums[0])
 
# if len(even) <= zerocnt:
#     for even_node in even:
#         ans[even_node] = nums[0].pop()
#     rest = nums[0] + nums[1] + nums[2]
#     for odd_node in odd:
#         ans[odd_node] = rest.pop()
 
# elif len(odd) <= zerocnt:
#     for odd_node in odd:
#         ans[odd_node] = nums[0].pop()
#     rest = nums[0] + nums[1] + nums[2]
#     for even_node in even:
#         ans[even_node] = rest.pop()
 

# else:
for num in nums[1]:
    ans[even.pop()] = num
for num in nums[2]:
    ans[odd.pop()] = num
rest = nums[0]
for even_node in even:
    ans[even_node] = rest.pop()
for odd_node in odd:
    ans[odd_node] = rest.pop()
 
print(*ans)

Buggy submission (verdict: Runtime Error):
import sys
 
sys.setrecursionlimit(10 ** 6)
n = int(input())
 
edge = [[] for i in range(n)]
ans = [-1] * n
odd = []
even =[]
nums = [[],[],[]]
for i in range(1,n+1):
    nums[i % 3].append(i)
 
def dfs(x, last = -1, d = 0):
    if d % 2 == 0: even.append(x)
    else: odd.append(x)
    for nxt in edge[x]:
        if nxt == last:continue
        dfs(nxt, x, d + 1)
 
for i in range(n-1):
    a, b = map(int, input().split())
    a-=1
    b-=1
    edge[a].append(b)
    edge[b].append(a)
 
dfs(0)
 
zerocnt = len(nums[0])
 
# if len(even) <= zerocnt:
#     for even_node in even:
#         ans[even_node] = nums[0].pop()
#     rest = nums[0] + nums[1] + nums[2]
#     for odd_node in odd:
#         ans[odd_node] = rest.pop()
 
# elif len(odd) <= zerocnt:
#     for odd_node in odd:
#         ans[odd_node] = nums[0].pop()
#     rest = nums[0] + nums[1] + nums[2]
#     for even_node in even:
#         ans[even_node] = rest.pop()
 
# len
# nums[1] >= nums[2] >= nums[0] >= nums[1]-1
# else:
    for num in nums[1]:
        ans[even.pop()] = num
    for num in nums[2]:
        ans[odd.pop()] = num
    rest = nums[0]
    for even_node in even:
        ans[even_node] = rest.pop()
    for odd_node in odd:
        ans[odd_node] = rest.pop()
 
print(*ans)

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

n = int(input())

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

odd = []
even = []

ans = [-1] * n
def dfs(x, last = -1, d = 0):
    if d%2 == 0: even.append(x)
    else: odd.append(x)
    for nxt in edge[x]:
        if nxt == last:
            continue
        dfs(nxt, x, d + 1)

dfs(0)

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

for num in nums[1]:
    ans[odd.pop()] = num
for num in nums[2]:
    ans[even.pop()] = num
rest = nums[0] + nums[1] + nums[2]
for num in rest:
    if len(odd):
        ans[odd.pop()] = num
    elif len(even):
        ans[even.pop()] = num

print(*ans)

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

n = int(input())

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

odd = []
even = []

ans = [-1] * n
def dfs(x, last = -1, d = 0):
    if d%2 == 0: even.append(x)
    else: odd.append(x)
    for nxt in edge[x]:
        if nxt == last:
            continue
        dfs(nxt, x, d + 1)

dfs(0)

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

for num in nums[1]:
    ans[odd.pop()] = num
for num in nums[2]:
    ans[even.pop()] = num
rest = nums[0] + nums[1] + nums[2]
for num in rest:
    if len(odd):
        ans[odd.pop()] = num
    elif len(even):
        ans[even.pop()] = num

print(*ans)