#P13855. [CERC 2023] Keys
[CERC 2023] Keys
题目描述
Alice and Bob live in a massive mansion with rooms (one of them denoting outdoors, where they play the Moon game) and doors between them. Each door connects two rooms or a room with the outdoors and has a single unique key that opens this door only. Every door closes behind you and locks automatically, so one always needs a key to pass through. The building is very large, but Alice and Bob only use one room – their bedroom. Other rooms are only there to make the house look bigger and make the neighbors jealous.
This unusual way of building their house is now causing some trouble for Alice and Bob. Bob is leaving for a two-week-long trip. In a week, Alice is also going abroad for a month and when she leaves, she needs the right keys to leave the house. However, Bob also needs keys to get back in since Alice will not be there at the time to let him in. Alice and Bob are now trying to figure out how to split the keys to the doors for Alice to be able to get from room (their bedroom) to (outdoors) and Bob to go from room (outdoors) to (bedroom) one week later.
Fortunately, Alice remembered that she could drop some of the keys on her way out for Bob to pick up on his way back. This way, they can both pass through the same doors. She, of course, cannot drop any keys in room (outdoors) since the neighbors could find them and break into their house.
Can you help Alice and Bob split their keys and plan their trip through the house?
Task
You are given the description of Alice and Bob’s mansion: doors between rooms numbered to , where is outdoors and is the bedroom. The -th door can be opened with the key number (0-indexed).
You should first print two lines containing space-separated numbers of keys for Alice and Bob, respectively. It is fine if they do not use all the keys, but it is not allowed for them to both have a copy of the same key (or for one of them to have multiple copies of a key).
You should then print instructions that Alice and Bob will follow. First, print Alice’s movements from room to by printing commands of one of the two types:
- "MOVE " to move to room (assuming that there is a door between Alice's current location and and that Alice has the key),
- "DROP …" to drop keys (printed as space-separated integers) in the room where Alice currently stands. This means that Alice no longer carries these keys.
Once Alice is done with her movements, print “DONE” in a new line. She should finish her movement in room . It is fine if Alice passes through room or multiple times while following the printed instructions.
Second, print Bob’s movements from room to by printing commands of one of the two types:
- “MOVE ” to move to room (assuming that there is a door between Bob’s current location and and that Bob has the key),
- “GRAB” to grab any keys in the room where Bob is currently standing. Bob always grabs all the keys that Alice left in the room. If Alice left none, he does not grab any.
Once Bob is done with his movements, print “DONE” in a new line. He should finish his movement in room . It is fine if Bob passes through room or multiple times while following the printed instructions.
Remarks: It is considered acceptable, albeit useless, to DROP an empty list of keys or to grab keys in a room that has no keys, or grab keys in room (i.e. outside).
输入格式
The first line contains integers and , the number of rooms (outdoors included) and number of doors. This is followed by lines that describe the doors. The -th line (count starting from ) is described by integers , , indicating that there is a door between rooms and , opened with key .
输出格式
First, print two lines, describing the split of keys. Then, print all the instructions for Alice and Bob, as described in the task description, one per line. If there is no solution, print "No solution" (without the quotes). If there are multiple valid solutions, any of them is accepted.
5 5
0 1
1 2
2 3
3 4
4 1
0 1 2
3 4
MOVE 1
MOVE 2
MOVE 3
DROP 0
MOVE 2
MOVE 1
DONE
MOVE 4
MOVE 3
GRAB
MOVE 4
MOVE 1
MOVE 0
DONE
3 2
0 2
1 2
No solution
提示
Comment
The first example represents the following floor plan, where blue numbers correspond to key IDs required for each door:
:::align{center}
:::
Alice takes keys , , and while Bob takes keys and . Alice walks from to , then to , and then to . There, she drops key . She retraces her way back to . Bob starts in , walks to , then to , where he picks up key . He retraces his way back to , and with the newly picked-up key , opens up the door to .
In the second example, there is no way for both Alice and Bob to reach their destination. Note that Alice cannot drop keys in room .
Input and output limits
- It is guaranteed that it’s always possible to reach any room from any other room if you have all the keys.
- Each pair of rooms is connected with at most one door.
- No room is connected to itself.
- Your program may print at most instructions.