forked from int32bit/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfs.cpp
55 lines (54 loc) · 1.3 KB
/
bfs.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 <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <utility>
#include <iostream>
using namespace std;
class Solution {
public:
vector<int> findOrder(int n, vector<pair<int, int>> &request) {
return topsort(n, request);
}
private:
/* BFS */
vector<int> topsort(int n, const vector<pair<int, int>> &request) {
vector<int> result;
vector<int> degree(n, 0);
for (auto p : request) {
degree[p.first]++;
}
while (result.size() < n) { // 还没有访问完
int cur = findZero(degree);
if (cur >= 0) { // 访问节点cur
result.push_back(cur);
degree[cur] = -1; // 标记当前节点为已经访问状态
for (auto p : request) {
if (p.second == cur)
degree[p.first]--; // 去掉已访问节点
}
} else
return vector<int>();
}
return result;
}
int findZero(const vector<int> &v) {
int n = v.size();
for (int i = 0; i < n; ++i) {
if (v[i] == 0)
return i;
}
return -1;
}
};
void print(vector<int> v) {
for_each(begin(v), end(v), [](int i){cout << i << ' ';});
cout << endl;
}
int main(int argc, char **argv)
{
Solution solution;
vector<pair<int, int>> request = {make_pair(1, 0), make_pair(2, 0), make_pair(3, 1), make_pair(3, 2)};
print(solution.findOrder(4, request));
return 0;
}