There is a directed graph of n
nodes with each node labeled from 0
to n - 1
. The graph is represented by a 0-indexed 2D integer array graph
where graph[i]
is an integer array of nodes adjacent to node i
, meaning there is an edge from node i
to each node in graph[i]
.
A node is a terminal node if there are no outgoing edges. A node is a safe node if every possible path starting from that node leads to a terminal node.
Return an array containing all the safe nodes of the graph. The answer should be sorted in ascending order.
Example 1:
Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]] Output: [2,4,5,6] Explanation: The given graph is shown above. Nodes 5 and 6 are terminal nodes as there are no outgoing edges from either of them. Every path starting at nodes 2, 4, 5, and 6 all lead to either node 5 or 6.
Example 2:
Input: graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]] Output: [4] Explanation: Only node 4 is a terminal node, and every path starting at node 4 leads to node 4.
Constraints:
n == graph.length
1 <= n <= 104
0 <= graph[i].length <= n
0 <= graph[i][j] <= n - 1
graph[i]
is sorted in a strictly increasing order.- The graph may contain self-loops.
- The number of edges in the graph will be in the range
[1, 4 * 104]
.
The point with zero out-degree is safe, and if a point can only reach the safe point, then it is also safe, so the problem can be converted to topological sorting.
class Solution:
def eventualSafeNodes(self, graph: List[List[int]]) -> List[int]:
n = len(graph)
outDegree = [len(vs) for vs in graph]
revGraph = defaultdict(list)
for u, vs in enumerate(graph):
for v in vs:
revGraph[v].append(u)
q = deque([i for i, d in enumerate(outDegree) if d == 0])
while q:
for u in revGraph[q.popleft()]:
outDegree[u] -= 1
if outDegree[u] == 0:
q.append(u)
return [i for i, d in enumerate(outDegree) if d == 0]
class Solution {
public List<Integer> eventualSafeNodes(int[][] graph) {
int n = graph.length;
int[] outDegrees = new int[n];
Queue<Integer> queue = new ArrayDeque<>();
List<List<Integer>> revGraph = new ArrayList<>();
for (int i = 0; i < n; i++) {
revGraph.add(new ArrayList<>());
}
for (int u = 0; u < n; u++) {
for (int v : graph[u]) {
revGraph.get(v).add(u);
}
outDegrees[u] = graph[u].length;
if (outDegrees[u] == 0) {
queue.offer(u);
}
}
while (!queue.isEmpty()) {
int v = queue.poll();
for (int u : revGraph.get(v)) {
if (--outDegrees[u] == 0) {
queue.offer(u);
}
}
}
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (outDegrees[i] == 0) {
ans.add(i);
}
}
return ans;
}
}
func eventualSafeNodes(graph [][]int) []int {
n := len(graph)
outDegree := make([]int, n)
revGraph := make([][]int, n)
queue := make([]int, 0)
ans := make([]int, 0)
for u, vs := range graph {
for _, v := range vs {
revGraph[v] = append(revGraph[v], u)
}
outDegree[u] = len(vs)
if outDegree[u] == 0 {
queue = append(queue, u)
}
}
for len(queue) > 0 {
v := queue[0]
queue = queue[1:]
for _, u := range revGraph[v] {
outDegree[u]--
if outDegree[u] == 0 {
queue = append(queue, u)
}
}
}
for i, d := range outDegree {
if d == 0 {
ans = append(ans, i)
}
}
return ans
}
class Solution {
public:
vector<int> eventualSafeNodes(vector<vector<int>> &graph) {
int n = graph.size();
vector<vector<int>> revGraph(n);
vector<int> outDegree(n);
for (int i = 0; i < n; ++i)
{
outDegree[i] += graph[i].size();
for (int j : graph[i])
revGraph[j].push_back(i);
}
queue<int> q;
for (int i = 0; i < n; ++i)
if (outDegree[i] == 0)
q.push(i);
while (!q.empty())
{
int i = q.front();
q.pop();
for (int j : revGraph[i])
{
if (--outDegree[j] == 0)
q.push(j);
}
}
vector<int> ans;
for (int i = 0; i < n; ++i)
if (outDegree[i] == 0)
ans.push_back(i);
return ans;
}
};