Cod sursa(job #2014256)

Utilizator whitewolfJon Snow whitewolf Data 23 august 2017 12:41:55
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>

using namespace std;

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

const int n_max = 50005;
int n, m, top, ans[n_max];
vector <int> L[n_max];
bool used[n_max];

inline void Read() {
    fin >> n >> m;
    int x, y, i;
    for (i = 1; i <= m; i++) {
        fin >> x >> y;
        L[x].push_back(y);
    }
    fin.close();
}

void Sorting(int nod) {
    used[nod] = 1;
    for (auto i : L[nod])
        if (!used[i])
            Sorting(i);
    ans[++top] = nod;
}

int main() {
    Read();
    for (int i = 1; i <= n; i++)
        if (!used[i]) Sorting(i);
    do {
        fout << ans[top] << " ";
    } while (--top);
    fout.close();
    return 0;
}