Cod sursa(job #1819233)

Utilizator Tiberiu02Tiberiu Musat Tiberiu02 Data 30 noiembrie 2016 13:48:13
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
# include <fstream>
# include <vector>

using namespace std;

# define MAX_N 50000
vector<int> v[1 + MAX_N];
bool check[1 + MAX_N];

void print( int n, ofstream &fout )
{
    if ( check[n] )
        return;
    check[n] = true;

    fout << n << ' ';

    for ( int i : v[n] )
        print( i, fout );
}

int main()
{
    ifstream fin( "sortaret.in" );
    ofstream fout( "sortaret.out" );

    ios::sync_with_stdio( false );

    int n, m, i, a, b;

    fin >> n >> m;

    for ( i = 0; i < m; i ++ ) {
        fin >> a >> b;
        v[a].push_back( b );
    }

    for ( i = 1; i <= n; i ++ )
        print( i, fout );
    fout << endl;

    fin.close();
    fout.close();

    return 0;
}