-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion67.cpp
41 lines (38 loc) · 1.04 KB
/
Question67.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
67 -> Find Eventual Safe States
Solution :
class Solution {
public:
bool dfs(vector<vector<int>>& graph,int source,vector<bool>&visited,vector<bool>&dfsvisited){
visited[source]=true;
dfsvisited[source]=true;
for(auto x:graph[source]){
if(!visited[x]){
if(dfs(graph,x,visited,dfsvisited)){
return true;
}
}
else if(dfsvisited[x]){
return true;
}
}
dfsvisited[source]=false;
return false;
}
vector<int> eventualSafeNodes(vector<vector<int>>& graph) {
int n=graph.size();
vector<bool>visited(n);
vector<bool>dfsvisited(n);
for(int i=0;i<n;i++){
if(visited[i]==false){
dfs(graph,i,visited,dfsvisited);
}
}
vector<int>ans;
for(int i=0;i<n;i++){
if(dfsvisited[i]==false){
ans.push_back(i);
}
}
return ans;
}
};