Problem:


Score : 600 points

Problem Statement

We have a tree with N vertices. The vertices are numbered 1 to N, and the i-th edge connects Vertex a_i and Vertex b_i.

Takahashi loves the number 3. He is seeking a permutation p_1, p_2, \ldots , p_N of integers from 1 to N satisfying the following condition:

For every pair of vertices (i, j), if the distance between Vertex i and Vertex j is 3, the sum or product of p_i and p_j is a multiple of 3.

Here the distance between Vertex i and Vertex j is the number of edges contained in the shortest path from Vertex i to Vertex j.

Help Takahashi by finding a permutation that satisfies the condition.

Constraints

2\leq N\leq 2\times 10^5

1\leq a_i, b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
\vdots
a_{N-1} b_{N-1}

Output

If no permutation satisfies the condition, print -1.

Otherwise, print a permutation satisfying the condition, with space in between.
If there are multiple solutions, you can print any of them.

Sample Input 1

5
1 2
1 3
3 4
3 5

Sample Output 1

1 2 5 4 3 

The distance between two vertices is 3 for the two pairs (2, 4) and (2, 5).

p_2 + p_4 = 6

p_2\times p_5 = 6

Thus, this permutation satisfies the condition.



Sample input:
5
1 2
1 3
3 4
3 5


Sample output:
1 2 5 4 3

Submission status: Runtime Error

Solution code:
# import sys
# sys.setrecursionlimit(2147483647)

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


vertice = [-1 for _ in range(n)]
stack = [(0, 0)]

while stack:
    parent, vertex = stack.pop()
    vertice[vertex] = vertice[parent] * (-1)
    for next in edges[vertex]:
        if next != parent:
            stack.append((vertex, next))

# vertice = [0 for _ in range(n)]
#
#
# def dfs(v, color_i):
#     vertice[v] = color_i
#     for i in edges[v]:
#         if vertice[i] == 0:
#             dfs(i, color_i*(-1))
#
# dfs(0, 1)

ans = []
if vertice.count(1) <= n // 3:
    not3 = [i for i in range(1, n+1) if i % 3 != 0]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == 1:
            if num3:
                ans.append(str(num3.pop()))
            elif not3:
                ans.append(str(not3.pop()))
        else:
            ans.append(str(not3.pop()))
elif vertice.count(-1) <= n // 3:
    not3 = [i for i in range(1, n+1) if i % 3 != 0]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == -1:
            if num3:
                ans.append(str(num3.pop()))
            elif not3:
                ans.append(str(not3.pop()))
        else:
            ans.append(str(not3.pop()))
else:
    num1 = [i for i in range(1, n+1) if i % 3 == 1]
    num2 = [i for i in range(1, n+1) if i % 3 == 2]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == 1:
            if num1:
                ans.append(str(num1.pop()))
            else:
                ans.append(str(num3.pop()))
        else:
            if num2:
                ans.append(str(num2.pop()))
            else:
                ans.append(str(num3.pop()))

print(" ".join(ans))

Submission status: Runtime Error

Solution code:
# import sys
# sys.setrecursionlimit(2147483647)

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


vertice = [-1 for _ in range(n)]
stack = [(0, 0)]

while stack:
    parent, vertex = stack.pop()
    vertice[vertex] = vertice[parent] * (-1)
    for next in edges[vertex]:
        if next != parent:
            stack.append((vertex, next))

# vertice = [0 for _ in range(n)]
#
#
# def dfs(v, color_i):
#     vertice[v] = color_i
#     for i in edges[v]:
#         if vertice[i] == 0:
#             dfs(i, color_i*(-1))
#
# dfs(0, 1)

ans = []
if vertice.count(1) <= n // 3:
    not3 = [i for i in range(1, n+1) if i % 3 != 0]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == 1:
            if num3:
                ans.append(str(num3.pop()))
            else:
                ans.append(str(not3.pop()))
        else:
            ans.append(str(not3.pop()))
elif vertice.count(-1) <= n // 3:
    not3 = [i for i in range(1, n+1) if i % 3 != 0]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    if v == -1:
        if num3:
            ans.append(str(num3.pop()))
        else:
            ans.append(str(not3.pop()))
    else:
        ans.append(str(not3.pop()))
else:
    num1 = [i for i in range(1, n+1) if i % 3 == 1]
    num2 = [i for i in range(1, n+1) if i % 3 == 2]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == 1:
            if num1:
                ans.append(str(num1.pop()))
            else:
                ans.append(str(num3.pop()))
        else:
            if num2:
                ans.append(str(num2.pop()))
            else:
                ans.append(str(num3.pop()))

print(" ".join(ans))

Submission status: Runtime Error

Solution code:
# import sys
# sys.setrecursionlimit(2147483647)

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


vertice = [-1 for _ in range(n)]
stack = [(0, 0)]

while stack:
    parent, vertex = stack.pop()
    vertice[vertex] = vertice[parent] * (-1)
    for next in edges[vertex]:
        if next != parent:
            stack.append((vertex, next))

# vertice = [0 for _ in range(n)]
#
#
# def dfs(v, color_i):
#     vertice[v] = color_i
#     for i in edges[v]:
#         if vertice[i] == 0:
#             dfs(i, color_i*(-1))
#
# dfs(0, 1)

ans = []
if vertice.count(1) <= n // 3:
    not3 = [i for i in range(1, n+1) if i % 3 != 0]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == 1:
            if num3:
                ans.append(str(num3.pop()))
                continue
        ans.append(str(not3.pop()))
elif vertice.count(-1) <= n // 3:
    not3 = [i for i in range(1, n+1) if i % 3 != 0]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == -1:
            if num3:
                ans.append(str(num3.pop()))
                continue
        ans.append(str(not3.pop()))
else:
    num1 = [i for i in range(1, n+1) if i % 3 == 1]
    num2 = [i for i in range(1, n+1) if i % 3 == 2]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == 1:
            if num1:
                ans.append(str(num1.pop()))
            else:
                ans.append(str(num3.pop()))
        else:
            if num2:
                ans.append(str(num2.pop()))
            else:
                ans.append(str(num3.pop()))

print(" ".join(ans))

Submission status: Runtime Error

Solution code:
N = int(input())
X = [list(map(int, input().split())) for i in range(N-1)]
X = sorted(X, key = lambda x: (x[0], x[1]))

table = [[] for _ in range(N)]
for a, b in X:
    table[a-1].append(b)
odd = []
even = []
odd.append(1)

def add(x):
    for b in table[x-1]:
        if x == 1:
            odd.append(b)
            add(b)
        elif x in even and b not in odd:
            odd.append(b)
            add(b)
        elif x in odd and b not in even:
            even.append(b)
    return 0

for i in range(1, N+1):
    add(i)
p = [i+1 for i in range(N)]
ans = [0 for i in range(N)]

def less_one_third(minority, majority):
    for index, x in enumerate(minority):
        value = 3*index +3
        ans[x-1] = value
        p.remove(value)
    for index, x in enumerate(majority):
        ans[x-1] = p[index]
        p.remove(p[index])
    # ans[0] = p[0]
    return ans

def greater_one_third():
    even_size, odd_size = len(even), len(odd)
    for i in range(N//3 +1):
        if i < even_size:
            even_value = 3*i +1
            ans[even[0]-1] = even_value
            p.remove(even_value)
            even.remove(even[0])
        if i < odd_size:
            odd_value = 3*i +2
            ans[odd[0]-1] = odd_value
            p.remove(odd_value)
            odd.remove(odd[0])

    for index, x in enumerate(even):
        ans[x-1] = p[index]
        p.remove(p[index])
    for index, x in enumerate(odd):
        ans[x-1] = p[index]
        p.remove(p[index])
    # ans[0] = p[0]
    return ans

if len(odd) <= N/3:
    ans = less_one_third(odd, even)
elif len(even) <= N/3:
    ans = less_one_third(even, odd)
else:
    ans = greater_one_third()
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
X = [list(map(int, input().split())) for i in range(N-1)]
X = sorted(X, key = lambda x: (x[0], x[1]))

table = [[] for _ in range(N)]
for a, b in X:
    table[a-1].append(b)
odd = []
even = []
odd.append(1)

def add(x):
    for b in table[x-1]:
        if x == 1:
            odd.append(b)
            add(b)
        elif x in even and b not in odd:
            odd.append(b)
            add(b)
        elif x in odd and b not in even:
            even.append(b)
    return 0

for i in range(1, N+1):
    add(i)
p = [i+1 for i in range(N)]
ans = [0 for i in range(N)]

def less_one_third(minority, majority):
    for index, x in enumerate(minority):
        value = 3*index +3
        ans[x-1] = value
        p.remove(value)
    for index, x in enumerate(majority):
        ans[x-1] = p[index]
        p.remove(p[index])
    # ans[0] = p[0]
    return ans

def greater_one_third():
    print(odd, even)
    even_size, odd_size = len(even), len(odd)
    for i in range(N//3 +1):
        print(i, even_size, odd_size)
        if i < even_size:
            even_value = 3*i +1
            print(even_value)
            ans[even[0]-1] = even_value
            p.remove(even_value)
            even.remove(even[0])
        if i < odd_size:
            odd_value = 3*i +2
            print(odd_value)
            ans[odd[0]-1] = odd_value
            p.remove(odd_value)
            odd.remove(odd[0])

    for index, x in enumerate(even):
        ans[x-1] = p[index]
        p.remove(p[index])
    for index, x in enumerate(odd):
        ans[x-1] = p[index]
        p.remove(p[index])
    # ans[0] = p[0]
    return ans

if len(odd) <= N/3:
    ans = less_one_third(odd, even)
elif len(even) <= N/3:
    ans = less_one_third(even, odd)
else:
    ans = greater_one_third()
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
X = [list(map(int, input().split())) for i in range(N-1)]
X = sorted(X, key = lambda x: (x[0], x[1]))

table = [[] for _ in range(N)]
for a, b in X:
    table[a-1].append(b)
odd = []
even = []
# odd.append(1)

def add(x):
    for b in table[x-1]:
        if x == 1:
            odd.append(b)
            add(b)
        elif x in even and b not in odd:
            odd.append(b)
            add(b)
        elif x in odd and b not in even:
            even.append(b)
    return 0

for i in range(1, N+1):
    add(i)
p = [i+1 for i in range(N)]
ans = [0 for i in range(N)]

def less_one_third(minority, majority):
    for index, x in enumerate(minority):
        value = 3*index +3
        ans[x-1] = value
        p.remove(value)
    for index, x in enumerate(majority):
        ans[x-1] = p[index]
        p.remove(p[index])
    ans[0] = p[0]
    return ans

def greater_one_third():
    print(odd, even)
    even_size, odd_size = len(even), len(odd)
    for i in range(N//3 +1):
        print(i, even_size, odd_size)
        if i < even_size:
            even_value = 3*i +1
            print(even_value)
            ans[even[0]-1] = even_value
            p.remove(even_value)
            even.remove(even[0])
        if i < odd_size:
            odd_value = 3*i +2
            print(odd_value)
            ans[odd[0]-1] = odd_value
            p.remove(odd_value)
            odd.remove(odd[0])

    for index, x in enumerate(even):
        ans[x-1] = p[index]
        p.remove(p[index])
    for index, x in enumerate(odd):
        ans[x-1] = p[index]
        p.remove(p[index])
    ans[0] = p[0]
    return ans

if len(odd) <= N/3:
    ans = less_one_third(odd, even)
elif len(even) <= N/3:
    ans = less_one_third(even, odd)
else:
    ans = greater_one_third()
print(ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(2147483647)

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


vertice = [0 for _ in range(n)]


def dfs(v, color_i):
    vertice[v] = color_i
    for i in edges[v]:
        if vertice[i] == 0:
            dfs(i, color_i*(-1))

dfs(0, 1)

ans = []
if vertice.count(1) <= n // 3:
    not3 = [i for i in range(1, n+1) if i % 3 != 0]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == 1:
            if num3:
                ans.append(str(num3.pop()))
                continue
        ans.append(str(not3.pop()))
elif vertice.count(-1) <= n // 3:
    not3 = [i for i in range(1, n+1) if i % 3 != 0]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == -1:
            if num3:
                ans.append(str(num3.pop()))
                continue
        ans.append(str(not3.pop()))
else:
    num1 = [i for i in range(1, n+1) if i % 3 == 1]
    num2 = [i for i in range(1, n+1) if i % 3 == 2]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == 1:
            if num1:
                ans.append(str(num1.pop()))
            else:
                ans.append(str(num3.pop()))
        else:
            if num2:
                ans.append(str(num2.pop()))
            else:
                ans.append(str(num3.pop()))

print(" ".join(ans))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**9)

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


vertice = [0 for _ in range(n)]


def dfs(v, color_i):
    vertice[v] = color_i
    for i in edges[v]:
        if vertice[i] == 0:
            dfs(i, color_i*(-1))

dfs(0, 1)

ans = []
if vertice.count(1) <= n // 3:
    not3 = [i for i in range(1, n+1) if i % 3 != 0]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == 1:
            if num3:
                ans.append(str(num3.pop()))
                continue
        ans.append(str(not3.pop()))
elif vertice.count(-1) <= n // 3:
    not3 = [i for i in range(1, n+1) if i % 3 != 0]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == -1:
            if num3:
                ans.append(str(num3.pop()))
                continue
        ans.append(str(not3.pop()))
else:
    num1 = [i for i in range(1, n+1) if i % 3 == 1]
    num2 = [i for i in range(1, n+1) if i % 3 == 2]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == 1:
            if num1:
                ans.append(str(num1.pop()))
            else:
                ans.append(str(num3.pop()))
        else:
            if num2:
                ans.append(str(num2.pop()))
            else:
                ans.append(str(num3.pop()))

print(" ".join(ans))

Submission status: Runtime Error

Solution code:
# 完全二部グラフで大丈夫なら全部大丈夫(最悪で大丈夫かどうか)
# 簡単のためN=3kとする(3の倍数以外でもほとんど同じなので)
# 0,1,2がk個ずつ

# 1-1,2-2間に辺があるとダメ(これだけを避ければよい)
#   1-2,1-0,2-1,2-0,0-0はOKなので

# 二部グラフで片側の頂点が2k以上(もう一方がk個以下)の場合
#   2k以上の方に1,2を割り当てて、反対側をすべて0にする

# 両方とも(k,2k)の場合
# 両方k以上なのでLを1、Rを2で埋めて残りを0で埋める


def main():
    from collections import deque
    from itertools import chain
    import sys
    input = sys.stdin.readline

    # INPUT
    N = int(input())

    graph = tuple(set() for _ in range(N))
    for _ in range(N - 1):
        a, b = (int(x) - 1 for x in input().split())
        graph[a].add(b)
        graph[b].add(a)

    # BFS
    dq = deque([0])
    parity = [-1] * N
    parity[0] = 0

    while dq:
        v = dq.popleft()
        p = parity[v]
        for u in graph[v]:
            if ~parity[u]: continue
            parity[u] = 1 ^ p
            dq.append(u)

    # SOLVE
    count_0 = parity.count(0)
    k = N // 3  # 3の倍数の個数

    if count_0 <= k:
        # parity=0を3の倍数で埋める
        # 残りを余った数で埋める
        g1 = range(1, N + 1, 3)
        g2 = range(2, N + 1, 3)
        g3 = range(3, N + 1, 3)
        g3r = range(N - N % 3, 3 - 1, -3)
        g12r3 = chain(g1, g2, g3r)

        for v in range(N):
            parity[v] = next(g3) if parity[v] == 0 else next(g12r3)

    elif N - count_0 <= k:
        # parity=1を3の倍数で埋める
        # 残りを余った数で埋める
        g1 = range(1, N + 1, 3)
        g2 = range(2, N + 1, 3)
        g3 = range(3, N + 1, 3)
        g3r = range(N - N % 3, 3 - 1, -3)
        g12r3 = chain(g1, g2, g3r)

        for v in range(N):
            parity[v] = next(g3) if parity[v] == 1 else next(g12r3)

    else:
        # parity=0を(mod3=1)で埋める
        # parity=1を(mod3=2)で埋める
        # 残りを3の倍数で埋める
        g1 = range(1, N + 1, 3)
        g2 = range(2, N + 1, 3)
        g3 = range(3, N + 1, 3)
        g3r = range(N - N % 3, 3 - 1, -3)
        g13 = chain(g1, g3)
        g2r3 = chain(g2, g3r)

        for v in range(N):
            if parity[v] == 0:
                parity[v] = next(g13)
            else:
                parity[v] = next(g2r3)

    print(*parity)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
X = [list(map(int, input().split())) for i in range(N-1)]
X = sorted(X, key = lambda x: (x[0], x[1]))

table = [[] for _ in range(N)]
for a, b in X:
    table[a-1].append(b)
odd = []
even = []
odd.append(1)

def add(x):
    for b in table[x-1]:
        if x == 1:
            odd.append(b)
            add(b)
        elif x in even and b not in odd:
            odd.append(b)
            add(b)
        elif x in odd and b not in even:
            even.append(b)
    return 0

for i in range(1, N+1):
    add(i)
p = [i+1 for i in range(N)]
ans = [0 for i in range(N)]

def less_one_third(minority, majority, gap):
    for index, x in enumerate(minority):
        value = 3*index +gap
        ans[x-1] = value
        p.remove(value)
    for index, x in enumerate(majority):
        ans[x-1] = p[index]
    return ans
def greater_one_third():
    for i in range(N//3):
        even_value = 3*i +1
        ans[even[i]-1] = even_value
        p.remove(even_value)
        even.remove(even[i])

        odd_value = 3*i +2
        ans[odd[i]-1] = odd_value
        p.remove(odd_value)
        odd.remove(odd[i])

    for index, x in enumerate(even):
        ans[x-1] = p[index]
        p.remove(p[index])
    for index, x in enumerate(odd):
        ans[x-1] = p[index]
    return ans

if len(odd) <= N/3:
    ans = less_one_third(odd, even, 3)
elif len(even) <= N/3:
    ans = less_one_third(even, odd, 3)
else:
    ans = greater_one_third()
print(ans)

Submission status: Runtime Error

Solution code:
# 完全二部グラフで大丈夫なら全部大丈夫(最悪で大丈夫かどうか)
# 簡単のためN=3kとする(3の倍数以外でもほとんど同じなので)
# 0,1,2がk個ずつ

# 1-1,2-2間に辺があるとダメ(これだけを避ければよい)
#   1-2,1-0,2-1,2-0,0-0はOKなので

# 二部グラフで片側の頂点が2k以上(もう一方がk個以下)の場合
#   2k以上の方に1,2を割り当てて、反対側をすべて0にする

# 両方とも(k,2k)の場合
# 両方k以上なのでLを1、Rを2で埋めて残りを0で埋める


def main():
    from collections import deque
    from itertools import chain
    import sys
    input = sys.stdin.readline

    # INPUT
    N = int(input())

    graph = tuple(set() for _ in range(N))
    for _ in range(N - 1):
        a, b = (int(x) - 1 for x in input().split())
        graph[a].add(b)
        graph[b].add(a)

    # BFS
    dq = deque([0])
    parity = [-1] * N
    parity[0] = 0

    while dq:
        v = dq.popleft()
        p = parity[v]
        for u in graph[v]:
            if ~parity[u]: continue
            parity[u] = 1 ^ p
            dq.append(u)

    # SOLVE
    count_0 = parity.count(0)
    k = N // 3  # 3の倍数の個数

    if count_0 <= k:
        # parity=0を3の倍数で埋める
        # 残りを余った数で埋める
        g1 = range(1, N + 1, 3)
        g2 = range(2, N + 1, 3)
        g3 = range(3, N + 1, 3)
        g12 = chain(g1, g2)

        for v in range(N):
            parity[v] = next(g3) if parity[v] == 0 else next(g12)

    elif N - count_0 <= k:
        # parity=1を3の倍数で埋める
        # 残りを余った数で埋める
        g1 = range(1, N + 1, 3)
        g2 = range(2, N + 1, 3)
        g3 = range(3, N + 1, 3)
        g12 = chain(g1, g2)

        for v in range(N):
            parity[v] = next(g3) if parity[v] == 1 else next(g12)

    else:
        # parity=0を(mod3=1)で埋める
        # parity=1を(mod3=2)で埋める
        # 残りを3の倍数で埋める
        g1 = range(1, N + 1, 3)
        g2 = range(2, N + 1, 3)
        g3 = range(3, N + 1, 3)
        g3r = range(N - N % 3, 3 - 1, -3)
        g13 = chain(g1, g3)
        g2r3 = chain(g2, g3r)

        for v in range(N):
            if parity[v] == 0:
                parity[v] = next(g13)
            else:
                parity[v] = next(g2r3)

    print(*parity)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**9)

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


vertice = [0 for _ in range(n)]

def dfs(v, color_i):
    vertice[v] = color_i
    for i in edges[v]:
        if vertice[i] == 0:
            dfs(i, color_i*(-1))

dfs(0, 1)

ans = []
if vertice.count(1) <= n // 3:
    not3 = [i for i in range(1, n+1) if i % 3 != 0]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == 1:
            if num3:
                ans.append(str(num3.pop()))
                continue
        ans.append(str(not3.pop()))
elif vertice.count(-1) <= n // 3:
    not3 = [i for i in range(1, n+1) if i % 3 != 0]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == -1:
            if num3:
                ans.append(str(num3.pop()))
                continue
        ans.append(str(not3.pop()))
else:
    num1 = [i for i in range(1, n+1) if i % 3 == 1]
    num2 = [i for i in range(1, n+1) if i % 3 == 2]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == 1:
            if num1:
                ans.append(str(num1.pop()))
            else:
                ans.append(str(num3.pop()))
        else:
            if num2:
                ans.append(str(num2.pop()))
            else:
                ans.append(str(num3.pop()))

print(" ".join(ans))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**10)

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


vertice = [0 for _ in range(n)]

def dfs(v, color_i):
    vertice[v] = color_i
    for i in edges[v]:
        if vertice[i] == 0:
            dfs(i, color_i*(-1))

dfs(0, 1)

ans = []
if vertice.count(1) <= n // 3:
    not3 = [i for i in range(1, n+1) if i % 3 != 0]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == 1:
            if num3:
                ans.append(str(num3.pop()))
                continue
        ans.append(str(not3.pop()))
elif vertice.count(-1) <= n // 3:
    not3 = [i for i in range(1, n+1) if i % 3 != 0]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == -1:
            if num3:
                ans.append(str(num3.pop()))
                continue
        ans.append(str(not3.pop()))
else:
    num1 = [i for i in range(1, n+1) if i % 3 == 1]
    num2 = [i for i in range(1, n+1) if i % 3 == 2]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == 1:
            if num1:
                ans.append(str(num1.pop()))
            else:
                ans.append(str(num3.pop()))
        else:
            if num2:
                ans.append(str(num2.pop()))
            else:
                ans.append(str(num3.pop()))

print(" ".join(ans))

Submission status: Runtime Error

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


vertice = [0 for _ in range(n)]

def dfs(v, color_i):
    vertice[v] = color_i
    for i in edges[v]:
        if vertice[i] == 0:
            dfs(i, color_i*(-1))

dfs(0, 1)

ans = []
if vertice.count(1) <= n // 3:
    not3 = [i for i in range(1, n+1) if i % 3 != 0]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == 1:
            if num3:
                ans.append(str(num3.pop()))
                continue
        ans.append(str(not3.pop()))
elif vertice.count(-1) <= n // 3:
    not3 = [i for i in range(1, n+1) if i % 3 != 0]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == -1:
            if num3:
                ans.append(str(num3.pop()))
                continue
        ans.append(str(not3.pop()))
else:
    num1 = [i for i in range(1, n+1) if i % 3 == 1]
    num2 = [i for i in range(1, n+1) if i % 3 == 2]
    num3 = [i for i in range(1, n+1) if i % 3 == 0]
    for v in vertice:
        if v == 1:
            if num1:
                ans.append(str(num1.pop()))
            else:
                ans.append(str(num3.pop()))
        else:
            if num2:
                ans.append(str(num2.pop()))
            else:
                ans.append(str(num3.pop()))

print(" ".join(ans))

Submission status: Runtime Error

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


vertice = [0 for _ in range(n)]

def dfs(v, color_i):
    vertice[v] = color_i
    for i in edges[v]:
        if vertice[i] == 0:
            dfs(i, color_i*(-1))

dfs(0, 1)

ans = []
if vertice.count(1) <= n // 3:
    not3 = [i for i in range(1, N+1) if i % 3 != 0]
    num3 = [i for i in range(1, N+1) if i % 3 == 0]
    for v in vertice:
        if v == 1:
            if num3:
                ans.append(str(num3.pop()))
                continue
        ans.append(str(not3.pop()))
elif vertice.count(-1) <= n // 3:
    not3 = [i for i in range(1, N+1) if i % 3 != 0]
    num3 = [i for i in range(1, N+1) if i % 3 == 0]
    for v in vertice:
        if v == -1:
            if num3:
                ans.append(str(num3.pop()))
                continue
        ans.append(str(not3.pop()))
else:
    num1 = [i for i in range(1, N+1) if i % 3 == 1]
    num2 = [i for i in range(1, N+1) if i % 3 == 2]
    num3 = [i for i in range(1, N+1) if i % 3 == 0]
    for v in vertice:
        if v == 1:
            if num1:
                ans.append(str(num1.pop()))
            else:
                ans.append(str(num3.pop()))
        else:
            if num2:
                ans.append(str(num2.pop()))
            else:
                ans.append(str(num3.pop()))

print(" ".join(ans))

Submission status: Runtime Error

Solution code:
# 完全二部グラフで大丈夫なら全部大丈夫(最悪で大丈夫かどうか)
# 簡単のためN=3kとする(3の倍数以外でもほとんど同じなので)
# 0,1,2がk個ずつ

# 1-1,2-2間に辺があるとダメ(これだけを避ければよい)
#   1-2,1-0,2-1,2-0,0-0はOKなので

# 二部グラフで片側の頂点が2k以上(もう一方がk個以下)の場合
#   2k以上の方に1,2を割り当てて、反対側をすべて0にする

# 両方とも(k,2k)の場合
# 両方k以上なのでLを1、Rを2で埋めて残りを0で埋める


def main():
    from collections import deque
    from itertools import chain
    import sys
    input = sys.stdin.readline

    # INPUT
    N = int(input())

    graph = tuple(set() for _ in range(N))
    for _ in range(N - 1):
        a, b = (int(x) - 1 for x in input().split())
        graph[a].add(b)
        graph[b].add(a)

    # BFS
    dq = deque([0])
    parity = [-1] * N
    parity[0] = 0

    while dq:
        v = dq.popleft()
        p = parity[v]
        for u in graph[v]:
            if ~parity[u]: continue
            parity[u] = 1 ^ p
            dq.append(u)

    # SOLVE
    count_0 = parity.count(0)
    k = N // 3  # 3の倍数の個数

    g1 = range(1, N + 1, 3)
    g2 = range(2, N + 1, 3)
    g3 = range(3, N + 1, 3)
    g3r = range((N // 3) * 3, 3 - 1, -3)
    g12 = chain(g1, g2)
    g13 = chain(g1, g3)
    g2r3 = chain(g2, g3r)
    if count_0 <= k:
        # parity=0を3の倍数で埋める
        # 残りを余った数で埋める
        for v in range(N):
            parity[v] = next(g3) if parity[v] == 0 else next(g12)

    elif N - count_0 <= k:
        # parity=1を3の倍数で埋める
        # 残りを余った数で埋める
        for v in range(N):
            parity[v] = next(g3) if parity[v] == 1 else next(g12)

    else:
        # parity=0を(mod3=1)で埋める
        # parity=1を(mod3=2)で埋める
        # 残りを3の倍数で埋める
        for v in range(N):
            if parity[v] == 0:
                parity[v] = next(g13)
            else:
                parity[v] = next(g2r3)

    print(*parity)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# 完全二部グラフで大丈夫なら全部大丈夫(最悪で大丈夫かどうか)
# 簡単のためN=3kとする(3の倍数以外でもほとんど同じなので)
# 0,1,2がk個ずつ

# 1-1,2-2間に辺があるとダメ(これだけを避ければよい)
#   1-2,1-0,2-1,2-0,0-0はOKなので

# 二部グラフで片側の頂点が2k以上(もう一方がk個以下)の場合
#   2k以上の方に1,2を割り当てて、反対側をすべて0にする

# 両方とも(k,2k)の場合
# 両方k以上なのでLを1、Rを2で埋めて残りを0で埋める


def main():
    from collections import deque
    from itertools import chain
    import sys
    input = sys.stdin.readline

    # INPUT
    N = int(input())

    graph = tuple(set() for _ in range(N))
    for _ in range(N - 1):
        a, b = (int(x) - 1 for x in input().split())
        graph[a].add(b)
        graph[b].add(a)

    # BFS
    dq = deque([0])
    parity = [-1] * N
    parity[0] = 0

    while dq:
        v = dq.popleft()
        p = parity[v]
        for u in graph[v]:
            if ~parity[u]: continue
            parity[u] = 1 ^ p
            dq.append(u)

    # SOLVE
    count_0 = parity.count(0)
    k = N // 3  # 3の倍数の個数

    g1 = range(1, N + 1, 3)
    g2 = range(2, N + 1, 3)
    g3 = range(3, N + 1, 3)
    g3r = range(N, 3 - 1, -3)
    g12 = chain(g1, g2)
    g13 = chain(g1, g3)
    g2r3 = chain(g2, g3r)
    if count_0 <= k:
        # parity=0を3の倍数で埋める
        # 残りを余った数で埋める
        for v in range(N):
            parity[v] = next(g3) if parity[v] == 0 else next(g12)

    elif N - count_0 <= k:
        # parity=1を3の倍数で埋める
        # 残りを余った数で埋める
        for v in range(N):
            parity[v] = next(g3) if parity[v] == 1 else next(g12)

    else:
        # parity=0を(mod3=1)で埋める
        # parity=1を(mod3=2)で埋める
        # 残りを3の倍数で埋める
        for v in range(N):
            if parity[v] == 0:
                parity[v] = next(g13)
            else:
                parity[v] = next(g2r3)

    print(*parity)


if __name__ == '__main__':
    main()