Cod sursa(job #3350257)

Utilizator marap2011Paun Mara marap2011 Data 6 aprilie 2026 18:42:48
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
#include <bits/stdc++.h>
#define pi pair<int,int>
using namespace std;

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

const int INF = 1e9 + 5 ;

vector < int > g[50005] ;
bool sel[50005] ;
stack < int > st ;

void dfs ( int nod )
{
    sel[nod] = 1 ;
    for ( auto it : g[nod] )
        if ( ! sel[it] )
            dfs ( it ) ;
    st.push(nod) ;
}

void solve ()
{
    int n , m ;
    fin >> n >> m ;
    for ( int i = 1 ; i <= m ; i ++ )
    {
        int x , y ;
        fin >> x >> y ;
        g[x].push_back(y) ;
    }
    for ( int i = 1 ; i <= n ; i ++ )
    {
        if ( ! sel[i] )
            dfs ( i ) ;
    }

    while ( ! st.empty() )
    {
        fout << st.top() << " " ;
        st.pop() ;
    }
}

int main()
{
    std :: ios_base :: sync_with_stdio ( false ) ;
    fin.tie(0) ;
    fout.tie(0) ;
    solve() ;

    return 0;
}