

Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 点
問題文
頂点の木があります。頂点には から までの番号がついており、 番目の辺は頂点 と頂点 を結んでいます。
が大好きな高橋くんは、以下の条件を満たす から までの整数の順列 を探しています。
- すべての頂点の組 について、頂点 と頂点 の距離が であるならば、 と の和または積が の倍数である。
ただし、頂点 と頂点 の距離とは、頂点 から頂点 へ最短経路で移動するときに使用する辺の個数のことを指します。
高橋くんのために条件を満たす順列を つ見つけてください。
制約
- 与えられるグラフは木である
入力
入力は以下の形式で標準入力から与えられる。
出力
問題の条件を満たす順列が存在しない場合は -1
と 行に出力せよ。
存在する場合、条件を満たす順列の つを空白区切りで 行に出力せよ。
入力例 1Copy
5 1 2 1 3 3 4 3 5
出力例 1Copy
1 2 5 4 3
距離が である頂点の組は と の つです。
であるため、この順列は条件を満たします。
Score : points
Problem Statement
We have a tree with vertices. The vertices are numbered to , and the -th edge connects Vertex and Vertex .
Takahashi loves the number . He is seeking a permutation of integers from to satisfying the following condition:
- For every pair of vertices , if the distance between Vertex and Vertex is , the sum or product of and is a multiple of .
Here the distance between Vertex and Vertex is the number of edges contained in the shortest path from Vertex to Vertex .
Help Takahashi by finding a permutation that satisfies the condition.
Constraints
- The given graph is a tree.
Input
Input is given from Standard Input in the following format:
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
5 1 2 1 3 3 4 3 5
Sample Output 1Copy
1 2 5 4 3
The distance between two vertices is for the two pairs and .
Thus, this permutation satisfies the condition.