#P15568. [COCI 2025/2026 #5] 摆放 / Slaganje

    ID: 17432 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度NOI/NOI+/CTS 上传者: 标签>Special JudgeO2优化构造COCI(克罗地亚)2026

[COCI 2025/2026 #5] 摆放 / Slaganje

Background

The full score for this problem is 110110.

Problem Description

Mr. Malnar ordered a tree with NN vertices, labeled 1,2,…,N1,2,\dots,N. Unfortunately, due to a communication mistake, he received a total of NN such trees.

While waiting for a reply, he placed these trees around a regular NN-gon that also has NN vertices, and the polygon vertices are labeled 1,2,…,N1,2,\dots,N. More specifically, for each tree, he places each vertex of the tree onto some vertex of the polygon, and different vertices of the same tree cannot be placed onto the same polygon vertex.

He soon noticed that after doing this, every side and every diagonal of the polygon was “covered” by some tree edge. To make sure this was not a coincidence, he wants to reconstruct a set of placements, but it is too hard, so he asks you for help.

Formally, you need to construct an integer matrix (pij)(p_{ij}) (1≤i,j≤N1 \le i,j \le N) such that: for each i=1,2,…,Ni=1,2,\dots,N, the sequence pi1,pi2,…,piNp_{i1},p_{i2},\dots,p_{iN} is a permutation of 1,2,…,N1,2,\dots,N; and for any 1≤i<j≤N1 \le i < j \le N, there exists an integer kk such that vertices pkip_{k i} and pkjp_{k j} are connected by an edge in the original tree.

It can be proven that for any tree, a construction satisfying the conditions always exists.

Input Format

The first line contains an integer NN (3≤N≤20003 \le N \le 2000), representing the number of vertices of the tree/polygon.

The next N−1N-1 lines each contain two integers u,vu,v (1≤u,v≤N1 \le u,v \le N), representing an edge of the tree.

Output Format

Output NN lines. On the ii-th line, output pi1,pi2,…,piNp_{i1},p_{i2},\dots,p_{iN}.

3
1 2
1 3
2 3 1
1 2 3
3 1 2
4
1 2
1 3
2 4
1 4 3 2
3 2 1 4
2 1 4 3
4 3 2 1
8
1 2
1 3
2 4
2 5
3 6
4 7
5 8
8 1 5 4 3 6 2 7
4 3 6 2 7 8 1 5
2 7 8 1 5 4 3 6
1 5 4 3 6 2 7 8
3 6 2 7 8 1 5 4
7 8 1 5 4 3 6 2
6 2 7 8 1 5 4 3
5 4 3 6 2 7 8 1

Hint

Subtasks

Subtask Score Constraints
11 1010 There exists a vertex uu such that every edge is connected to uu
22 1515 N≤10N \le 10
33 2020 The tree is a path
44 2525 N≤300N \le 300
55 4040 No additional constraints

Translated by ChatGPT 5