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 |
Tags
- 문자열
- XR Interaction Toolkit
- VR
- 다이나믹 프로그래밍
- 백준
- ue5
- 자료구조
- c++
- 백트래킹
- DFS
- BFS
- 그래프
- 시뮬레이션
- 브루트포스
- 트리
- 유니온 파인드
- 유니티
- Team Fortress 2
- 구현
- Unreal Engine 5
- 재귀
- 누적 합
- 우선순위 큐
- 수학
- 다익스트라
- 정렬
- 스택
- 그리디 알고리즘
- 알고리즘
- 투 포인터
Archives
- Today
- Total
1일1알
백준 1766번 문제집 C++ (골드2) 본문
https://www.acmicpc.net/problem/1766
어떤 문제를 풀기전에 풀어야 하는 문제가 있기 때문에 이에 적합한 위상정렬을 사용해서 문제를 해결하였다.
그리고 번호가 낮은 문제부터 풀어야 하기 때문에 위상정렬에서 사용하는 큐를 우선순위 큐로 바꾸고 낮은 번호부터 뽑히도록 하였다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
vector<vector<int>> graph;
vector<int> inDegree;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
graph = vector<vector<int>>(n + 1, vector<int>());
inDegree = vector<int>(n + 1, 0);
for (int i = 0; i < m; i++)
{
int a, b;
cin >> a >> b;
inDegree[b]++;
graph[a].push_back(b);
}
priority_queue<int, vector<int>, greater<int>> pq;
for (int i = 1; i <= n; i++)
{
if (inDegree[i] == 0) pq.push(i);
}
vector<int> ans;
while (!pq.empty())
{
int curr = pq.top();
ans.push_back(curr);
pq.pop();
for (int next : graph[curr])
{
if (inDegree[next] == 1) pq.push(next);
else inDegree[next]--;
}
}
for (auto a : ans)
{
cout << a << " ";
}
}
'알고리즘' 카테고리의 다른 글
백준 17143번 낚시왕 C++ (골드1) (0) | 2024.05.29 |
---|---|
백준 1799번 비숍 C++ (골드1) (0) | 2024.05.28 |
백준 9328번 열쇠 C++ (골드1) (0) | 2024.05.26 |
백준 27172번 수 나누기 게임 C++ (골드5) (0) | 2024.05.26 |
백준 20529번 가장 가까운 세 사람의 심리적 거리 C++ (실버1) (0) | 2024.05.26 |