-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKahn.cpp
More file actions
84 lines (75 loc) · 1.32 KB
/
Kahn.cpp
File metadata and controls
84 lines (75 loc) · 1.32 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef std::vector<std::vector<int>> Graph;
//std::vector<std::vector<int>> G; // 인접 노드
//std::vector<int> DAG; // 위상정렬
std::vector<int> topologicalsort(const Graph &G, int v)
{
std::vector<int> indegree;
indegree.resize(v + 1);
std::queue<int> qu;
std::vector<int > DAG;
for (int i = 1; i <= v; i++)
{
int e = G[i].size();
for (int j = 0; j < e; j++)
{
indegree[G[i][j]]++;
}
}
for (int i = 1; i <= v; i++)
{
if (indegree[i] == 0)
{
qu.push(i);
}
}
for (int i = 1; i <= v; i++)
{
if (qu.empty())
{
return DAG;
}
int x = qu.front();
qu.pop();
DAG.push_back(x);
int x_size = G[x].size();
for (int i = 0; i < x_size; i++)
{
int y = G[x][i];
indegree[y]--;
if (indegree[y] == 0)
{
qu.push(y);
}
}
}
return DAG;
}
int main()
{
Graph G;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
std::cin >> n >> m;
G.resize(n + 1);
for (int i = 0; i < m; i++)
{
int u, v;
std::cin >> u >> v;
G[u].push_back(v);
}
for (int i = 1; i <= n; i++) // 리스트 넘버순 정렬
std::sort(G[i].begin(), G[i].end());
std::vector<int > DAG = topologicalsort(G, n);
for (auto a : DAG)
{
std::cout << a << ' ';
}
return 0;
}