Cod sursa(job #936218)
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iterator>
#include <algorithm>
using namespace std;
const int NMAX = 50011;
bool was[NMAX];
vector<int> ans;
vector<int> G[NMAX];
inline void DFS(int x)
{
was[x] = true;
vector<int>::iterator it = G[x].begin(), iend = G[x].end();
for(; it < iend; ++it)
{
if(!was[*it])
DFS(*it);
}
ans.push_back(x);
}
int main()
{
int N, M, x, y;
ifstream in("sortaret.in");
ofstream out("sortaret.out");
for(in >> N >> M; M; --M)
{
in >> x >> y;
G[x].push_back(y);
}
for(x = 1; x <= N; ++x)
{
if(!was[x]) DFS(x);
}
copy(ans.rbegin(), ans.rend(), ostream_iterator<int>(out, " "));
out << '\n';
return EXIT_SUCCESS;
}