Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- dx12
- 4134번
- lis응용
- Perforce
- 백준 24444 c++
- 2d 박스충돌
- 다음소수
- 백준 c++ 24479번
- 백준
- 랜더링 파이프라인
- 뱀과사다리게임
- dx12 정리
- WinAPI
- 11286번
- 드래곤플라이트 모작
- 다익스트라
- 애니메이션
- BFS
- 백준 1260 c++
- 2565번
- 2075번
- unrealengine
- C++
- DirectX12
- 바이토닉 수열
- 루트서명
- directx12 그리기 연산2
- 24779
- Unreal
- 그리기 연산
Archives
- Today
- Total
game-1 님의 블로그
[백준] 2252번 C++ 줄 세우기 , 위상 정렬 본문


풀이)
이 문제는 위상정렬을 이용하는 문제로, 저의 경우 큐를 이용하여 문제를 해결하였습니다.
위상 정렬 문제는 처음 풀어봐서 제가 설명드리는 것 보다 문제풀이에 참고한 사이트를 소개해드리겠습니다.
https://m.blog.naver.com/ndb796/221236874984
25. 위상 정렬(Topology Sort)
위상 정렬(Topology Sort)은 '순서가 정해져있는 작업'을 차례로 수행해야 할 때 그 순서를 ...
blog.naver.com
이분 설명진짜 잘 하십니다..
코드)
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int n, m;
vector<vector<int>>graph;
vector<int> result;
vector<int> indegree;
void topologysort() {
queue<int> q;
for (int i = 1; i <= n; ++i) {
if (indegree[i] == 0) q.push(i);
}
while (not q.empty()) {
int tmp = q.front();
q.pop();
result.emplace_back(tmp);
for (const int a : graph[tmp]) {
indegree[a]--;
if (indegree[a] == 0) q.push(a);
}
}
}
int main()
{
cin >> n >> m;
graph.resize(n+1);
indegree.assign(n+1, 0);
while (m--) {
int u, v;
cin >> u >> v;
graph[u].push_back(v);
indegree[v]++;
}
topologysort();
for (const int n : result) {
cout << n << ' ';
}
}

'백준 문제풀이' 카테고리의 다른 글
| [백준]1916 번 C++ 최소 비용 구하기 컴파일에러 (1) | 2025.02.08 |
|---|---|
| [백준] 1238번 C++ 파티 (1) | 2025.02.07 |
| [백준] c++ 13549번 숨바꼭질 3 (bfs풀이) (1) | 2025.02.06 |
| [백준] 1504번 C++ 특정한 최단 경로 (0) | 2025.02.05 |
| [백준] 2075번 N번째 큰 수 C++ (2) | 2025.02.03 |