Ratatöskr
Time limit2sMemory limit512 MB
Two ravens hunt a squirrel on a tree, moving one raven per signal while the squirrel slips along branches. Find the fewest signals needed to capture it.
- Level
Medium7 of 10
- Topics
- Game theory, Graph, BFS
- Solved
- No attempts yet
Problem
Ratatöskr is a squirrel that lives in a giant (but finite) mythical tree called Yggdrasil. He likes to gossip, which sets the other inhabitants of the tree against each other. Ratatöskr is therefore hunted by the two ravens of Odin, Hugin and Munin, who want to bring him to justice.
The tree is made up of nodes connected by branches. Initially, the ravens and the squirrel sit on three different nodes. Then the following happens repeatedly:
- On Odin's signal, one of the ravens launches into the air and flies to another node of the tree (or possibly back to its previous position), while the other stays where it is.
- During this maneuver, Ratatöskr can travel along the branches to reach another node, but may not pass through a node where a raven sits. He is much quicker than the ravens and will reach his destination before the flying raven lands.
Ratatöskr gets captured if one of the ravens flies to his position and there is no other node he can escape to.
Help Odin determine an optimal strategy for capture, that is, the minimum number of signals he has to give until Ratatöskr is guaranteed to be captured by a raven. If Ratatöskr can escape them indefinitely, output impossible.
Input
The input consists of:
- one line with a single integer (), the number of nodes in the tree. The nodes are labeled .
- one line with a single integer (), the initial position of the squirrel Ratatöskr.
- one line with a single integer (), the initial position of the raven Hugin.
- one line with a single integer (), the initial position of the raven Munin.
- lines each containing two integers and (), indicating a branch between nodes and .
The positions , and are distinct. There is at most one branch between any two nodes, and every node is reachable from every other node by a sequence of branches.
Output
One line containing an integer , the number of signals that the ravens need to capture Ratatöskr in an optimal strategy. If Ratatöskr can escape them indefinitely, output impossible.