Cod sursa(job #1728052)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 12 iulie 2016 09:41:44
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <fstream>
#include <vector>
#include <stack>

using namespace std;

ifstream f("sortaret.in");
ofstream g("sortaret.out");

bool viz[50005];
int sol[50005], k;
int i, x, y, n, m;
vector <int> ls[50005];
stack <int> stiva;

void df(int x) {
    viz[x] = 1;
    int l = ls[x].size(), i;
    for (i = 0; i < l; i++)
        if (!viz[ ls[x][i] ])
            df(ls[x][i]);
    stiva.push(x);
}

int main() {
    f >> n >> m;
    for (i = 1; i <= m; i++) {
        f >> x >> y;
        ls[x].push_back(y);
    }
    for (i = 1; i <= n; i++)
        if (viz[i] == 0)
            df(i);

    while (!stiva.empty()) {
        g << stiva.top() << ' ';
        stiva.pop();
    }
    return 0;
}