Cod sursa(job #2878177)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 25 martie 2022 22:48:39
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

const int maxN = 50005;
int n, m;
bool used[maxN];
vector <int> G[maxN], sort_top;

void dfs(int nod)
{
    used[nod] = 1;
    for(int vecin : G[nod])
        if(!used[vecin])
            dfs(vecin);
    sort_top.push_back(nod);
}

int main()
{
    fin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        int x, y;
        fin >> x >> y;
        G[x].push_back(y);
    }
    for(int i = 1; i <= n; i++)
       if(!used[i])
            dfs(i);
    for(int i = n - 1; i >= 0; i--)
        fout << sort_top[i] << ' ';
    return 0;
}