Question
I have a question about this problem:
Given an directed graph that contains N vertices and M edges, please determine that "there is a path from vertex i to vertex j for all 1 <= i, j <= N
".
I want to solve for N <= 500, M <= 250000
.
I found the naive pathfinding algorithm with dfs but the time complexity is O(N^2 M)
, so it is very slow.
Please tell me the efficient algorithm to solve it.
Example
For example, if this graph is given:
The answer is NO because there isn't a path from 4 to 1.