Cleaning the Room
Time limit1sMemory limit256 MB
Each of N bottles fits exactly two of L drawers, and each bottle in order claims a drawer through a chain of displaced bottles or is discarded.
- Level
Medium7 of 10
- Topics
- Union-find, Graph, Greedy
- Solved
- No attempts yet
Problem
Eungi has bottles, numbered 1 through , and drawers, numbered 1 through . The bottles are scattered across his bedroom floor, and he has decided to clean the room.
A drawer holds at most one bottle. So that he can find the bottle he wants quickly later, Eungi wrote down two drawer numbers and for every bottle. Those two are the only drawers bottle fits into.
Eungi puts the bottles away in order from bottle 1 to bottle , applying the following rules to each bottle from top to bottom.
- If drawer is empty, put bottle into it.
- If drawer is empty, put bottle into it.
- Move the bottle sitting in drawer into the other drawer that bottle fits into. If that drawer already holds a bottle, move that bottle into its other drawer, and continue the same way. If the chain of moves reaches an empty drawer, carry out every move and put bottle into drawer . If it never reaches an empty drawer, go on to the next rule.
- Try the same procedure starting from drawer . On success, put bottle into drawer . On failure, go on to the next rule.
- If rules 1 through 4 all fail, Eungi drinks bottle on the spot. (He never gets drunk at all.)
Write a program that determines, for each bottle, whether it is stored in a drawer or drunk on the spot.
Input
The first line contains and . ()
Each of the next lines contains and . (, )
Output
For bottle 1 through bottle , in order, print one line each: LADICA if the bottle is stored in a drawer, SMECE if it is drunk on the spot.
Hint
In the example, the first six bottles go into drawers 1, 3, 5, 7, 9, and 2 by rule 1.
The seventh bottle uses rule 3. The bottle in drawer 1 moves to drawer 2, the bottle in drawer 2 moves to drawer 3, and the bottle in drawer 3 moves to drawer 4.
The eighth bottle goes into drawer 8.
The ninth bottle uses rule 3 as well. The bottle in drawer 7 moves to drawer 8, the one in drawer 8 moves to drawer 2, the one in drawer 2 moves to drawer 1, the one in drawer 1 moves to drawer 5, and the one in drawer 5 moves to drawer 6.