https://m.blog.naver.com/kks227/220800013823
에서 위상정렬을 공부하고 풀었다.
글에서 쓰여진대로 들어오는 정점을 어떻게 끊을까 싶었는데, 개수만 센다는게 재밌었다.
https://www.acmicpc.net/problem/2252
#include <cstdio>
#include <vector>
#include <queue>
std::vector<int> graph[32010];
int n, m, a, b, indegree[32010], ans[32010];
int main() {
scanf("%d %d", &n, &m);
for(int i = 0; i < m; i++) {
scanf("%d %d", &a, &b);
graph[a].push_back(b);
indegree[b]++;
}
std::queue <int> queue;
for(int i = 1; i <= n; i++) {
if(indegree[i] == 0) queue.push(i);
}
for(int i = 1; i <= n; i++) {
ans[i] = queue.front();
queue.pop();
for(int next:graph[ans[i]]) if(--indegree[next] == 0) queue.push(next);
}
for(int i = 1; i <= n; i++) printf("%d ", ans[i]);
}
'알고리즘 > BOJ' 카테고리의 다른 글
Good Bye, BOJ 2021! (0) | 2021.12.31 |
---|---|
boj 2747, 1182, 11729 (2) | 2021.08.11 |
10026-적록색약 (0) | 2021.05.19 |
14502-연구소 (0) | 2021.05.19 |
17978-Washer (2) | 2020.12.25 |
15682 - 삼차 방정식 풀기 2 (0) | 2020.12.25 |
13705-Ax+Bsin(x)=C (0) | 2020.12.25 |
[Python]7420-맹독 방벽 (0) | 2020.09.25 |