Hide and Seek

Interview

Time limit1sMemory limit256 MB

Summary
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 NN barns in total, numbered from 11 to NN (2≤N≤20,0002 \le N \le 20{,}000).

Jaeseo knows that Suhyeon always starts searching from barn 11. All barns are connected by MM bidirectional paths (1≤M≤50,0001 \le M \le 50{,}000), and each path joins two distinct barns AiA_i and BiB_i (1≤Ai≤N1 \le A_i \le N, 1≤Bi≤N1 \le B_i \le N, Ai≠BiA_i \ne B_i). 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 11, where distance is the minimum number of paths that must be traversed between two barns. In other words, the barn farthest from barn 11 is the best hiding spot. Help Jaeseo find the barn to hide in!

Input

The first line contains the number of barns NN and the number of paths MM, separated by a space.

Each of the next MM lines contains two barn numbers AiA_i and BiB_i, 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 11 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 44, 55, and 66 are all at distance 22 from barn 11. Barn 44 is chosen among them because it has the smallest number.

Examples3

  1. Example 1

    Input
    6 7
    3 6
    4 3
    3 2
    1 3
    1 2
    2 4
    5 2
    
    Expected output
    4 2 3
    
  2. Example 2

    Input
    2 1
    1 2
    
    Expected output
    2 1 1
    
  3. Example 3

    Input
    5 4
    1 2
    2 3
    3 4
    4 5
    
    Expected output
    5 4 1