You are maintaining a project that has n methods numbered from 0 to n - 1.
You are given two integers n and k, and a 2D integer array invocations, where invocations[i] = [ai, bi] indicates that method ai invokes method bi.
There is a known bug in method k. Method k, along with any method invoked by it, either directly or indirectly, are considered suspicious and we aim to remove them.
A group of methods can only be removed if no method outside the group invokes any methods within it.
Return an array containing all the remaining methods after removing all the suspicious methods. You may return the answer in any order. If it is not possible to remove all the suspicious methods, none should be removed.
Example 1:
Input:n = 4, k = 1, invocations = [[1,2],[0,1],[3,2]]
Output:[0,1,2,3]
Explanation:
Method 2 and method 1 are suspicious, but they are directly invoked by methods 3 and 0, which are not suspicious. We return all elements without removing anything.
Example 2:
Input:n = 5, k = 0, invocations = [[1,2],[0,2],[0,1],[3,4]]
Output:[3,4]
Explanation:
Methods 0, 1, and 2 are suspicious and they are not directly invoked by any other method. We can remove them.
Example 3:
Input:n = 3, k = 2, invocations = [[1,2],[0,1],[2,0]]
Output:[]
Explanation:
All methods are suspicious. We can remove them.
Constraints:
1 <= n <= 105
0 <= k <= n - 1
0 <= invocations.length <= 2 * 105
invocations[i] == [ai, bi]
0 <= ai, bi <= n - 1
ai != bi
invocations[i] != invocations[j]
Solutions
Solution 1: Two DFS
Thinking
Methods reachable from \(k\) along call edges are suspicious, yet we must not remove a node that a clean method still invokes. The graph is large enough that only linear traversals are acceptable.
The first DFS marks the directed closure of \(k\). The second starts from every unmarked node and, along undirected edges, clears any node that a clean method can reach.
Only nodes that remain marked after both passes are removed; the rest form the answer.
We can start from \(k\) and find all suspicious methods, recording them in the array \(\textit{suspicious}\). Then, we traverse from \(0\) to \(n-1\), starting from all non-suspicious methods, and mark all reachable methods as non-suspicious. Finally, we return all non-suspicious methods.
The time complexity is \(O(n + m)\), and the space complexity is \(O(n + m)\), where \(n\) and \(m\) represent the number of methods and the number of call relationships, respectively.
Methods reachable from \(k\) along call edges are suspicious, yet we must not remove a node that a clean method still invokes. With \(n \le 10^5\), linear traversals are enough.
Recursing from \(k\) into each callee reaches depth \(n\) on a chain and overflows the call stack. Both the suspicious set and the later cleared set depend only on reachability, so neighbor order does not matter.
Both passes therefore use an explicit stack. The first marks the directed closure of \(k\), marking a node when it is pushed. The second starts from every still-unmarked node and, along undirected edges, clears any node a clean method can reach. Only nodes that remain marked are removed; the rest form the answer.
An explicit stack starts from \(k\) and follows directed call edges, recording suspicious methods in \(\textit{suspicious}\) and marking a node when it is pushed. We then scan from \(0\) to \(n - 1\). From each non-suspicious method, another explicit stack follows undirected edges and marks every reachable method as non-suspicious. Finally, we return all non-suspicious methods.
The time complexity is \(O(n + m)\), and the space complexity is \(O(n + m)\), where \(n\) and \(m\) represent the number of methods and the number of call relationships, respectively.