C - ThREE Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600600

問題文

NN 頂点の木があります。頂点には 11 から NN までの番号がついており、 ii 番目の辺は頂点 aia_i と頂点 bib_i を結んでいます。

33 が大好きな高橋くんは、以下の条件を満たす 11 から NN までの整数の順列 p1,p2,,pNp_1, p_2, \ldots , p_N を探しています。

  • すべての頂点の組 (i,j)(i, j) について、頂点 ii と頂点 jj の距離が 33 であるならば、pip_ipjp_j の和または積が 33 の倍数である。

ただし、頂点 ii と頂点 jj の距離とは、頂点 ii から頂点 jj へ最短経路で移動するときに使用する辺の個数のことを指します。

高橋くんのために条件を満たす順列を 11 つ見つけてください。

制約

  • 2N2×1052\leq N\leq 2\times 10^5
  • 1ai,biN1\leq a_i, b_i \leq N
  • 与えられるグラフは木である

入力

入力は以下の形式で標準入力から与えられる。

NN
a1a_1 b1b_1
a2a_2 b2b_2
\vdots
aN1a_{N-1} bN1b_{N-1}

出力

問題の条件を満たす順列が存在しない場合は -111 行に出力せよ。

存在する場合、条件を満たす順列の 11 つを空白区切りで 11 行に出力せよ。


入力例 1Copy

Copy
5
1 2
1 3
3 4
3 5

出力例 1Copy

Copy
1 2 5 4 3 

距離が 33 である頂点の組は (2,4)(2, 4)(2,5)(2, 5)22 つです。

  • p2+p4=6p_2 + p_4 = 6
  • p2×p5=6p_2\times p_5 = 6

であるため、この順列は条件を満たします。

Score : 600600 points

Problem Statement

We have a tree with NN vertices. The vertices are numbered 11 to NN, and the ii-th edge connects Vertex aia_i and Vertex bib_i.

Takahashi loves the number 33. He is seeking a permutation p1,p2,,pNp_1, p_2, \ldots , p_N of integers from 11 to NN satisfying the following condition:

  • For every pair of vertices (i,j)(i, j), if the distance between Vertex ii and Vertex jj is 33, the sum or product of pip_i and pjp_j is a multiple of 33.

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

Help Takahashi by finding a permutation that satisfies the condition.

Constraints

  • 2N2×1052\leq N\leq 2\times 10^5
  • 1ai,biN1\leq a_i, b_i \leq N
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

NN
a1a_1 b1b_1
a2a_2 b2b_2
\vdots
aN1a_{N-1} bN1b_{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 1Copy

Copy
5
1 2
1 3
3 4
3 5

Sample Output 1Copy

Copy
1 2 5 4 3 

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

  • p2+p4=6p_2 + p_4 = 6
  • p2×p5=6p_2\times p_5 = 6

Thus, this permutation satisfies the condition.



2025-04-18 (Fri)
13:26:59 +00:00