Pagini recente » Cod sursa (job #1353889) | Cod sursa (job #75084) | Cod sursa (job #1405740) | Cod sursa (job #773262) | Cod sursa (job #905079)
Cod sursa(job #905079)
# include <fstream>
# define dim 50001
# include <vector>
using namespace std;
vector< vector < int > > mat( dim );
vector< int > v, grad( dim ), uz( dim );
int n,m;
inline void df( int x )
{
if( grad[ x ] == 0 && ! uz[ x ] )
uz[ x ] = 1, v.push_back( x );
for(int i=0; i<mat[ x ].size(); ++i )
grad[ mat[ x ][ i ] ] -- , df( mat[ x ][ i ] );
}
inline void citire()
{
ifstream fin("sortaret.in");
fin >> n >> m;
for(; m; --m )
{
int x,y;
fin >> x >> y;
mat[ x ].push_back( y );
grad[ y ]++;
}
for(int i=1; i<=n; ++i )
if( grad[ i ] == 0 )
df( i );
}
inline void afisare()
{
ofstream fout("sortaret.out");
for(int i=0; i<v.size(); ++i )
fout << v[ i ] <<" ";
}
int main()
{
citire();
afisare();
}