eolymp
bolt
Try our new interface for solving problems
Problems

Lonely Island

Lonely Island

There are many islands that are connected by one-way bridges, that is, if a bridge connects islands $a$ and $b$, then you can only use the bridge to go from $a$ to $b$ but you cannot travel back by using the same. If you are on island $a$, then you select (uniformly and randomly) one of the islands that are directly reachable from $a$ through the one-way bridge and move to that island. You are stuck on an island if you cannot move any further. It is guaranteed that after leaving any island it is not possible to come back to that island. Find the island that you are most likely to get stuck on. Two islands are considered equally likely if the absolute difference of the probabilities of ending up on them is ≤ $10^{-9}$. \InputFile The first line contains three integers: the number of islands $n~(1 \le n \le 200000)$, the number of one-way bridges $m~(1 \le m \le 500000)$ and the index $r~(1 \le r \le n)$ of the island you are initially on. Each of the next $m$ lines contains two integers $u_i$ and $v_i~(1 \le u_i, v_i \le n)$ representing a one-way bridge from island $u_i$ to $v_i$. \OutputFile Print the index of the island that you are most likely to get stuck on. If there are multiple islands,then print them in the increasing order of indices (space separated values in a single line).
Time limit 2 seconds
Memory limit 128 MiB
Input example #1
5 7 1
1 2
1 3
1 4
1 5
2 4
2 5
3 4
Output example #1
4