We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 2a3ad90 commit a435ea5Copy full SHA for a435ea5
802/802.cpp
@@ -0,0 +1,39 @@
1
+class Solution {
2
+public:
3
+
4
+ enum Color{
5
+ white,
6
+ gray,
7
+ black
8
+ };
9
10
+ int dfs(vector<vector<int>>& graph, vector<int>& color, int x){
11
12
+ // base
13
+ if(color[x])
14
+ return color[x] == black;// safe
15
16
+ // main
17
+ color[x] = gray;
18
+ for(auto v: graph[x]){
19
+ if(!dfs(graph, color, v))
20
+ return false;
21
+ }
22
+ color[x] = black;
23
+ return true;
24
25
26
+ vector<int> eventualSafeNodes(vector<vector<int>>& graph) {
27
+ int n = graph.size();
28
+ vector<int> color(n, white);
29
30
+ // dfs that returns safe/unsafe
31
+ vector<int> safe;
32
+ for(int i = 0; i < n; i++){
33
+ if(dfs(graph, color, i))
34
+ safe.push_back(i);
35
36
37
+ return safe;
38
39
+};
0 commit comments