Cod sursa(job #2564714)

Utilizator theo2003Theodor Negrescu theo2003 Data 2 martie 2020 09:39:14
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <vector>
#include <fstream>
using namespace std;
ifstream cin("sortaret.in");
ofstream cout("sortaret.out");
int has[50001], i = 0;
vector<int> link[50001], v, out;
int main(){
    int n, m;
    cin>>n>>m;
    for(int x = 1, a, b;x<=m;x++){
        cin>>a>>b;
        has[b]++;
        link[a].push_back(b);
    }
    for(int x = 1;x<=n;x++){
        if(!has[x]){
            v.push_back(x);
        }
    }
    while(i < v.size()){
        out.push_back(v[i]);
        for(int x : link[v[i]]){
            has[x]--;
            if(!has[x])
                v.push_back(x);
        }
        i++;
    }
    for(int x = 0;x<out.size();x++)
        cout<<out[x]<<' ';
    return 0;
}