#P6676. [COCI 2019/2020 #2] Slagalica
[COCI 2019/2020 #2] Slagalica
Description
This problem is collecting an SPJ. However, if you submit the correct solution, i.e., output the lexicographically smallest solution, you can still get AC.
Xiao Bin likes playing jigsaw puzzles.
Xiao Bin got a one-dimensional jigsaw puzzle game consisting of pieces. He soon realized that each piece belongs to one of the following types:

Also, among these pieces, there is exactly one type or type piece (the left border), and exactly one type or type piece (the right border).
Xiao Bin wants to arrange all pieces into a single row such that the first (leftmost) piece is of type or type , and the last (rightmost) piece is of type or type . Two pieces can be placed next to each other if and only if the shapes of their adjacent borders are different, i.e., one border is concave and the other border is convex.
This is too easy for Xiao Bin, so he decided to write a unique positive integer on each piece. Now he wants to find the lexicographically smallest arrangement.
Note: Pieces cannot be rotated.
Input Format
The input has lines.
The first line contains a positive integer .
Each of the next lines contains two integers and . Here, denotes the type of the piece, and is the number Fabian wrote on it. The input guarantees that all are distinct.
Output Format
Output one line.
If Xiao Bin cannot solve the puzzle, output -1. Otherwise, output the lexicographically smallest sequence of numbers on the pieces.
5
1 5
2 7
2 3
8 4
6 1
1 3 7 5 4
3
5 1
7 2
4 3
1 3 2
5
2 5
2 7
2 3
8 4
6 1
-1
Hint
Sample #1 Explanation
There are only solutions, as shown below:

You can see that the second solution is lexicographically smaller, so the output is 1 3 7 5 4.
Constraints
For of the testdata, , , and .
If you do not output the lexicographically smallest solution and only construct any valid solution, you can get of the score.
Notes
The score of this problem follows the original COCI setting, with a full score of .
Translated from COCI2019-2020 CONTEST #2 T2 Slagalica.
Translated by ChatGPT 5
京公网安备 11011102002149号