Pagini recente » Cod sursa (job #1682842) | Cowfood | Rating Neculau Rares-Andrei (Andrei_ierdnA) | Cod sursa (job #3297779) | Cod sursa (job #806940)
Cod sursa(job #806940)
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iterator>
#include <algorithm>
using namespace std;
const int NMAX = 50011;
bool was[NMAX];
vector< int > G[NMAX];
vector< int > dfsOrder;
inline void DFS(int x)
{
was[x] = true;
vector<int>::const_iterator it, iend;
for(it = G[x].begin(), iend = G[x].end(); it < iend; ++it)
{
if(false == was[*it])
{
DFS(*it);
}
}
dfsOrder.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(false == was[x])
{
DFS(x);
}
}
copy(dfsOrder.rbegin(), dfsOrder.rend(), ostream_iterator<int>(out, " "));
return EXIT_SUCCESS;
}