Cod sursa(job #1699904)

Utilizator Gabiap2015Apostol Gabriel Gabiap2015 Data 8 mai 2016 20:09:02
Problema Sortare topologica Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include "iostream"
#include "vector"
#include "stack"
#include "fstream"
using namespace std;

vector<int> graf[10000];
int nod[10000], n, m;
stack<int> st;

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


void dfs(int nod_start)
{
	nod[nod_start] = 1;
	for (int i = 0; i < graf[nod_start].size(); i++)
	{
		if (nod[graf[nod_start][i]] == 0)
			dfs(graf[nod_start][i]);
	}
	nod[nod_start] = 2;
	st.push(nod_start);
}

int main()
{
	int x, y;
	sortare_in >> n >> m;
	for (int i = 0; i < m; i++)
	{
		sortare_in >> x >> y;
		graf[x].push_back(y);
	}
	for (int i = 1; i <= n; i++)
	{
		if (nod[i] == 0)
			dfs(i);
	}
	while (!st.empty())
	{
		sortare_out << st.top()<<' ';
		st.pop();
	}
	return 0;
}