-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find-Eventual-Safe-States.cpp
55 lines (46 loc) · 1.19 KB
/
Find-Eventual-Safe-States.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
48
49
50
51
52
53
54
55
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
bool DFS(int s, vector<bool> &visited, vector<bool> &dfsVisited, vector<vector<int>> &graph, vector<bool> &present_cycle)
{
visited[s] = true;
dfsVisited[s] = true;
for (auto v : graph[s])
{
if (!visited[v])
{
if (DFS(v, visited, dfsVisited, graph, present_cycle))
return present_cycle[s] = true;
}
else if (visited[v] && dfsVisited[v])
return present_cycle[s] = true;
}
dfsVisited[s] = false;
return false;
}
vector<int> eventualSafeNodes(vector<vector<int>> &graph)
{
int n = graph.size();
vector<int> ans;
vector<bool> visited(n, false), dfsVisited(n, false), present_cycle(n, false);
for (int i = 0; i < n; i++)
if (!visited[i])
DFS(i, visited, dfsVisited, graph, present_cycle);
for (int i = 0; i < n; i++)
if (!present_cycle[i])
ans.push_back(i);
return ans;
}
};
int main()
{
Solution st;
vector<vector<int>> graph = {{1, 2}, {2, 3}, {5}, {0}, {5}, {}, {}};
vector<int> ans = st.eventualSafeNodes(graph);
for (auto num : ans)
cout << num << " ";
cout << endl;
return 0;
}