| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- object detection
- 자바스크립트
- 회고록
- 알고리즘
- 플로이드 와샬
- back propagation
- 가끔은 말로
- NEXT
- c++
- dfs
- tensorflow
- lazy propagation
- BFS
- pytorch
- 세그먼트 트리
- 2023
- 우선 순위 큐
- 미래는_현재와_과거로
- DP
- 가끔은_말로
- 너비 우선 탐색
- 이분 탐색
- dropout
- 조합론
- 다익스트라
- 백트래킹
- Overfitting
- 분할 정복
- 크루스칼
- 문자열
Archives
- Today
- Total
Doby's Lab
백준 2252번: 줄 세우기 (C++) 본문
https://www.acmicpc.net/problem/2252
2252번: 줄 세우기
첫째 줄에 N(1 ≤ N ≤ 32,000), M(1 ≤ M ≤ 100,000)이 주어진다. M은 키를 비교한 회수이다. 다음 M개의 줄에는 키를 비교한 두 학생의 번호 A, B가 주어진다. 이는 학생 A가 학생 B의 앞에 서야 한다는 의
www.acmicpc.net
각 쌍들 간의 관계로 키를 작은 순서대로 세운다.
>> Topological Sort를 떠올릴 수 있다.
[AC 코드]
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int n, m;
int inDegree[32001];
vector<int> adj[32001];
vector<int> result;
void topologicalSort() {
queue<int> q;
for (int i = 1; i <= n; ++i) {
if (inDegree[i] == 0) {
q.push(i);
result.push_back(i);
}
}
while (!q.empty()) {
int now = q.front();
q.pop();
for (int i = 0; i < adj[now].size(); ++i) {
int next = adj[now][i];
if (--inDegree[next] == 0) {
q.push(next);
result.push_back(next);
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
inDegree[b]++;
}
topologicalSort();
for (int i = 0; i < result.size(); ++i) {
cout << result[i] << ' ';
}
return 0;
}'PS > BOJ' 카테고리의 다른 글
| 백준 2003번: 수들의 합 2 (C++), 투 포인터 (0) | 2022.03.13 |
|---|---|
| 백준 1516번: 게임 개발 (C++) (0) | 2022.03.10 |
| 백준 1005번: ACM Craft (C++) (0) | 2022.03.09 |
| 백준 14567번: 선수과목 (Prerequisite) (C++), 위상 정렬 (1) | 2022.03.07 |
| 백준 17048번: 수열과 쿼리 24 (C++) (0) | 2022.03.07 |
