Cod sursa(job #3142435)

Utilizator AlexPlesescuAlexPlesescu AlexPlesescu Data 21 iulie 2023 11:42:19
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

vector<int> gf[100001], stiva;
int viz[100001], n, m;

void topsort(int x) {
    viz[x] = 1;
    for (int i : gf[x])
        if (!viz[i])
            topsort(i);
    stiva.push_back(x);
}

int main()
{
    fin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int x, y;
        fin >> x >> y;
        gf[x].push_back(y);
    }
    for (int i = 1; i <= n; i++)
        if (!viz[i])
            topsort(i);
    int l = stiva.size();
    for (int i = l - 1; i >= 0; i--)
        fout << stiva[i] << " ";
    return 0;
}