Cod sursa(job #2441097)

Utilizator AlexandruGabrielAliciuc Alexandru AlexandruGabriel Data 19 iulie 2019 22:09:46
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>
#define N 50005

using namespace std;

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

int n, m, a, b;
bool gasit[N];
vector <int> L[N];
list <int> ans;

void DFS(int nod)
{
    gasit[nod] = 1;
    for (auto &next_nod : L[nod])
    {
        if (!gasit[next_nod])
            DFS(next_nod);
    }

    ans.push_front(nod);
}

int main()
{
    fin >> n >> m;
    while (m--)
    {
        fin >> a >> b;
        L[a].push_back(b);
    }

    for (int i = 1; i <= n; i++)
    {
        if (!gasit[i])
            DFS(i);
    }

    for (auto &it : ans)
        fout << it << " ";
    return 0;
}