Cod sursa(job #2313890)

Utilizator AndreiLunguLungu Andrei Sebastian AndreiLungu Data 7 ianuarie 2019 16:31:01
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
int n , m , pred[50004] , viz[50004] , k;
vector <int> L[50004];
void Citire()
{
    int i , x , y;
    fin >> n >> m;
    for(i = 1; i <= m; i++)
    {
        fin >> x >> y;
        L[x].push_back(y);
    }
    fin.close();
}
void DFS(int nod)
{
    viz[nod] = 1;
    for(auto i : L[nod])
        if(viz[i] == 0)
            DFS(i);
    pred[++k] = nod;
}
int main()
{
    int i;
    Citire();
    for(i = 1; i <= n; i++)
        if(viz[i] == 0)
            DFS(i);
    for(i = n; i >= 1; i--)
        fout << pred[i] << " ";
    fout.close();
    return 0;
}