Pagini recente » Cod sursa (job #535681) | Cod sursa (job #621262) | Cod sursa (job #1538674) | Cod sursa (job #1944962) | Cod sursa (job #447408)
Cod sursa(job #447408)
/*
* File: main.cpp
* Author: virtualdemon
*
* Created on April 28, 2010, 5:09 PM
*/
#include <stack>
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iterator>
#define Nmax 50011
/*
*
*/
using namespace std;
int uz[Nmax];
stack< int > S;
vector< int > v;
vector< int > G[Nmax];
vector< int >::const_iterator it, iend;
inline void dfs( int x )
{
while( true )
{
for( it=G[x].begin()+uz[x], iend=G[x].end(); it < iend; ++it )
if( !uz[*it] )
break;
uz[x]=it-G[x].begin()+1;
if( it >= iend )
{
v.push_back(x);
break;
}
S.push(x);
x=*it;
}
}
int main(int argc, char** argv)
{
int N, M, x, y;
ifstream in( "sortaret.in" );
for( in>>N>>M; M; --M )
{
in>>x>>y;
G[x].push_back(y);
}
for( x=1; x <= N; ++x )
if( !uz[x] )
{
y=x;
while( true )
{
dfs(y);
if( S.empty() )
break;
y=S.top(); S.pop();
//v.push_back(y);
}
}
ofstream out( "sortaret.out" );
copy( v.rbegin(), v.rend(), ostream_iterator<int>( out, " " ) );
out<<'\n';
return (EXIT_SUCCESS);
}