-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path802.cpp
More file actions
53 lines (49 loc) · 1.09 KB
/
Copy path802.cpp
File metadata and controls
53 lines (49 loc) · 1.09 KB
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
#include "common.h"
#include "numeric"
using namespace std;
class Solution {
public:
vector<int> eventualSafeNodes(vector<vector<int>>& graph) {
map<int, vector<int>> t;
vector<int> indegree(graph.size(), 0);
for (int i = 0; i < graph.size(); ++i) {
for (auto node : graph[i]) {
t[node].push_back(i);
indegree[i]++;
}
}
queue<int> q;
for (int i = 0; i < indegree.size(); ++i) {
if (indegree[i] == 0) {
q.push(i);
}
}
vector<bool> temp(graph.size(), false);
while (q.empty() == false) {
temp[q.front()] = true;
for (auto node : t[q.front()]) {
--indegree[node];
if (indegree[node] == 0) {
q.push(node);
}
}
q.pop();
}
vector<int> ans;
for (int i = 0; i < temp.size(); ++i) {
if (temp[i]) {
ans.push_back(i);
}
}
return ans;
}
};
int main() {
Solution s;
vector<vector<int>> v = {{1, 2}, {2, 3}, {5}, {0}, {5}, {}, {}};
auto ret = s.eventualSafeNodes(v);
for (auto r : ret) {
cout << r << endl;
}
return 0;
}