코딩테스트/백준

[골2] 1766 - 문제집

ShovelingLife 2023. 10. 2. 12:27
#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;
priority_queue<int, vector<int>, greater<int>> pq;

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)
			pq.push(i);
	}
	while (!pq.empty())
	{
		int cur = pq.top();
		pq.pop();
		cout << cur << ' ';

		for (int i = 0; i < graph[cur].size(); i++)
		{
			int next = graph[cur][i];

			if (--degree[next] == 0)
				pq.push(next);
		}
	}
	return 0;
}