Cod sursa(job #2872618)

Utilizator rapidu36Victor Manz rapidu36 Data 17 martie 2022 16:04:06
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
#include <algorithm>

using namespace std;

const int N = 50000;

vector <int> a[N+1], s;
bitset <N+1> viz;

void dfs(int x)
{
    viz[x] = 1;
    for (auto y: a[x])
    {
        if (!viz[y])
        {
            dfs(y);
        }
    }
    s.push_back(x);
}

int main()
{
    ifstream in("sortaret.in");
    ofstream out("sortaret.out");
    int m, n;
    in >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int x, y;
        in >> x >> y;
        a[x].push_back(y);
    }
    for (int i = 1; i <= n; i++)
    {
        if (!viz[i])
        {
            dfs(i);
        }
    }
    reverse(s.begin(), s.end());
    for (auto x: s)
    {
        out << x << " ";
    }
    in.close();
    out.close();
    return 0;
}