Pagini recente » Cod sursa (job #1948704) | Cod sursa (job #238274) | Cod sursa (job #7622) | Cod sursa (job #988126) | Cod sursa (job #1561455)
#include <fstream>
#include <array>
#include <vector>
#include <stack>
#include <iterator>
#include <algorithm>
const int NMAX = 50011;
std::stack<int> rTopSort;
std::array<bool, NMAX> vWas;
std::array<std::vector<int>, NMAX> G;
void dfs(int x) {
if (vWas[x]) return;
vWas[x] = true;
for (const int& y: G[x]) {
if (!vWas[y]) {
dfs(y);
}
}
rTopSort.push(x);
}
int main() {
int N, M, x, y;
std::ifstream in{"sortaret.in"};
std::ofstream out{"sortaret.out"};
in >> N >> M;
while(M--) {
in >> x >> y;
G[x].push_back(y);
}
for (int i = 1; i <= N; ++i) {
if (!vWas[i]) {
dfs(i);
}
}
for (; !rTopSort.empty(); rTopSort.pop()) {
out << rTopSort.top() << ' ';
}
out << '\n';
return 0;
}