Intermediate vertices X34137


Statement
 

pdf   zip

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

Input

The input consists in several cases. Each case begins with nn, uu, vv and mm, followed by mm different pairs xx yy, with xyx \neq y, which indicate an arc that goes from xx to yy. Assume 2n1042 \leq n \leq 10^4, 0m10n0 \leq m \leq 10n, and that the vertices are numbered between 00 and n1n - 1.

Output

For each case, write the number of vertices that can be visited when going from uu to vv 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++