Cod sursa(job #2377205)

Utilizator My_Road_To_ONIAndrei Panainte My_Road_To_ONI Data 8 martie 2019 23:22:48
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>

using namespace std;

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

const int Nmax  = 50005;

vector <int> L[Nmax];
int viz[Nmax], sol[Nmax];
int n, ln;


void Read()
{
    int x, y, m;
    fin >> n >> m;
    while(m--)
    {
        fin >> x >> y;
        L[x].push_back(y);
    }
    fin.close();
}

void DFS(int k)
{
    viz[k] = 1;
    for(auto i : L[k])
        if(!viz[i]) DFS(i);
    sol[++ln] = k;
}

void Do()
{
    int i;
    for(i = 1; i <= n; i++)
        if(!viz[i]) DFS(i);
}

void Write()
{
    int i;
    for(i = ln; i >= 1; i--)
        fout << sol[i] << " ";
    fout.close();
}

int main()
{
    Read();
    Do();
    Write();
    return 0;
}