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.
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).
two buildings selected to build the fire stations.
If there are multiple solutions, any one will be acceptable.
题意:给了一棵树,要求选两个点作为消防队,使得其它点到这两个消防队的最大距离尽量小,每个点只会选择离它近的消防队
思路:首先求树的直径,然后在从直径的中点将树分成两颗子树
然后两颗子树分别求一遍直径即可,两颗子树的直径中点就是答案
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 thetwo 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
题意:给了一棵树,要求选两个点作为消防队,使得其它点到这两个消防队的最大距离尽量小,每个点只会选择离它近的消防队
思路:首先求树的直径,然后在从直径的中点将树分成两颗子树
然后两颗子树分别求一遍直径即可,两颗子树的直径中点就是答案
相关文章推荐
- [zoj 3822]2014牡丹江区域赛 Domination 概率dp求期望
- ZOJ 3826 Hierarchical Notation(2014 牡丹江 B,树的直径+二分)
- zoj 3822 Domination(2014牡丹江区域赛D题)
- ZOJ 3820 2014ACM/ICPC牡丹江赛区B题
- zoj 3827 Information Entropy(2014牡丹江区域赛I题)
- ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)
- zoj 3829 Known Notation(2014在牡丹江区域赛k称号)
- ZOJ 3820 2014ACM/ICPC牡丹江司B称号
- zoj 3829 (2014牡丹江区域赛K) Known Notation
- zoj 3822 Domination(2014牡丹江区域赛D题) (概率dp)
- ZOJ 3827 Information Entropy (2014牡丹江区域赛)
- zoj 3820 Building Fire Stations The 2014 ACM-ICPC Asia Mudanjiang Regional Contest B题 树的直径
- zoj 3822 Domination(2014牡丹江区域赛D称号)
- ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)
- ZOJ 3829 Known Notation / 2014牡丹江区域赛 K (模拟, 贪心)
- ZOJ 3820 Building Fire Stations (二分+贪心) 2014 牡丹江现场赛B
- zoj 3822 (2014 牡丹江区域赛 D) Domination
- zoj 3820(2014牡丹江现场赛B题)
- poj 3820 牡丹江区域赛 (树的直径+bfs)
- zoj 3809 水 2014 ACM牡丹江区域赛网赛