Cod sursa(job #2539970)

Utilizator CharacterMeCharacter Me CharacterMe Data 6 februarie 2020 16:31:42
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <bits/stdc++.h>
std::ifstream fin("sortaret.in");
std::ofstream fout("sortaret.out");
int n, m;
int weigth[50005], sol[50005];
std::vector<int> graph[50005];
std::queue<int> q;
int main()
{
    fin>>n>>m;
    while(m--){
        int x, y;
        fin>>x>>y;
        graph[x].push_back(y);
        ++weigth[y];
    }
    for(int i=1; i<=n; ++i) if(!weigth[i]) q.push(i);
    n=0;
    while(!q.empty()){
        int now=q.front(); q.pop();
        sol[++n]=now;
        for(auto next:graph[now]){
            --weigth[next];
            if(!weigth[next]) q.push(next);
        }
    }
    for(int i=1; i<=n; ++i) fout<<sol[i]<<" ";
    return 0;
}