## CEOI '22 P6 - Parking

View as PDF

Points: 30 (partial)
Time limit: 2.0s
Memory limit: 512M

Problem type

Valerija works as a valet at a fancy restaurant. Her job is to wait for the arrival of distinguished guests, politely greet them, obtain the keys of their vehicles, and park their vehicles at a nearby parking lot. Once the event is over, she makes sure that each of the guests regains custody of their vehicle and happily leaves the venue.

One evening, shortly after she finished parking all of the vehicles, she noticed a particularly interesting property regarding their colors. Namely, it turned out there were exactly vehicles at the parking lot, and they were colored in different colors, such that there were exactly two vehicles of each color. We denote the colors of the vehicles by integers from to .

The parking lot itself is organized as a sequence of parking spaces, denoted by integers from to , where each parking space can contain at most two vehicles. There is only one entrance to a parking space, and a vehicle can enter or exit the space if no other vehicle is blocking the entrance. We'll call the vehicle parked closer to the entrance the top vehicle, and the vehicle parked further from the entrance the bottom vehicle. Valerija parked the vehicles in such a way that each parking space is either empty, full (i.e. contains two vehicles), or contains a single bottom vehicle.

Illustration of the first example, showing the only possible first drive.

Valerija would like to repark the vehicles so that each pair of vehicles of the same color is parked in the same parking space. She doesn't care which parking space will contain which color, and which specific vehicle will be at the top or bottom of the parking space. She will repark the vehicles in a series of drives. In each drive, she will sit in a parked vehicle that is able to exit its current parking space, and will drive it to another parking space that is either:

• empty, in which case she parks it as a bottom vehicle, or
• contains a single parked vehicle of the same color as the one she's currently driving, in which case she parks it as a top vehicle.

Valerija wants to minimize the number of drives needed to repark the vehicles according to her wishes. Your task is to help her by finding the shortest sequence of drives that would achieve her goal, or determine that no such sequence exists.

#### Input Specification

The first line contains two space-separated integers and from the task description.

The -th of the next lines contains two space-separated integers and that describe the -th parking space. More precisely, the number represents the bottom vehicle color, and the number represents the top vehicle color. If a position in the parking space is empty, the corresponding integer will be equal to . It is guaranteed that no parking space contains only a top vehicle, i.e. if , then as well.

#### Output Specification

If there is no sequence of drives that could repark the vehicles according to Valerija's wishes, print -1 in the only line.

Otherwise, the first line should contain an integer , the smallest number of drives needed to satisfy Valerija's goal.

The -th of the next lines should describe the -th drive. More precisely, it should contain two integers, and , representing that Valerija should take a vehicle from parking space to parking space in the -th drive. Of course, the -th parking space must contain at least one vehicle at that point, and the vehicle closer to the entrance must be movable to parking space , i.e. parking space must be either empty or contain a vehicle of the same color.

#### Constraints

If your solution correctly determines the smallest number of drives in all test cases of a certain subtask, but incorrectly outputs the description of drives in some of them (or doesn't output it at all), it will receive of the points allocated to that particular subtask.

All parking spaces are initially either empty or full, and .
All parking spaces are initially either empty or full.

4 5
1 0
2 0
1 3
4 4
3 2

3
5 2
3 5
3 1

#### Explanation for Sample 1

The image from the task description depicts the initial state of the parking lot in this example. Notice that in this case, each drive is forced, i.e. there is only one valid first drive, only one valid second drive, and two equivalent third drives, after which we reach the end goal.

4 5
0 0
2 1
3 1
3 4
2 4

-1

5 7
1 0
2 1
2 3
4 3
5 4
5 0
0 0

6
2 1
3 7
4 7
2 3
5 4
5 6