Cod sursa(job #2658396)

Utilizator ioanapintilie07Pintilie Ioana ioanapintilie07 Data 13 octombrie 2020 20:56:34
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using  namespace std;

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

const int maxi = 100001;
vector<int> a[maxi];
stack<int> stiva;
int vizitat[maxi];

void st(int nod) {
    vizitat[nod] = 1;
    for(auto i : a[nod])
        if(vizitat[i] == 0)
            st(i);
    stiva.push(nod);
}

void sortareTopologica(int n) {
    int i;
    for(i = 1; i <= n; ++i)
        if(vizitat[i] == 0)
            st(i);
    while(!stiva.empty()) {
        fout << stiva.top() << " ";
        stiva.pop();
    }
}

int main() {
    int n, m, i, x, y;
    fin >> n >> m;
    for(i = 1; i <= m; ++i) {
        fin >> x >> y;
        a[x].push_back(y);
    }
    sortareTopologica(n);
    return 0;
}