Gnomes and the Lonely Mountain
Time limit2sMemory limit512 MB
Two gnome groups start at two nodes of a tree and walk to unvisited nodes without ever colliding; find the longest number of minutes the search can last.
- Level
Medium7 of 10
- Topics
- Tree, Dynamic programming, DFS
- Solved
- No attempts yet
Problem
Gnomes keep searching for their ancestors' gold in the depths of the Lonely Mountain. The caves there number , and some of them are joined by two-way passages. From every cave, any other cave can be reached through the passages, and there is exactly one way to do so.
Two gnome groups start their search from caves and . Each group moves together. A group spends exactly one minute examining its cave, then quickly moves through a passage to one of the neighboring caves. A group never enters a cave that it or the other group has already visited. The two groups never enter the same cave. If at least one group cannot move under these rules, both groups stop searching immediately.
The gnomes want the search to last as long as possible. Given the map of the caves and the starting positions of the groups, find the maximum number of minutes the search can continue.
Input
The first line contains (), the number of caves. Each of the next lines contains two cave numbers and connected by a passage (). The last line contains the starting caves and of the two groups (, ).
Output
Print the maximum number of minutes the search can continue.