Cod sursa(job #2152106)

Utilizator CammieCamelia Lazar Cammie Data 5 martie 2018 10:51:29
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>
#include <vector>

#define MAXN 50005

using namespace std;

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

int postordine[MAXN], N, M, viz[MAXN], nn;
vector <int> graph[MAXN];

inline void Read() {
    int x, y;

    fin >> N >> M;

    for (int i = 1; i <= M; i++) {
        fin >> x >> y;

        graph[x].push_back(y);
    }
}

void DFS(int node) {
    viz[node] = 1;

    for (auto x : graph[node]) {
        if (!viz[x]) {
            DFS(x);
        }
    }

    postordine[++nn] = node;
}

inline void Solve() {
    DFS(1);

    for (int i = nn; i; i--)
        fout << postordine[i] << " ";
}

int main () {
    Read();
    Solve();

    fin.close(); fout.close(); return 0;
}