Pagini recente » Cod sursa (job #1699956) | Cod sursa (job #1294711) | Cod sursa (job #2067166) | Cod sursa (job #2495058) | Cod sursa (job #2714871)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream cin("sortaret.in");
ofstream cout("sortaret.out");
const int NMAX = 1e5;
int N, M;
vector <int> g[NMAX + 2];
bool seen[NMAX + 2];
vector <int> topSort;
void dfs(int node) {
seen[node] = true;
for(int it : g[node]) {
if(!seen[it]) {
dfs(it);
}
}
topSort.push_back(node);
}
int main() {
cin >> N >> M;
for(int i = 1; i <= M; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
}
for(int i = 1; i <= N; i++) {
if(!seen[i]) {
dfs(i);
}
}
reverse(topSort.begin(), topSort.end());
for(int it : topSort) {
cout << it << ' ';
}
return 0;
}