-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtopological_sort_bfs.cpp
More file actions
55 lines (47 loc) · 1.39 KB
/
Copy pathtopological_sort_bfs.cpp
File metadata and controls
55 lines (47 loc) · 1.39 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
54
55
//
// トポロジカルソート (by BFS)
//
// cf.
// BFS (幅優先探索) 超入門! 〜 キューを鮮やかに使いこなす 〜
// https://qiita.com/drken/items/996d80bcae64649a6580
//
//
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
using Graph = vector<vector<int>>;
int main() {
// 頂点数と辺数
int N, M; cin >> N >> M;
// グラフ入力受取
Graph G(N);
vector<int> deg(N, 0); // 各頂点の出次数
for (int i = 0; i < M; ++i) {
int a, b;
cin >> a >> b;
G[b].push_back(a); // 逆向きに辺を張る
deg[a]++; // 出次数
}
// シンクたちをキューに挿入する
queue<int> que;
for (int i = 0; i < N; ++i) if (deg[i] == 0) que.push(i);
// 探索開始
vector<int> order;
while (!que.empty()) {
// キューから頂点を取り出す
int v = que.front(); que.pop();
order.push_back(v);
// v へと伸びている頂点たちを探索する
for (auto nv : G[v]) {
// 辺 (nv, v) を削除する
--deg[nv];
// それによって nv が新たにシンクになったらキューに挿入
if (deg[nv] == 0) que.push(nv);
}
}
// 答えをひっくり返す
reverse(order.begin(), order.end());
for (auto v : order) cout << v << endl;
}