Pagini recente » Cod sursa (job #485097) | Cod sursa (job #187075) | Cod sursa (job #249929) | Cod sursa (job #1035797) | Cod sursa (job #3040464)
#include <iostream>
#include <fstream>
#include <vector>
std::vector<std::vector<int>> graph;
std::vector<bool> visited;
std::vector<int> stack;
void dfs(int node) {
for (const auto &x: graph[node]) {
if (!visited[x]) {
visited[x] = true;
dfs(x);
}
}
stack.push_back(node);
}
int main() {
std::ifstream input("sortaret.in");
std::ofstream output("sortaret.out");
int n, m;
input >> n >> m;
graph.resize(n + 1);
visited.resize(n + 1, false);
while (m--) {
int x, y;
input >> x >> y;
graph[x].push_back(y);
}
for (int i = 1; i <= n; ++i) {
if (!visited[i]) dfs(i);
}
while (!stack.empty()) {
output << stack.back() << " ";
stack.pop_back();
}
return 0;
}