I'd like to ask about the following problem:
Given a directed graph (not necessarily a DAG), for every vertex v calculate the number of reachable vertices from v.
So using brute force approach (n times DFS) we can get the answer in O(n^2) time complexity. Is there a way to calculate it faster? I can definitely make a DAG from the given graph using SCC. I tried to use previously calculated values, so I could visit each vertex only once, but it does not work at all. The biggest problem lies in a graph like this:
2 -> 1
3 -> 2
3 -> 1
1 -> 4
I run a DFS from vertex 1 and return result 1. Then, using it I can immidiately calculate answer for 2 (I do not enter vertex 1 again in second DFS, I use its answer instead), what is 2. Then I get to the vertex 3, and... the algorithm would sum up the result of 1 and 2, since I can reach both of those vertices. But vertex 1 is already calculated in the result for vertex 2. In that way I get the answer equal to 4, which is not true.