코딩테스트/백준

[골3] 2252 - 줄 세우기

ShovelingLife 2023. 10. 2. 10:23
#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;
}