-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path802.cpp
29 lines (28 loc) · 782 Bytes
/
802.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
class Solution {
public:
vector<int> eventualSafeNodes(vector<vector<int>> &graph) {
vector<int> ret;
vector<int> res;
int n = graph.size();
res = vector<int>(n, 0);
for (int i = 0; i < n; ++i) {
if (res[i] == 0)
DFS(i, graph, res);
}
for (int i = 0; i < n; ++i)
if (res[i] == 2)
ret.push_back(i);
return ret;
}
bool DFS(int root, vector<vector<int>> &graph, vector<int> &res) {
res[root] = 1;
for (auto &node:graph[root]) {
if (res[node] == 2)
continue;
if (res[node] == 1 || !DFS(node, graph, res))
return false;
}
res[root] = 2;
return true;
}
};