5-Path

면접 대비

시간 제한2초메모리 제한512 MB

요약
무방향 간선 목록과 두 정점 a, b가 주어질 때, a와 b 사이에 정확히 5개의 간선을 가진 단순 경로가 포함되는 최소 접두사의 길이를 구하고, 없으면 -1을 출력한다.
난이도

보통10점 중 6점

유형
유니온 파인드, 그래프, DFS, 이분 탐색
정답자
아직 제출이 없습니다

문제

You are given a list of edges of an undirected graph. There are two special nodes in the graph: a and b. Find the minimum size of a prefix of this list such that a graph represented by this prefix includes a simple path of 5 edges between nodes a and b.

입력

The first line of input contains two integers n and m: the number of nodes and the number of edges in the graph, respectively.

Each of the following m lines contains two integers vi and ui which describe two endpoints of an edge (1 ≤ vi, ui ≤ n).

The last line contains two integers a and b: the numbers of special nodes (a 6= b, 1 ≤ a, b ≤ n).

The graph has no multiple edges and no self-loops.

출력

If there is a simple path of 5 edges in the graph represented by the given edge list, output the answer to the problem. Otherwise, output −1.

예제2

  1. 예제 1

    입력
    10 9
    1 6
    6 9
    9 10
    10 3
    3 2
    2 7
    7 5
    5 8
    8 4
    6 7
    
    예상 출력
    6
    
  2. 예제 2

    입력
    10 10
    1 2
    1 3
    1 4
    1 5
    1 6
    1 7
    1 8
    1 9
    10 2
    10 3
    1 10
    
    예상 출력
    -1