Cod sursa(job #2156582)

Utilizator DawlauAndrei Blahovici Dawlau Data 8 martie 2018 20:30:07
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include<fstream>
#include<list>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
const int NMAX = 5e4 + 5;

list<int> adjList[NMAX];

int Stack[NMAX];
int nodesCnt, edgesCnt, StackLen;

inline void readData(){

    fin >> nodesCnt >> edgesCnt;

    int from, to;
    while(edgesCnt--){

        fin >> from >> to;
        adjList[from].push_back(to);
    }
}

void topoSort(int node){

    for(const auto &nextNode : adjList[node])
        topoSort(nextNode);
    Stack[++StackLen] = node;
}

inline void print(){

    while(StackLen){

        fout << Stack[StackLen] << ' ';
        --StackLen;
    }
}

int main(){

    readData();
    topoSort(1);
    print();
}