Alice and Bob
Time limit1sMemory limit128 MB
Given all polygon sides and non-crossing diagonals as an unordered edge list, reconstruct the convex polygon's cyclic vertex order.
Problem
Alice and Bob play a puzzle. Alice draws a convex polygon with vertices and labels the vertices with the integers in an arbitrary order. She then draws some diagonals so that no two of them cross (sharing an endpoint is not considered a crossing).
Alice lists, in a single mixed collection, every side of the polygon together with the diagonals she drew, each described only by its two endpoints. She does not reveal which pairs are sides and which are diagonals. From this list alone Bob must recover the cyclic order of the vertices around the border of the polygon.
Because the polygon is convex and the diagonals never cross, the border order is uniquely determined up to rotation and reflection. Write a program that, for each puzzle, reconstructs that border order.
Input
The first line contains one integer , the number of puzzles ().
Each puzzle is given on two lines.
- The first line contains two integers and (, ): the number of vertices and the number of diagonals.
- The second line contains integers describing the sides and the diagonals. For each with , the integers at positions and are the endpoints and of one side or diagonal, with and .
The sides and diagonals appear in an arbitrary order and there are no duplicates. Every puzzle is guaranteed to have a solution.
Output
Print exactly lines, one per puzzle. For the -th puzzle, print a permutation of : the vertices in the order they appear around the border of the polygon. The sequence must start at vertex , and its second element must be the smaller of the two border neighbours of vertex .