Hi. I am trying to solve this problem.
For convenience, I have summarized the problem statement below (based on my understanding):
Given a directed graph with N vertices and E edges (with cycles and not necessarily connected), find the minimum number of edges that we need to retain such that connectivity between vertices is retained as given in the original graph.
Input size: 1 ≤ N, E ≤ 2e5
Time limit: 1s
For example, for the following graph:
We should retain the edges:
0 -> 1
1 -> 2
1 -> 3
So we must use a minimum of 3 edges.
Note that:
0 -> 2
is redundant as we can use the path 0 -> 1 -> 2
to get from 0
to 2
.
0 -> 3
is redundant as we can use the path 0 -> 1 -> 3
to get from 0
to 3
. (Thanks filippos for catching this mistake!)
This problem seems to be asking for the transitive reduction of a graph which can only be found in at least O(n2) based on what I found on Google and this clearly wouldn't pass the time limit. Furthermore, the list of AC solutions suggests that there is a linear time solution to this problem.
Could someone please advise me on how to solve this problem?
UPD Managed to solve this problem (thank you please_delete_account for your incredible patience and explanation!).
I indeed had a wrong understanding of this problem and overly constrained it.
For readers who are looking for the solution to this problem, I have posted my solution below: