Cod sursa(job #3234052)

Utilizator betybety bety bety Data 6 iunie 2024 10:31:43
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("sortaret.in");
ofstream out("sortaret.out");
const int lim = 5e4 + 5;
vector<int> vec[lim];
stack<int> topo;
int n, m, x, y;
bool ok[lim];
void df(int nod) {
	ok[nod] = true;
	for (int x : vec[nod]) {
		if (!ok[x]) {
			df(x);
		}
	}
	topo.push(nod);
}
int main() {
	in >> n >> m;
	for (int i = 1; i <= m; ++i) {
		in >> x >> y;
		vec[x].push_back(y);
	}
	for (int i = 1; i <= n; ++i) {
		if (!ok[i]) {
			df(i);
		}
	}
	while (!topo.empty()) {
		out << topo.top() << ' ';
		topo.pop();
	}
	out << '\n';
	return 0;
}