Cod sursa(job #1698060)

Utilizator ArkinyStoica Alex Arkiny Data 3 mai 2016 16:21:24
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include<fstream>
#include<algorithm>
#include<vector>
#include<stack>
using namespace std;

ifstream in("sortaret.in");
ofstream out("sortaret.out");

int viz[50010];

vector<int> G[50010];
stack<int> stk;

void sort_top(int x)
{
	viz[x] = 1;

	for (int i = 0;i < G[x].size();++i)
		if (!viz[G[x][i]])
			sort_top(G[x][i]);
	stk.push(x);

}


int main()
{
	
	int N, M;
	in >> N >> M;

	for (int i = 1;i <= M;++i)
	{
		int x, y;
		in >> x >> y;
		G[x].push_back(y);
	}

	for (int i = 1;i <= N;++i)
		if (!viz[i])
			sort_top(i);

	while (stk.size())
		out << stk.top() << " ", stk.pop();



	return 0;
}