-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind Eventual Safe States.cpp
More file actions
25 lines (25 loc) · 936 Bytes
/
Copy pathFind Eventual Safe States.cpp
File metadata and controls
25 lines (25 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public:
bool dfscheck(int node, vector<vector<int>>& graph, vector<bool>& visited, vector<int>& isSafe){
if(isSafe[node] != -1) return isSafe[node];
for(auto& neighbour:graph[node]){
if(visited[neighbour]) return isSafe[node] = false;
visited[neighbour] = true;
bool temp = dfscheck(neighbour, graph, visited, isSafe);
visited[neighbour] = false;
if(!temp) return isSafe[node] = false;
}
return isSafe[node] = true;
}
vector<int> eventualSafeNodes(vector<vector<int>>& graph) {
vector<int> isSafe(graph.size(), -1);
vector<int> ans;
vector<bool> visited(graph.size(), 0);
for(int i = 0; i < graph.size(); i++){
visited[i] = true;
if(dfscheck(i, graph, visited, isSafe)) ans.push_back(i);
visited[i] = false;
}
return ans;
}
};