Getting Back Home
Time limit2sMemory limit512 MB
On a tree with home at node 1 and office at node n, find the smallest flashlight range d so that a random walk governed by visibility rules always reaches home within 10^9 steps.
Problem
Arman recently moved to a remote village in the countryside. The map of the village is a tree: exactly roads connect intersections, and every pair of intersections is joined by a sequence of roads.
Every morning Arman goes to his office, and late at night he gets back home. Nights are very dark and the village roads have no lights, so Arman has started to have trouble finding his way back home. The intersections carry no signs and he cannot tell them apart. His phone has no signal on the way either, so GPS is not possible. To solve the problem he decided to buy a flashlight. Flashlights have different integer beam distances, and a flashlight with a longer beam costs more. A flashlight with range reveals every intersection within distance at most of the current intersection. All roads of the village have the same length 1.
When Arman leaves the office towards home, he makes a decision at every intersection of the path he traverses.
- If he sees home, he just moves towards it.
- Assume he is at intersection . Let be the set of all roads incident to . Let be the empty set at the initial time, and otherwise, where is the road he just arrived at through. Let be the set of useless roads incident to . A road incident to is called useless if all simple paths starting at and passing through have length less than . If is not empty, one road of this set is chosen randomly. Otherwise the road is chosen again.
Arman does not mind walking a little bit longer as long as he knows that, regardless of his random choices, he will eventually reach home after passing through at most roads. He wants to purchase the cheapest flashlight for that purpose. Find the smallest beam range by which he is able to reach home.
Input
There are multiple test cases in the input. The first line of each test case contains an integer , the number of intersections in the village (). Each of the next lines contains two integers and , meaning that there is a road between intersections and (). Arman's home is at intersection 1 and his office is at intersection . The input terminates with a line containing 0, which should not be processed.
Output
For each test case, output a single line containing the minimum range of a flashlight that guarantees Arman can always reach home after passing through at most roads, regardless of his random choices. If Arman does not need any flashlight, print 0.