Cod sursa(job #2940722)

Utilizator carpstefanCarp Stefan carpstefan Data 16 noiembrie 2022 11:23:45
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
int n, m;
vector <int> a[50005];
bitset <50005> viz;

///O(n + m)
void Dfs(int x)
{
    viz[x] = 1;
    for (int w : a[x])
        if (viz[w] == 0)
        Dfs(w);
    st[++top] = x;
}
int main()
{
    int i , x , y;
    fin >> n >> m;
    for (i = 1; i <= m; i++)
    {
        fin >> x >> y;
        a[x].push_back(y);
    }

    for (i = 1; i <= n; i++)
        if (viz[i] == 0)
            Dfs(i);

    for (i = n; i >= 1; i--)
        fout << st[i] << " ";

    return 0;
}