Under construction

Still being built. Things may change

Router Placement to Minimize Maximum TTL

Interview

Time limit1sMemory limit128 MB

Summary
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).
Level

Medium5 of 10

Topics
Tree, Graph, BFS, DFS
Solved
No attempts yet

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 cc (1≤c≤1001 \le c \le 100). For each test case, the first line contains the number of computers NN (1<N≤100,0001 < N \le 100{,}000). Computers are numbered from 00 to N−1N-1. Each of the next N−1N-1 lines contains two computer numbers aa and bb (0≤a,b<N0 \le a, b < N) that are directly connected; if aa is connected to bb then bb is connected to aa. 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.

Examples1

  1. Example 1

    Input
    3
    2
    1 0
    5
    3 2
    2 1
    0 2
    2 4
    9
    3 1
    6 5
    3 4
    0 3
    8 1
    1 7
    1 6
    2 3
    
    Expected output
    1
    1
    2