Cod sursa(job #2637551)

Utilizator Andrei-27Arhire Andrei Andrei-27 Data 23 iulie 2020 15:47:39
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
/*
 *  Created by Andrei Arhire 23/07/2020
 *  Copyright © 2020 Andrei Arhire. All rights reserved.
 */
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define lll long long
#define llf __float128
#define lld long double
using namespace std;
const int NR = 5e4 + 10  ,oo = 1e9 + 10, MOD = 1e9 + 7 ;
const long double pi = 2*acos(0.0);


ifstream in ("sortaret.in") ;
ofstream out ("sortaret.out") ;
int n , m ;
vector < int > v [ NR ] , sol ;
bool viz [ NR ] ;

void dfs ( int nod )    {
    viz [ nod ] = 1 ;
    for ( auto it : v [ nod ] ) {
        if ( !viz [ it ] )  {
            dfs( it ) ;
        }
    }
    sol.pb( nod ) ;
}

signed main () {
    int x , y , i ;
    ios::sync_with_stdio(0);
    in.tie(0);
    out.tie(0) ;
    in >> n >> m ;
    while ( m -- )  {
        in >> x >> y ;
        v [ x ].pb( y ) ;
    }
    for ( i = 1 ; i <= n ; ++ i )   {
        if ( !viz [ i ] )
            dfs ( i ) ;
    }
    for ( i = n - 1 ; i >= 0 ; -- i )
        out << sol [ i ] << ' ' ;
    out << '\n' ;
    return 0 ;
}