Cod sursa(job #2174071)

Utilizator lulian23Tiganescu Iulian lulian23 Data 16 martie 2018 10:38:34
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <bits/stdc++.h>

using namespace std;

int n, m;

vector < int > v[ 50010 ];
stack < int > dx;
bitset < 50010 > viz;

void dfs(int nod){

    viz[ nod ] = 1;
    for (int i = 0; i < v[ nod ].size(); ++i)
        if (viz[ v[ nod ][ i ] ] == 0)
            dfs(v[ nod ][ i ]);
    dx.push( nod );
}

int main(){
    ios_base::sync_with_stdio( false );
    cin.tie( 0 );

    ifstream cin("sortaret.in");
    ofstream cout("sortaret.out");

    cin >> n >> m;

    for (int i = 0, x, y; i < m; ++i){
        cin >> x >> y;
        v[ x ].push_back( y );
    }

    for (int i = 1; i <= n; ++i){
        if (viz[ i ] == 0){
            dfs( i );
        while (dx.empty() == false){
            cout << dx.top() << " ";
            dx.pop();
        }
     }
   }
}