Intermediate vertices

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.

Problem information

Author: Salvador Roura

Generation: 2026-01-25T22:47:27.299Z

© Jutge.org, 2006–2026.
https://jutge.org