-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopological_Sort.cpp
More file actions
64 lines (54 loc) · 1.46 KB
/
Topological_Sort.cpp
File metadata and controls
64 lines (54 loc) · 1.46 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
56
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
// トポロジカルソートを実行する
vector<int> TopologicalSort(vector<vector<int>> vecGraph, vector<int> vecEdges)
{
// 処理する頂点のインデックス
int idx(0);
// 処理待ちの頂点を詰める
queue<int> edgeque;
// 入次数が0の頂点を集める
for (int i = 0; i < vecEdges.size(); i++)
{
if (vecEdges[i] == 0)
{
edgeque.push(i);
}
}
// 出力する配列
vector<int> vecTopo;
while (!edgeque.empty())
{
// 出次数0の頂点を取り出す
idx = edgeque.front();
edgeque.pop();
// 隣接する頂点の出次数を減算する
for(auto edge : vecGraph[idx]) {
vecEdges[edge] -= 1;
if(vecEdges[edge] == 0) edgeque.push(edge);
}
vecTopo.push_back(idx);
}
return vecTopo;
}
int main() {
int num_vertex(0),num_edge(0);
cin >> num_vertex >> num_edge;
// 各ノードの情報を管理する配列
vector<vector<int>> vecGraph(num_vertex);
// 各頂点の入次数を記録する配列
vector<int> vecEdges(num_vertex,0);
for (int i = 0; i < num_edge; i++)
{
int edge1(0), edge2(0);
cin >> edge1 >> edge2;
vecGraph[edge1].push_back(edge2);
vecEdges[edge2] += 1;
}
vector<int> vecRes = TopologicalSort(vecGraph, vecEdges);
for (auto edge : vecRes)
{
cout << edge << "\n";
}
return 0;
}