Cod sursa(job #2809645)

Utilizator teomarsTeodora Sintea teomars Data 27 noiembrie 2021 12:13:56
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <fstream>
#include <vector>

using namespace std;

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

int n, m, s[100005], nr;
bool viz[100005];

vector <int> g[100005];

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

void curent(int x){
    viz[x] = 1;
    for(auto &a: g[x])
        if(!viz[a])
            curent(a);
    s[++nr] = x;
}

void rezolvare(){
    for(int i = 1; i <= n; i++)
        if(!viz[i])
            curent(i);
}

void afisare(){
    for(int i = n; i >= 1; i--)
        fout << s[i] << ' ';
}

int main()
{
    citire();
    rezolvare();
    afisare();
    return 0;
}