Cod sursa(job #2361357)

Utilizator qwerty1234qwerty qwerty1234 Data 2 martie 2019 14:56:03
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <bits/stdc++.h>

using namespace std;

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

const int Nmax = 50005;

vector < int > L[Nmax];

int ans[Nmax] , n , d , m;

bool viz[Nmax];

void DFS(int nod)
{
    viz[nod] = true;
    for(auto it : L[nod])
        if(!viz[it])
            DFS(it);
    ans[++d] = nod;
}


int main()
{
    int x , y;
    fin >> n >> m;
    for(int i = 1 ; i <= m ; i++)
        fin >> x >> y , L[x].push_back(y);
    for(int i = 1 ; i <= n ; i++)
        if(!viz[i])
            DFS(i);
    for(int i = d ; i >= 1 ; i--)
        fout << ans[i] << " ";
    fout << "\n";
    fin.close();
    fout.close();
    return 0;
}