Cod sursa(job #806940)

Utilizator BitOneSAlexandru BitOne Data 3 noiembrie 2012 19:12:58
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iterator>
#include <algorithm>

using namespace std;

const int NMAX = 50011;

bool was[NMAX];
vector< int > G[NMAX];
vector< int > dfsOrder;

inline void DFS(int x)
{
	was[x] = true;
	vector<int>::const_iterator it, iend;
	
	for(it = G[x].begin(), iend = G[x].end(); it < iend; ++it)
	{
		if(false == was[*it])
		{
			DFS(*it);
		}
	}
	dfsOrder.push_back(x);
}

int main()
{
	int N, M, x, y;
	ifstream in("sortaret.in");
	ofstream out("sortaret.out");
	
	for(in >> N >> M; M; --M)
	{
		in >> x >> y;
		G[x].push_back(y);
	}
	
	for(x = 1; x <= N; ++x)
	{
		if(false == was[x])
		{
			DFS(x);
		}
	}
	
	copy(dfsOrder.rbegin(), dfsOrder.rend(), ostream_iterator<int>(out, " "));
	
	return EXIT_SUCCESS;
}