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
- BFS
- 유니온 파인드
- 투 포인터
- 정렬
- 유니티
- 스택
- 다익스트라
- DFS
- 누적 합
- Unreal Engine 5
- 시뮬레이션
- VR
- 다이나믹 프로그래밍
- XR Interaction Toolkit
- 백준
- 자료구조
- 그래프
- 브루트포스
- c++
- 알고리즘
- ue5
- 백트래킹
- 재귀
- 우선순위 큐
- 문자열
- 그리디 알고리즘
- 수학
- 구현
- Team Fortress 2
- 트리
Archives
- Today
- Total
1일1알
백준 2623번 음악프로그램 C++ 본문
https://www.acmicpc.net/problem/2623
2623번: 음악프로그램
첫째 줄에는 가수의 수 N과 보조 PD의 수 M이 주어진다. 가수는 번호 1, 2,…,N 으로 표시한다. 둘째 줄부터 각 보조 PD가 정한 순서들이 한 줄에 하나씩 나온다. 각 줄의 맨 앞에는 보조 PD가 담당한
www.acmicpc.net
위상 정렬
#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<int> inDegree;
vector<vector<int>> graph;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
inDegree = vector<int>(n + 1, 0);
graph = vector<vector<int>>(n + 1, vector<int>());
for (int i = 0; i < m; i++) {
int cnt;
cin >> cnt;
int lastSinger = 0;
for (int j = 0; j < cnt; j++) {
int singer;
cin >> singer;
if (lastSinger != 0) {
graph[lastSinger].push_back(singer);
inDegree[singer]++;
}
lastSinger = singer;
}
}
queue<int> q;
for (int i = 1; i <= n; i++) {
if (inDegree[i] == 0) q.push(i);
}
vector<int> ans;
while (!q.empty()) {
int curr = q.front();
ans.push_back(curr);
q.pop();
for (auto next : graph[curr]) {
if (--inDegree[next] == 0) q.push(next);
}
}
if (ans.size() == n) {
for (auto a : ans) cout << a << " ";
}
else cout << 0;
}
'알고리즘' 카테고리의 다른 글
백준 2473번 세 용액 C++ (0) | 2022.10.02 |
---|---|
백준 17404번 RGB거리 2 C++ (1) | 2022.09.30 |
백준 1005번 ACM Craft C++ (1) | 2022.09.26 |
백준 2252번 줄 세우기 C++ (1) | 2022.09.23 |
백준 14442번 벽 부수고 이동하기 2 C++ (1) | 2022.09.21 |