#C884. Shortest Path in River Network

    ID: 52866 Type: Default 1000ms 256MiB

Shortest Path in River Network

Shortest Path in River Network

In this problem, you are given an undirected graph that represents a network of rivers and canals. The nodes represent junction points, and an edge represents a segment (river or canal) connecting two junctions. Your task is to determine the minimum number of segments needed to travel from a starting point (S) to a target point (T). If no such path exists, output "NO PATH".

More formally, given a graph with (N) nodes and (E) edges, and a list of unordered pairs ((U,V)) representing the segments, find the smallest integer (d) such that there exists a sequence of nodes (S = v_0, v_1, \ldots, v_d = T) where each consecutive pair ((v_i, v_{i+1})) is an edge in the graph. If no such sequence exists, return "NO PATH".

inputFormat

The input is provided via standard input (stdin) and has the following format:

  • The first line contains two integers (N) and (E), where (N) is the number of nodes and (E) is the number of edges.
  • The next (E) lines each contain two integers (U) and (V), indicating that there is an edge connecting nodes (U) and (V).
  • The last line contains two integers (S) and (T), representing the starting and target nodes respectively.

outputFormat

Output a single line to standard output (stdout):

  • If there exists a path from (S) to (T), output the minimum number of segments (edges) required.
  • If there is no such path, output the string "NO PATH".## sample
5 6
1 2
2 3
3 4
4 5
1 3
3 5
1 5
2