Cod sursa(job #2929418)

Utilizator gabriel10tm@gmail.comGabriel Marian [email protected] Data 25 octombrie 2022 20:36:53
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.24 kb
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pi = pair<int, int>;
using ti = tuple<int, int, int>;
using tl = tuple<ll, ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pi>;
using vpil = vector<pil>;
using vpli = vector<pli>;
using vpl = vector<pl>;
using vti = vector<ti>;
using vtl = vector<tl>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvpi = vector<vpi>;
using vvpil = vector<vpil>;
using vvpli = vector<vpli>;
using vvpl = vector<vpl>;
using vvti = vector<vti>;
using vvtl = vector<vtl>;


#if 1
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
#define cin fin
#define cout fout
#endif
const int nmx = 5e4 + 2;
set<int> a[nmx];
int kt = 0;
int srt[nmx];
int vis[nmx];
void Dfs(int k)
{
	vis[k] = 1;
	for (int to : a[k])
	{
		if (vis[to] == 0)
		{
			Dfs(to);
		}
	}
	srt[kt++] = k;

}

int main()
{
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int x, y;
		cin >> x >> y;
		a[x].insert(y);
	}
	for (int i = 1; i <= n; i++)
		if(vis[i] == 0)
			Dfs(i);
	for (int i = kt - 1; i >= 0; i--)
	{
		cout << srt[i] << " ";
	}
}