P15568 [COCI 2025/2026 #5] Placement / Slaganje

Background

The full score for this problem is $110$.

Description

Mr. Malnar ordered a tree with $N$ vertices, labeled $1,2,\dots,N$. Unfortunately, due to a communication mistake, he received a total of $N$ such trees. While waiting for a reply, he placed these trees around a regular $N$-gon that also has $N$ vertices, and the polygon vertices are labeled $1,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 $(p_{ij})$ ($1 \le i,j \le N$) such that: for each $i=1,2,\dots,N$, the sequence $p_{i1},p_{i2},\dots,p_{iN}$ is a permutation of $1,2,\dots,N$; and for any $1 \le i < j \le N$, there exists an integer $k$ such that vertices $p_{k i}$ and $p_{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 $N$ ($3 \le N \le 2000$), representing the number of vertices of the tree/polygon. The next $N-1$ lines each contain two integers $u,v$ ($1 \le u,v \le N$), representing an edge of the tree.

Output Format

Output $N$ lines. On the $i$-th line, output $p_{i1},p_{i2},\dots,p_{iN}$.

Explanation/Hint

#### Subtasks | Subtask | Score | Constraints | | :----: | :--: | :--: | | $1$ | $10$ | There exists a vertex $u$ such that every edge is connected to $u$ | | $2$ | $15$ | $N \le 10$ | | $3$ | $20$ | The tree is a path | | $4$ | $25$ | $N \le 300$ | | $5$ | $40$ | No additional constraints | Translated by ChatGPT 5