The directed graph has n nodes and edges [a, b] (arrows from a to b). A node with no outgoing arrows is terminal. A node is safe if every path starting at it eventually reaches a terminal node (it cannot be trapped in a cycle). Return the safe nodes in increasing order.
For n = 7 and edges = [[0, 1], [0, 2], [1, 2], [1, 3], [2, 5], [3, 0], [4, 5]] the answer is [2, 4, 5, 6].
Example 1
Input:
eventual_safe_nodes(7, [[0, 1], [0, 2], [1, 2], [1, 3], [2, 5], [3, 0], [4, 5]])
Output:
[2, 4, 5, 6]
Example 2
Input:
eventual_safe_nodes(5, [[1, 2], [2, 3], [3, 4], [4, 1]])
Output:
[0]
0 <= n <= 1000000 <= len(edges) <= 200000No results yet
Run your code to check it against the examples, or submit to run every test.