P17660 [IO 2025 #3] Nightmare Maze
Description
To save her friend, Wednesday has entered a nightmare maze created by an ancient curse. The maze consists of $n$ rooms connected by $n-1$ corridors, so its structure is a tree. Each room has a distinct number from $1$ to $n$.
The curse on the maze twists space, so Wednesday can not only move from her current room to an adjacent room, but also move directly to any room reachable through at most three corridors. In other words, if the shortest path between two rooms in the maze contains no more than three corridors, Wednesday can skip the rooms in between and move directly between these two rooms.
To break the curse and find the exit, Wednesday must start from any room, visit every room exactly once, and then return to the room she started from. Output a route that this gloomy heroine can follow.
Input Format
The first line contains an integer $n$ — the number of rooms in the nightmare maze, satisfying $3\le n\le 2\cdot 10^5$.
Each of the next $n-1$ lines contains two integers $u,v$, meaning there is a corridor between room $u$ and room $v$, satisfying $1\le u,v\le n$.
It is guaranteed that the maze is a tree. The rooms are numbered from $1$ to $n$.
Output Format
Output $n+1$ room numbers describing Wednesday's route. The first $n$ numbers must contain every room exactly once, and the last number must be the same as the first one. For any two adjacent numbers in the output, the shortest path between the corresponding rooms must contain no more than $3$ corridors.
Explanation/Hint
Translated by QwenWork