Farthest vertex X83283


Statement
 

pdf   zip

Given an undirected graph, compute the vertex which is farthest from vertex 0.

Input

Input consists of several graphs. Each graph starts with the number of vertices nn and the number of edges mm, followed by mm pairs xx yy that correspond to and edge between vertices xx and yy. It holds that 1n1041 \le n \le 10^4, 0m5n0 \le m \le 5n, vertices are numbered from 00 to n1n-1, and there are neither repeated edges nor edges of the form xx xx.

Output

For each graph, write the vertex which is farthest from vertex 0. In case of a tie, choose the smallest vertex. Ignore vertices that are not reachable from 0.

Public test cases
  • Input

    3 2  0 2  0 1
    1 0
    7 6  0 1  4 2  6 3  2 1  2 5  4 0
    

    Output

    1
    0
    5
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Translator
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++
    User solutions
    C++