Cod sursa(job #2195830)

Utilizator WebDesignbyTMGhiorghiu Ioan-Viorel WebDesignbyTM Data 17 aprilie 2018 14:09:14
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.58 kb
#define DM 50001
#include <bitset>
#include <fstream>
#include <vector>
using namespace std;

ifstream fi ("sortaret.in");
ofstream fo ("sortaret.out");
bitset <DM> bs;
int n, m, a, b, s;
vector <int> v[DM], sol;

void dfs(int x)
{
	if (!bs[x])
	{
		sol.push_back(x);
		bs[x] = 1;
	}
	for (auto i:v[x])
		dfs(i);
}

int main()
{
	fi >> n >> m;
	for (int i = 1; i <= m; ++i)
	{
		fi >> a >> b;
		bs[b] = 1;
		v[a].push_back(b);
	}
	for (int i = 1; i <= n && !s; ++i)
		if (!bs[i])
			s = i;
	for (int i = 0; i < DM; ++i)
		bs[i] = 0;
	dfs(s);
	for (auto i:sol)
		fo << i << ' ';
	return 0;
}