티스토리 뷰
https://www.acmicpc.net/problem/1766
1766번: 문제집
첫째 줄에 문제의 수 N(1 ≤ N ≤ 32,000)과 먼저 푸는 것이 좋은 문제에 대한 정보의 개수 M(1 ≤ M ≤ 100,000)이 주어진다. 둘째 줄부터 M개의 줄에 걸쳐 두 정수의 순서쌍 A,B가 빈칸을 사이에 두고 주
www.acmicpc.net
우선순위 큐와 위상정렬을 이용해 풀이했습니다.
C++ 소스 코드
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#define MAX 32001
using namespace std;
vector<int> adj[MAX];
int inDegree[MAX];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n,m,a,b;
cin >> n >> m;
for(int i=0;i<m;i++){
cin >> a >> b;
adj[a].push_back(b);
inDegree[b]++;
}
priority_queue<int, vector<int>, greater<int> > pq;
for(int i=1;i<=n;i++){
if(inDegree[i] == 0) {
pq.push(i);
}
}
for(int t = 0;t<n;t++){
int now = pq.top();
pq.pop();
cout << now << ' ';
for(int i=0;i<adj[now].size();i++){
int next = adj[now][i];
if(--inDegree[next] == 0){
pq.push(next);
}
}
}
return 0;
}'Algorithm > 알고리즘 문제풀이' 카테고리의 다른 글
| BOJ) 1325 - 효율적인 해킹 (0) | 2020.11.23 |
|---|---|
| BOJ) 11728 - 배열 합치기 (0) | 2020.11.15 |
| BOJ) 2636 - 치즈 (0) | 2020.11.11 |
| BOJ) 1504 - 특정한 최단 경로 (0) | 2020.11.07 |
| BOJ) 2589 - 보물섬 (0) | 2020.11.06 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- C++
- SWEA
- blendshape
- ARKit
- UIHostingController
- disposeBag
- Swift
- boj
- 알고리즘
- coreml
- rxswift
- cocoapods
- Reactivex
- 백준
- Swift weak
- 코코아팟
- GraphDB
- SwiftUI
- 안드로이드
- Kotlin
- 프로그래머스
- ios
- Lottie
- blendshapes
- Swift unowned
- 백준온라인저지
- DispatchQueue
- rxswift6
- infallible
- 카카오인턴십
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
글 보관함