Dog Snacks
Time limit3sMemory limit512 MB
Given a tree, find the smallest k so Badugi can eat every snack by always going to the nearest one within k meters, then return to intersection 1.
- Level
Medium7 of 10
- Topics
- Tree, Binary search, DFS
- Solved
- No attempts yet
Problem
Gildong is playing with his dog, Badugi. They are at a park that has intersections and bidirectional roads. Each road is meter long and connects two intersections. The intersections are numbered from to , and for every and (), Badugi can get from intersection to intersection using some set of roads.
Gildong has put one snack at every intersection of the park. He gives Badugi a mission to eat all of the snacks. Badugi starts at intersection and moves by the following rules:
- Badugi looks for the snacks closest to him. Distance is the length of the shortest path from his current location to the intersection with the snack. His sense of smell reaches only meters, so he can find only snacks at distance or less. If he finds no such snack, he fails the mission.
- Among the snacks he can smell from his current location, he picks the one that minimizes the distance he must travel. If several snacks tie, he picks any one of them.
- He repeats this until he has eaten all snacks. Then he must find intersection again, and that intersection must be at distance or less from the last snack he ate. If he finds it, he completes the mission. Otherwise, he fails.
Gildong does not know . Find the minimum value of that lets Badugi complete the mission when he moves optimally.
Input
The first line contains the number of test cases ().
The first line of each test case contains one integer (), the number of intersections.
Each of the next lines contains two integers and (, ), meaning there is a road between intersections and . All roads are bidirectional and distinct.
It is guaranteed that for each test case, for every and (), it is possible to get from intersection to intersection using roads. The sum of over all test cases does not exceed .
Output
For each test case, print one integer: the minimum value of such that Badugi can complete the mission.
Hint
In the first case, Badugi can complete the mission with by moving as follows:
- Badugi starts at intersection . The closest snack is at intersection , so he eats it.
- The next closest snacks are at intersection and intersection . Suppose he picks intersection . He moves meter to intersection and eats the snack.
- The only remaining snack is at intersection . He moves along roads to get there.
- After eating it, he returns to intersection , which is meter away. He completes the mission.
In the second case, the only possible sequence of moves is . The distance from intersection to intersection is , so must be at least .
In the third case, Badugi can move as . It can be shown that this is the only sequence that completes the mission with .
