Intermediate vertices X34137


Statement
 

pdf   zip

html

Given a directed graph and two different vertices u and v, compute how many vertices x different from u and v there are such that there exists some path from u to v passing through x.

Input

The input consists in several cases. Each case begins with n, u, v and m, followed by m different pairs x y, with xy, which indicate an arc that goes from x to y. Assume 2 ≤ n ≤ 104, 0 ≤ m ≤ 10n, and that the vertices are numbered between 0 and n − 1.

Output

For each case, write the number of vertices that can be visited when going from u to v following some path.

Hint

For each case, essentially the expected solution only makes two traversals, each on the right graph.

Public test cases
  • Input

    9 7 4 9
    8 7
    7 1
    7 2
    7 5
    1 3
    2 3
    3 4
    6 4
    4 0
    
    2 0 1 0
    
    3 0 1 2
    1 2
    2 0
    
    4 0 2 3
    0 2
    2 3
    3 0
    

    Output

    3
    0
    0
    1
    
  • Information
    Author
    Language
    English
    Translator
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++
    User solutions
    C++