Cod sursa(job #2514741)

Utilizator kokitchyAlastor kokitchy Data 26 decembrie 2019 18:28:08
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>
#include <vector>
#include <stack>

const int NMAX = 50002;

int viz[NMAX];
std::vector<int> G[NMAX];

std::stack <int> q;

void DFS(int u) {
	viz[u] = 1;
	for (int i = 0; i < G[u].size(); i++) {
		int v = G[u][i];
		if (!viz[v])
			DFS(v);
	}

	q.push(u);
}

int main()
{
	std::ifstream fin("sortaret.in");
	std::ofstream fout("sortaret.out");

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

	for (int i = 1; i <= n; i++)
		if (!viz[i])
			DFS(i);

	while (!q.empty()) {
		fout << q.top() << " ";
		q.pop();
	}

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

	return 0;
}