#K6776. Furthest Lake from the Main River
Furthest Lake from the Main River
Furthest Lake from the Main River
You are given a series of lakes connected by directed waterways which represent the falls between them. The lake numbered 1 is the beginning of the main river. For each subsequent lake, you can only travel from a lake to another if there exists a direct waterfall (edge) connecting them. Your task is to determine the lake that is the furthest from the main river in terms of the number of waterfalls crossed. In the event that more than one lake is the furthest (i.e. at the same maximum distance), output the lake with the smallest index.
The distance is defined as the number of edges (waterfalls) you must pass through from lake 1 to get to the lake. Mathematically, if d(u) denotes the distance from lake 1 to lake u, you are to find a lake k such that
[ d(k) = \max_{1 \leq u \leq n} d(u), ]
and if there is more than one such lake, choose the one with the smallest k.
Note: The input graph is guaranteed to have exactly m directed edges.
inputFormat
The first line contains two integers n and m, where n is the number of lakes and m is the number of waterfalls (edges). Each of the following m lines contains two integers u and v, indicating a directed edge from lake u to lake v. It is guaranteed that lake 1 is the starting point of the river.
outputFormat
Output a single integer denoting the lake that is the furthest from lake 1 in terms of the number of waterfalls. If multiple lakes are at the same maximum distance, output the one with the smallest index.## sample
6 5
1 2
1 3
2 4
3 5
5 6
6