## Yet Another Contest 3 P1 - Shell Swap Scam

View as PDF

Points: 5 (partial)
Time limit: 1.0s
Python 2.0s
Memory limit: 512M

Author:
Problem type

Nils is playing a shell swapping game! The game involves identical-looking opaque shells being placed in a row, aligned with positions labelled from to from left to right. Initially, the stone is placed under the shell in the -th position. Then, a man will quickly perform swaps; in the -th swap, the man will choose two different positions and and swaps the shells in those positions. If the stone is under a shell being swapped, then the stone will move with that shell, changing position. After the swaps, Nils is asked to identify which shell is covering the stone.

With the moves being so fast, Nils is not sure, and guesses that the stone has ended up under the shell in the -th position. To everyone's surprise, Nils is correct! However, the man is a scammer and is not eager to part with his money - Nils will only receive his reward if he can list all swaps that were made!

Unfortunately, the man moved too fast for Nils to see all the moves that were made. Can you help Nils find any sequence of moves that could have resulted in the stone ending up in the -th position, without contradicting the moves that Nils observed?

#### Constraints

There is at least one swap in which Nils did not see which shells were swapped.

It is guaranteed that there exists at least one valid sequence of swaps which matches Nils' observations.

#### Input Specification

The first line contains four space-separated integers, , representing the number of shells, the number of swaps, the starting position of the shell, and the ending position of the shell respectively.

The -th of the following lines represents the -th swap. If the line contains a single integer , then Nils does not know which shells were swapped. Otherwise, the line contains two space-separated integers , representing the positions of the shells swapped.

#### Output Specification

Output lines. The -th line should contain two different space-separated integers, representing the positions of the shells swapped. The order of the two integers on any line does not matter. If there are multiple possible sequences of swaps, output any of them.

#### Sample Input

3 3 1 3
1 2
-1
3 2

#### Sample Output

1 2
1 3
2 3

#### Explanation

Nils knows that shells in positions and were swapped in the first swap, and that the shells in positions and were swapped in the third swap. However, he did not see which shells were swapped in the second swap.

Consider the scenario where the shells in positions and were swapped in the second swap. Then, the stone would start at position , move to position after the first swap, stay in position after the second swap, and move to position after the third swap. This matches Nils' observations, so this output would be accepted.