Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/graf-yetib-boriladigan

Reachable vertices

Easy 1000 ms 128 MB 28%
Share

A graph with $n$ vertices and $m$ directed edges is given: an edge $u \to v$ can be traversed only from $u$ to $v$. How many vertices can be reached along the edges from vertex $s$? $s$ itself also counts.

A directed graph: the vertices reachable from s along the arrows are highlighted

Input

The first line contains three integers: $n$, $m$ and $s$. Each of the next $m$ lines contains two positive integers: $u$ and $v$ — an edge $u \to v$.

Output

One integer — the number of reachable vertices.

Note

$1 \le n \le 10^{5}$
$0 \le m \le 2 * 10^{5}$
$1 \le s, u, v \le n$
There may be multiple edges and loops.

Examples

Input #1
5 5 1
1 2
2 3
3 1
4 3
2 5
Output #1
4
Input #2
4 2 3
1 3
2 3
Output #2
1

Sign in to submit a solution

Signing up is free and takes a minute.