Cod sursa(job #2905983)

Utilizator andreeat27Andreea Taclit andreeat27 Data 24 mai 2022 18:26:18
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

stack<int>stiva;
bool viz[50001];
vector <int> a[50001];

void dfs(int x)
{
    viz[x]=true;
    for (auto y:a[x])
    {
        if (!viz[y])
            dfs(y);
    }
    stiva.push(x);
}

int main()
{
    ifstream fin("sortaret.in");
    ofstream fout("sortaret.out");
    int n, m;
    fin >> n >> m;
    for (int i = 0; i < m; i ++)
    {
        int x, y;
        fin >> x >> y;
        a[x].push_back(y);
    }

    for (int i=1;i<=n;i++)
        if (!viz[i])
            dfs(i);

    while (!stiva.empty())
    {
        fout<<stiva.top()<<" ";
        stiva.pop();
    }

    fin.close();
    fout.close();
    return 0;
}