码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-10-13 12:21:25      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:acm   区域赛   

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

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

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

            然后两颗子树分别求一遍直径即可,两颗子树的直径中点就是答案

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

标签:acm   区域赛   

原文地址:http://blog.csdn.net/cq_phqg/article/details/40032579

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!