Cod sursa(job #2317220)

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

using namespace std;

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

const int VMAX = 50000;

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

int V, E;

void readData() {

	fin >> V >> E;

	Stack.reserve(V);
	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_back(node);
}

int main() {

	readData();

	for (int node = 1; node <= V; ++node)
		DFS(node);

	for (const int &node : Stack)
		fout << node << ' ';
}