Cod sursa(job #2317224)

Utilizator DawlauAndrei Blahovici Dawlau Data 12 ianuarie 2019 23:25:43
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <fstream>
#include <list>
#include <bitset>
#include <stack>

using namespace std;

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

const int VMAX = 50000;

list <int> adjList[1 + VMAX];
bitset <1 + VMAX> seen;
stack <int> Stack;

int V, E;

void readData() {

	fin >> V >> E;

	for(; E; --E) {

		int from, to;
		fin >> from >> to;

		adjList[from].push_back(to);
	}
}

void DFS(int node) {

	seen[node] = true;

	for (const int &nextNode : adjList[node])
		if (!seen[nextNode])
			DFS(nextNode);
	Stack.push(node);
}

int main() {

	readData();

	for (int node = 1; node <= V; ++node)
		if(!seen[node])
			DFS(node);

	for (; !Stack.empty(); Stack.pop())
		fout << Stack.top() << ' ';
}