#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#pragma region 빠른 입출력
#define FAST_IO() \
{\
ios::sync_with_stdio(false);\
cin.tie(NULL); \
cout.tie(NULL); \
}\
#pragma endregion
using namespace std;
vector<int> degree;
vector<vector<int>> graph;
queue<int> q;
int main()
{
FAST_IO();
int n, m;
cin >> n >> m;
graph.assign(100001, vector<int>());
degree.assign(n + 1, 0);
for (int i = 0; i < m; i++)
{
int a, b;
cin >> a >> b;
graph[a].push_back(b);
degree[b]++;
}
for (int i = 1; i <= n; i++)
{
if (degree[i] == 0)
q.push(i);
}
while (!q.empty())
{
int cur = q.front();
q.pop();
cout << cur << ' ';
for (int i = 0; i < graph[cur].size(); i++)
{
int next = graph[cur][i];
if (--degree[next] == 0)
q.push(next);
}
}
return 0;
}
'코딩테스트 > 백준' 카테고리의 다른 글
[골4] 2110 - 공유기 설치 (0) | 2023.10.05 |
---|---|
[골2] 1766 - 문제집 (0) | 2023.10.02 |
[골5] 1717 - 집합의 표현 (0) | 2023.09.30 |
[골4] 4386 - 별자리 만들기 (0) | 2023.09.26 |
[골5] 2166 - 다각형의 면적 (0) | 2023.09.26 |