Cod sursa(job #2132874)

Utilizator CronosClausCarare Claudiu CronosClaus Data 16 februarie 2018 10:32:51
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>

using namespace std;

const int mxn = 50 * 1000 + 10;

vector< int > v[ mxn ];
stack< int > s;

int n, m;

bool viz[ mxn ];

void dfs(int nod){
    viz[ nod ] = 1;
    for(auto it: v[ nod ])
        if(viz[ it ] == 0)
            dfs( it );
    s.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(!s.empty()){
        cout<< s.top() << ' ';
        s.pop();
    }
    return 0;
}