您的位置:首页 > 其它

2014牡丹江区域赛B(树的直径)ZOJ3820

2014-10-13 01:10 316 查看
Building Fire Stations

Time Limit: 5 Seconds Memory Limit: 131072 KB Special Judge

Marjar University is a beautiful and peaceful place. There are N buildings and N - 1 bidirectional roads in the campus. These buildings are connected by roads in
such a way that there is exactly one path between any two buildings. By coincidence, the length of each road is 1 unit.
To ensure the campus security, Edward, the headmaster of Marjar University, plans to setup two fire stations in two different buildings so that firefighters are able to arrive at the
scene of the fire as soon as possible whenever fires occur. That means the longest distance between a building and its nearest fire station should be as short as possible.
As a clever and diligent student in Marjar University, you are asked to write a program to complete the plan. Please find out two proper buildings to setup the fire stations.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains an integer N (2 <= N <= 200000).
For the next N - 1 lines, each line contains two integers Xi and Yi. That means there is a road connecting building Xi and
building Yi (indexes are 1-based).

Output

For each test case, output three integers. The first one is the minimal longest distance between a building and its nearest fire station. The next two integers are the indexes of the
two buildings selected to build the fire stations.
If there are multiple solutions, any one will be acceptable.

Sample Input

2
4
1 2
1 3
1 4
5
1 2
2 3
3 4
4 5

Sample Output

1 1 2
1 2 4


题意:给了一棵树,要求选两个点作为消防队,使得其它点到这两个消防队的最大距离尽量小,每个点只会选择离它近的消防队

思路:首先求树的直径,然后在从直径的中点将树分成两颗子树

然后两颗子树分别求一遍直径即可,两颗子树的直径中点就是答案
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: