-
Notifications
You must be signed in to change notification settings - Fork 2
/
14567_josueyeon.cpp
71 lines (56 loc) · 1.37 KB
/
14567_josueyeon.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// 위상 정렬: 방향 그래프의 순서대로 정렬
// 이때 수업을 듣는 학기를 증가시키기 위해 pair를 사용해서 학기를 저장한다
#include <vector>
#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;
vector<int> graph[1001];
int indegree[1001];
void topologySort(int N)
{
vector<pair<int, int>> result;
queue<pair<int, int>> q;
for(int i = 1;i <= N;i++)
{
if (indegree[i] == 0)
q.push({i, 1});
}
while(!q.empty())
{
int temp = q.front().first;
int sem = q.front().second;
q.pop();
result.push_back({temp, sem});
for(int i = 0;i < graph[temp].size();i++)
{
indegree[graph[temp][i]] -= 1;
if (indegree[graph[temp][i]] == 0)
q.push({graph[temp][i], sem + 1});
}
}
sort(result.begin(), result.end());
for(int i = 0;i < result.size();i++)
{
cout<<result[i].second<<" ";
}
cout<<"\n";
}
int main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
// N(node), M(edge)
int N, M;
cin>>N>>M;
for(int i = 0;i < M;i++)
{
int a, b;
cin>>a>>b;
graph[a].push_back(b);
indegree[b] += 1;
}
topologySort(N);
return 0;
}