Cod sursa(job #2968464)

Utilizator andu2006Alexandru Gheorghies andu2006 Data 21 ianuarie 2023 09:13:24
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include<bits/stdc++.h>

using namespace std;
ifstream fin("sortareset.in");
ofstream fout("sortareset.out");
typedef long long ll;
const ll NMAX=5e4+5;
vector<ll> edg[NMAX];
ll deg[NMAX];
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ll n,m;
    fin>>n>>m;
    for(ll i=0;i<m;i++){
        ll u,v;
        fin>>u>>v;
        edg[u].push_back(v);
        ++deg[v];
    }
    queue<ll> q;
    for(ll i=1;i<=n;i++){
        if(deg[i]==0)
            q.push(i);
    }
    while(!q.empty()){
        fout<<q.front()<<' ';
        for(auto it : edg[q.front()]){
            --deg[it];
            if(deg[it]==0) q.push(it);
        }
        q.pop();
    }
    return 0;
}