Cod sursa(job #2661645)

Utilizator speedypleathGheorghe Andrei speedypleath Data 22 octombrie 2020 14:16:04
Problema Sortare topologica Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
using namespace std;
ifstream in("sortaret.in");
ofstream out("sortaret.out");
int n,m,a,b;
map<int, unordered_set<int>> graf;
bool viz[50005] = {0};
stack<int> s;
void top(int x)
{
    viz[x] = 1;
    for(auto i:graf[x])
        if(!viz[i])
            top(i);
    s.push(x);
}
int main()
{
    in>>n>>m;
    for(int i=0;i<m;i++){
        in>>a>>b;
        graf[a].insert(b);
    }
    for(int i=1;i<=n;i++)
        if(!viz[i])
            top(i);
    for(int i=0;i<n;i++){
        out<<s.top()<<' ';
        s.pop();
    }
    return 0;
}