Cod sursa(job #2444542)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 31 iulie 2019 18:01:15
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 <int> a[50005], r;
int f[50005], anss;

void dfs(int k)
{
    f[k] = 1;
    for(auto v : a[k])
        if(!f[v]) dfs(v);
    r.push_back(k);
}
int main()
{
    ios::sync_with_stdio(false);
    fin.tie(0);
    int n, m;
    fin >> n >> m;
    for(int i = 1; i <= m; ++i) {
        int x, y;
        fin >> x >> y;
        a[x].push_back(y);
    }
    for(int i = 1; i <= n; ++i)
        if(!f[i]) dfs(i);
    reverse(r.begin(), r.end());
    for(auto v : r) fout << v << " ";
    return 0;
}