Hide and Seek
InterviewTime limit1sMemory limit256 MB
In a connected undirected graph, find the barn farthest from barn 1. Print the smallest such barn number, its distance, and how many barns tie at that distance.
- Level
Medium4 of 10
- Topics
- Graph, BFS, Queue, Shortest path
- Solved
- No attempts yet
Problem
Jaeseo is playing hide-and-seek with Suhyeon on a country farm. The farm has many barns, and Jaeseo must hide in one of them. There are barns in total, numbered from to ().
Jaeseo knows that Suhyeon always starts searching from barn . All barns are connected by bidirectional paths (), and each path joins two distinct barns and (, , ). From any barn, every other barn is always reachable.
Because Jaeseo's feet smell terrible, he wants to hide where the smell is least noticeable. The smell fades the farther a barn is from barn , where distance is the minimum number of paths that must be traversed between two barns. In other words, the barn farthest from barn is the best hiding spot. Help Jaeseo find the barn to hide in!
Input
The first line contains the number of barns and the number of paths , separated by a space.
Each of the next lines contains two barn numbers and , separated by a space, describing one path.
Output
Print three values on a single line, separated by spaces.
- First value: the number of the barn to hide in (the barn whose distance from barn is the largest; if several barns share this largest distance, print the smallest such barn number).
- Second value: the distance to that barn.
- Third value: the number of barns that share this maximum distance.
Hint
A bird's-eye view of the farm is shown below.
1--2--5
| /|
|/ |
3--4
|
6
Barns , , and are all at distance from barn . Barn is chosen among them because it has the smallest number.