Cod sursa(job #1976877)

Utilizator blatulInstitutul de Arta Gastronomica blatul Data 4 mai 2017 14:12:48
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <bits/stdc++.h>
using namespace std;

#define NMAX 50002

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

vector<int> graf[NMAX];
stack<int> stiva;

bool viz[NMAX];

void DFS(int nod) {

    viz[nod] = true;

    for (auto adj: graf[nod])
        if (!viz[adj])
            DFS(adj);

    stiva.push(nod);
}

int main() {

    int N, M;

    fin >> N >> M;

    int x, y;
    while (M--) {

        fin >> x >> y;

        graf[x].push_back(y);
    }

    for (int i = 1; i <= N; ++i)
        if (!viz[i])
            DFS(i);

    while (!stiva.empty()) {

        fout << stiva.top() << " ";

        stiva.pop();
    }

    return 0;
}