Cod sursa(job #3002271)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 14 martie 2023 16:43:40
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>
#include <vector>
using namespace std;

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

const int MAX = 5e4 + 1;

vector <int> g[MAX];

int topo[MAX] , in , n , m , x , y;

bool viz[MAX];

void dfs( int x ){

    viz[x] = 1;

    for(auto it : g[x]){

        if(!viz[it]){

            dfs(it);
        }
    }

    topo[in--] = x;
}

int main(){

    cin >> n >> m;

    in = n;

    while(m--){

        cin >> x >> y;

        g[x].push_back(y);
    }

    for(int i = 1 ; i <= n ; i++){

        if(!viz[i]) dfs(i);
    }

    for(int i = 1 ; i <= n ; i++){

        cout << topo[i] << ' ';
    }

    return 0;
}