forked from TheSYNcoder/InterviewPrep
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Graph2.cpp
66 lines (52 loc) · 1.51 KB
/
Graph2.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
56
57
58
59
60
61
62
63
64
65
66
class Solution {
public:
vector<int> eventualSafeNodes(vector<vector<int>>& graph) {
queue<int> q;
int n = graph.size();
int in[10001];
fill( in , in + n+1,0);
vector< vector<int> > edges(n);
for ( int i = 0 ; i < n ; i++){
for ( int x : graph[i]) {
edges[x].push_back(i);
in[i]++;
}
}
for ( int i = 0 ; i < n; i++){
if (in[i] == 0) {
// cout << i << "\n";
q.push(i);
}
}
vector<int> ans;
while(!q.empty()){
int top = q.front();q.pop();
ans.push_back(top);
for ( int ne : edges[top]){
in[ne]--;
if ( in[ne] == 0 )q.push(ne);
}
}
sort( ans.begin(), ans.end());
return ans;
}
};
/*
Alternate solution : Graph colouring
*/
class Solution(object):
def eventualSafeNodes(self, graph):
WHITE, GRAY, BLACK = 0, 1, 2
color = collections.defaultdict(int)
def dfs(node):
if color[node] != white:
return color[node] == BLACK
color[node] = GRAY
for nei in graph[node]:
if color[nei] == BLACK:
continue
if color[nei] == GRAY or not dfs(nei):
return False
color[node] = BLACK
return True
return filter(dfs, range(len(graph)))