Pagini recente » Cod sursa (job #521504) | Profil AlexMuntean | Cod sursa (job #2142968) | Cod sursa (job #428466) | Cod sursa (job #3232125)
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
//std::ifstream fin("in");
std::ifstream fin("sortaret.in");
std::ofstream fout("sortaret.out");
//std::ofstream fout("out");
std::vector<int> beenThere, sortareTopologica;
std::vector<std::vector<int>> gf;
int n, m;
void dfs(int node) {
beenThere[node] = 1;
for (const auto& nextNode : gf[node]) {
if (!beenThere[nextNode]) {
dfs(nextNode);
}
}
sortareTopologica.emplace_back(node);
}
int main() {
fin >> n >> m;
beenThere.resize(n + 1, 0);
gf.resize(n + 1);
for (int i = 0; i < m; ++i) {
int x, y;
fin >> x >> y;
gf[x].push_back(y);
}
for (int i = 1; i <= n; ++i) {
if (!beenThere[i]) {
dfs(i);
}
}
for (int i = sortareTopologica.size() - 1; i >= 0; i--) {
fout << sortareTopologica[i] << " ";
}
fout << endl;
return 0;
}