Router Placement to Minimize Maximum TTL
InterviewTime limit1sMemory limit128 MB
Given a tree, pick the vertex that minimizes the largest distance to any other vertex, and output that minimum maximum distance (the tree's radius).
Problem
Most computer networks form a tree: between any two computers there is always exactly one path.
When a network packet fails to reach its destination, it is discarded after a certain amount of time. The time a packet is allowed to live is called its TTL (Time To Live). Without a TTL, a packet could circle the network forever and corrupt the routing tables.
If one computer is chosen as the router, the cost of that choice is the largest TTL needed to reach any other computer, where the TTL to a computer equals the number of edges on the path from the router to it. We want to choose the router so that this maximum TTL is as small as possible.
Given the network, write a program that determines which computer, used as the router, minimizes the maximum TTL, and outputs that minimized maximum TTL.
Input
The first line contains the number of test cases (). For each test case, the first line contains the number of computers (). Computers are numbered from to . Each of the next lines contains two computer numbers and () that are directly connected; if is connected to then is connected to . Each network is always a tree.
Output
For each test case, output on its own line the minimized maximum TTL, i.e. the maximum TTL obtained when the router is placed on the best possible computer.