Jumps in pairs S97190


Statement
 

pdf   zip

thehtml

Given an undirected graph and two vertices s and t, compute the minimum number of jumps needed to go from s to t. Here, we say that a jump between two vertices x and z is possible if there is a vertex y adjacent to both x and z.

Input

Input consists of several graphs. Every case begins with n, m, s and t, followed by m pairs x ‍y, with xy, indicating an edge between x and y. Suppose 2 ≤ n ≤ 105, 0 ≤ m ≤ 5n, st, that the vertices are numbered between 0 and n − 1, and that there are no repeated edges.

Output

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

Observation

Even if a green light is obtained, only Θ(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++