Mousetrap
Time limit5sMemory limit512 MB
On a tree, Dumbo blocks or cleans edges while an edge-averse mouse moves; find the minimum moves to force it into the trap.
- Level
Hard8 of 10
- Topics
- Tree, DFS, Game theory, Greedy
- Solved
- No attempts yet
Problem
Dumbo the elephant has a huge labyrinth with rooms numbered through and passages, arranged so that every room is reachable from every other room. A mouse has sneaked into the labyrinth. Dumbo is terribly afraid of mice, so he sets a mousetrap in room . The mouse avoids the room with the trap, so Dumbo needs a better plan to bait the mouse into it.
The mouse runs around constantly and never stops, unless it has nowhere to move. It leaves a dirty trail of droppings and footprints in every passage it uses, and it refuses to use a dirty passage again. Dumbo can clean a dirty passage, or block a passage with stones. By blocking passages and cleaning them, he wants to force the mouse into the trap. He would like to do this in as few moves as possible, because he feels highly uncomfortable near a mouse.
Describe this as a game for two players. The mouse tries to maximize the number of Dumbo's moves, and Dumbo tries to win in the smallest number of moves. Dumbo goes first. On his turn he may clean one dirty passage of the labyrinth or block one passage. It does not matter whether the blocked passage is clean or dirty. He cannot unblock a passage. He may also choose to do nothing, and a turn in which he does nothing does not count as a move. When it is the mouse's turn, the mouse chooses a clean unblocked passage leading out of its current room and runs to the room on the other side. If no such passage exists, the mouse does not move.
At the start every passage is clean, the mouse is in room , the trap is in room , and it is Dumbo's turn. Both sides play optimally. Find the smallest number of moves, counting passages cleaned and passages blocked, that Dumbo needs.
Input
The first line contains the integers , , and , separated by spaces. Each of the next lines contains two integers and , separated by a space, meaning that a passage joins room and room .
Note that the input size is large.
Output
Print the minimum number of Dumbo's moves on one line. If the mouse starts in the room with the trap, that is , print .
Constraints
- ,
Hint
One scenario that finishes the example in four moves:
- Dumbo blocks the passage between rooms and .
- The mouse moves to room . The passage between rooms and is now dirty.
- Dumbo blocks the passage between rooms and .
- The mouse cannot move.
- Dumbo cleans the passage between rooms and .
- The mouse moves to room . The passage between rooms and is dirty again.
- Dumbo blocks the passage between rooms and .
- The mouse moves to room . The passage between rooms and is now dirty.
- Dumbo does nothing.
- The mouse can only move to room and gets caught in the trap.
Dumbo made four moves.