Farthest vertex

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 n and the number of edges m, followed by m pairs x y that
correspond to and edge between vertices x and y. It holds that
1 ≤ n ≤ 10⁴, 0 ≤ m ≤ 5n, vertices are numbered from 0 to n − 1, and
there are neither repeated edges nor edges of the form x x.

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.

Problem information

Author: Salvador Roura

Generation: 2026-01-25T22:42:05.603Z

© Jutge.org, 2006–2026.
https://jutge.org
