Jumps in pairs S97190


Statement
 

pdf   zip

Given an undirected graph and two vertices ss and tt, compute the minimum number of jumps needed to go from ss to tt. Here, we say that a jump between two vertices xx and zz is possible if there is a vertex yy adjacent to both xx and zz.

Input

Input consists of several graphs. Every case begins with nn, mm, ss and tt, followed by mm pairs xx yy, with xyx \ne y, indicating an edge between xx and yy. Suppose 2n1052 \le n \le 10^5, 0m5n0 \le m \le 5n, sts \ne t, that the vertices are numbered between 0 and n1n - 1, and that there are no repeated edges.

Output

For each graph, print the minimum number of jumps to go from ss to tt. If it is impossible, print “NO”.

Observation

Even if a green light is obtained, only Θ(n+m)\Theta(n+m) solutions will receive the maximum score.

Public test cases
  • Input

    3 2 1 2
    1 0  2 1
    
    4 4 3 2
    0 1  1 2  2 0  3 0
    
    2 1 0 1
    0 1
    
    5 6 0 1
    0 1  1 2  1 3  1 4  2 4  3 4
    

    Output

    NO
    1
    NO
    2
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Translator
    Salvador Roura
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++
    User solutions
    C++