A directed graph is given as an adjacency list graph where graph[i] is the list of nodes reachable from i in one step (0 <= i < n). A node is a terminal if it has no outgoing edges. A node is safe if every possible walk starting from it eventually stops at a terminal (it can never get stuck in a cycle).
Return the sorted list of all safe nodes.
Examples
Input: graph = [[1], [2, 3], [1], [], [3, 5], []]
Output: [3, 4, 5]
Explanation: 1 and 2 form a cycle (1 -> 2 -> 1), and 0 can walk into it; node 1 is unsafe even though one walk from it ends at 3. Every walk from 4 ends at 3 or 5.
Input: graph = [[1], [0]]
Output: []
Constraints
1 <= n <= 10**4, total edges<= 3 * 10**4- Target: O(V + E) time.
Goals
- Reverse the edges so that out-degree peeling becomes in-degree peeling
- Classify each node by whether every walk from it must terminate