Skip to content

Commit

Permalink
24-08-14 줄 세우기
Browse files Browse the repository at this point in the history
  • Loading branch information
makehard23 committed Aug 14, 2024
1 parent 67538f5 commit e0454ee
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions yuyu0830/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
| 19차시 | 2024.07.12 | 재귀 | [우수마을](https://www.acmicpc.net/problem/1949) | - |
| 20차시 | 2024.07.22 | LIS | [가장 긴 증가하는 부분 수열 5](https://www.acmicpc.net/problem/14003) | - |
| 21차시 | 2024.07.23 | 수학 | [1의 개수 세기](https://www.acmicpc.net/problem/9527) | - |
| 24차시 | 2024.08.14 | 위상정렬 | [줄 세우기](https://www.acmicpc.net/problem/2252) | - |
---
54 changes: 54 additions & 0 deletions yuyu0830/위상정렬/2252.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <iostream>
#include <vector>
#include <queue>

#define NUM 32001

using namespace std;

int n, m;
int enter[NUM] = {0, };
bool visited[NUM] = {0, };

vector<int> rule[NUM];

int main() {
cin >> n >> m;

for (int i = 0; i < m; i++) {
int a, b; cin >> a >> b;

rule[a].push_back(b);
enter[b]++;
}

queue<int> empty;
vector<int> ans;

for (int i = 1; i <= n; i++) {
if (!enter[i]) {
empty.push(i);
visited[i] = true;
}
}

while (!empty.empty()) {
int cur = empty.front();
empty.pop();

ans.push_back(cur);

for (int i : rule[cur]) {
enter[i]--;

if (!enter[i]) {
empty.push(i);
visited[i] = true;
}
}
}

for (int i : ans) {
printf("%d ", i);
}
}

0 comments on commit e0454ee

Please sign in to comment.