Cod sursa(job #1726589)

Utilizator andreitulusAndrei andreitulus Data 8 iulie 2016 14:13:41
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//


#include <stdio.h>
#include <fstream>
#include<vector>
#include <stack>
#define maxn 50003
using namespace std;

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


vector <int> L[maxn];
stack <int> s;
int n, m, v[maxn];

void read()
{
	int i, x, y;

	fin >> n >> m;

	for (i = 1; i <= m; i++)
	{
		fin >> x >> y;

		L[x].push_back(y);
	}
}




void DFS(int x)
{
	v[x] = 1;

	int i;

	for (i = 0; i < L[x].size(); i++)
		if (v[L[x][i]] == 0)
			DFS(L[x][i]);

	s.push(x);

}



void solve()
{
	int i;

	for (i = 1; i <= n; i++)
		if (v[i] == 0)
		{
			DFS(i);
		}
}


void afis()
{
	while (!s.empty())
	{
		fout << s.top() << " ";
		s.top();
	}

}



int main()
{
	

	read();

	solve();

	afis();

	fin.close();
	fout.close();

	return 0;
}