5-Path
Time limit2sMemory limit512 MB
Given an edge list, find the shortest prefix that creates a simple path of exactly 5 edges between nodes a and b, or report -1.
- Level
Hard8 of 10
- Topics
- Graph, Binary search, BFS
- Solved
- No attempts yet
Problem
You are given a list of edges of an undirected graph. The graph has two special nodes, and . Find the minimum size of a prefix of this list such that the graph represented by this prefix contains a simple path of 5 edges between nodes and .
Input
The first line contains two integers and , the number of nodes and the number of edges in the graph. Each of the following lines contains two integers and , the endpoints of an edge (). The last line contains two integers and , the special nodes (, ). The graph has no multiple edges and no self-loops.
Output
If the graph represented by the given edge list contains a simple path of 5 edges, output the answer to the problem. Otherwise, output .