728x90
반응형
https://www.acmicpc.net/problem/2623
#include <iostream>
#include <vector>
#include<queue>
using namespace std;
int n, m;
vector<int>indegree;
vector<vector<int>>graph;
vector<int>arr;
void tpsort() {
queue<int>q;
for (int i = 1; i <= n; i++) {
if (indegree[i] == 0)q.push(i);
}
while(!q.empty()) {
int p = q.front();
arr.push_back(p);
q.pop();
for (int i = 0; i < graph[p].size(); i++) {
int next = graph[p][i];
indegree[next]--;
if (indegree[next] == 0)q.push(next);
}
}
}
int main() {
cin >> n >> m;
indegree.assign(n+1, 0);
graph.assign(n + 1, vector<int>());
int num;
for (int i = 0; i < m; i++) {
cin >> num;
vector<int>t;
int tmp;
t.assign(num, 0);
for(int j=0;j<num;j++){
cin >> t[j];
}
for (int j = 0; j < num-1; j++) {
graph[t[j]].push_back(t[j+1]);
indegree[t[j + 1]]++;
}
}
tpsort();
int cnt = 0;
for (int i = 0; i < n; i++) {
if (indegree[i] > 0)cnt++;
}
if (cnt > 0) {
cout << 0; return 0;
}
for (int i = 0; i < n; i++) {
cout<<arr[i]<<'\n';
}
}
728x90
반응형
'하루 1문제 챌린지 > Gold3' 카테고리의 다른 글
백준 117799번 최소비용구하기 2 (4) | 2024.03.17 |
---|---|
백준 16202번 MST게임(C++)🚩 (0) | 2024.03.07 |
백준 1774번 우주신과의 교감(C++) (0) | 2024.03.04 |