Cod sursa(job #2679240)

Utilizator AndreiAlexandru2k3Ciucan Andrei Alexandru AndreiAlexandru2k3 Data 30 noiembrie 2020 00:05:17
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream f("sortaret.in");
ofstream g("sortaret.out");

const int NMAX = 50000;

int N, M;
vector<int>G[NMAX + 1], L;
bool viz[NMAX + 1];

void citire()
{
    int x, y;
    f >> N >> M;
    while(M--)
    {
        f >> x >> y;
        G[x].push_back(y);
    }
}

void DFS(int x)
{
    viz[x] = 1;
    for(int &y : G[x])
        if(!viz[y])
            DFS(y);
    L.push_back(x);
}

int main()
{
    citire();
    for(int i=1;i<=N;i++)
        if(!viz[i])
        DFS(i);
    for(int i=L.size()-1;i>=0;i--)
        g<<L[i]<<' ';
    return 0;
}