Cod sursa(job #2014113)

Utilizator MihaelaCismaruMihaela Cismaru MihaelaCismaru Data 22 august 2017 21:52:34
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include<fstream>
#include<vector>
using namespace std;
ifstream in("sortaret.in");
ofstream out("sortaret.out");
int sol[50001],k,hz[50001],a,b,n,m;
vector<int>h[50001];
void dfs( int nod ){
    for( int i = 0; i < h[nod].size(); i ++ ){
        if( hz[ h[nod][i] ] == 0 ){
            hz[h[nod][i]] = 1;
            dfs( h[nod][i] );
            sol[++k] = h[nod][i];
        }
    }
    return;
}
int main(){
    in >> n >> m;
    for( int i = 1; i <= m; i ++ ){
        in >> a >> b;
        h[a].push_back(b);
    }
    for( int i = 1; i <= n; i ++ ){
        if( hz[i] == 0 ){
            hz[i] = 1;
            dfs( i );
            sol[++k] = i;
        }
    }
    for( int i = k; i >= 1; i -- ){
        out<<sol[i]<<" ";
    }
    return 0;
}