Cod sursa(job #2568954)

Utilizator ikogamesIon Ceaun ikogames Data 4 martie 2020 10:35:59
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda r3capitusulare Marime 0.7 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("sortaret.in");
ofstream fout ("sortaret.out");
int n, m;
vector <int> h[50001];
int sol[50001], len;
bool viz[50001];
void Read()
{
    int x, y;
    fin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        fin >> x >> y;
        h[x].push_back(y);
    }
}

void DFS(int cNode)
{
    viz[cNode] = true;
    for(auto w : h[cNode])
        if(viz[w] == false)
            DFS(w);
    sol[++len] = cNode;
}

void Print()
{
    for(int i = len; i >= 1; i--)
        fout << sol[i] << " ";
}
int main()
{
    Read();
    for(int i = 1; i <= n; i++)
        if(viz[i] == false) DFS(i);
    Print();
    return 0;
}