Reachability from the Capital
题目描述
There are cities and roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way.
What is the minimum number of new roads that need to be built to make all the cities reachable from the capital?
New roads will also be one-way.
输入格式
The first line of input consists of three integers , and ( ) — the number of cities, the number of roads and the index of the capital. Cities are indexed from to .
The following lines contain roads: road is given as a pair of cities , ( , ). For each pair of cities , there can be at most one road from to . Roads in opposite directions between a pair of cities are allowed (i.e. from to and from to ).
输出格式
Print one integer — the minimum number of extra roads needed to make all the cities reachable from city . If all the cities are already reachable from , print 0.
输入输出样例
输入样例 #1
9 9 1
1 2
1 3
2 3
1 5
5 6
6 1
1 8
9 8
7 1
输出样例 #1
3
输入样例 #2
5 4 5
1 2
2 3
3 4
4 1
输出样例 #2
1
说明/提示
The first example is illustrated by the following:
For example, you can add roads ( ), ( ), ( ) to make all the cities reachable from .
The second example is illustrated by the following:
In this example, you can add any one of the roads ( ), ( ), ( ), ( ) to make all the cities reachable from .