-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventualSafeStates.cpp
47 lines (42 loc) · 1.14 KB
/
EventualSafeStates.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
42
43
44
45
46
47
class Solution {
private:
bool dfs(int node, vector<int>&vis,vector<vector<int>>& graph, vector<int>&pathVis, vector<int>&check){
vis[node]=1;
pathVis[node]=1;
check[node]=0;
for(auto it:graph[node]){
if(!vis[it]){
if(dfs(it,vis,graph,pathVis,check)==true){
check[node]=0;
return true;
}
}
else if(pathVis[it]){
check[node]=0;
return true;
}
}
check[node]=1;
pathVis[node]=0;
return false;
}
public:
vector<int> eventualSafeNodes(vector<vector<int>>& graph) {
int V=graph.size();
vector<int>vis(V,0);
vector<int>pathVis(V,0);
vector<int>check(V,0);
vector<int>safe;
for(int i=0;i<V;i++){
if(!vis[i]){
dfs(i,vis,graph,pathVis,check);
}
}
for(int i=0;i<V;i++){
if(check[i]==1){
safe.push_back(i);
}
}
return safe;
}
};