Cod sursa(job #2485492)

Utilizator vali_27Bojici Valentin vali_27 Data 1 noiembrie 2019 17:55:53
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <bits/stdc++.h>
using namespace std;

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

vector <vector <int > >la;
list <int> topo;
vector <bool> used;
int n,m;

void citire()
{
    int x,y;
    fin >> n >> m;
    la.resize(n+1);
    used.resize(n+1);
    for(int i=1;i<=m;++i)
    {
        fin >> x >> y;
        la[x].push_back(y);
    }
}

void dfs(int x)
{
    used[x] = 1;
    for(auto i : la[x])
        if(!used[i])dfs(i);

    topo.push_front(x);
}

int main()
{
    citire();
    for(int i=1;i<=n;++i)
        if(!used[i])dfs(i);

    for(auto i : topo)fout << i << ' ';

}