Cod sursa(job #3141332)

Utilizator NToniBoSSNicolae Tonitza NToniBoSS Data 13 iulie 2023 17:50:19
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <vector>
#include <cstring>
#include <iterator>
#include <queue>
#define LIM 1<<17
/// TONI BO$$ was here
/// #MLC

using namespace std;
char BUF[LIM];
int poz;
int f[50001],rad[50001];
vector <int> L[50001];
vector <int> TONI_BO$$;

inline char getChar(){
    poz++;
    if(poz>=LIM){
        fread(BUF,LIM,1,stdin);
        poz=0;
    }
    return BUF[poz];
}

inline int getNr(){
    int r=0, semn=1;
    char ch=getChar();
    while(isdigit(ch)==0 && ch!='-') ch=getChar();
    if(ch=='-'){
        semn=-1;
        ch=getChar();
    }
    while(isdigit(ch)!=0){
        r=r*10+semn*(ch-'0');
        ch=getChar();
    }
    return r;
}

int main()
{
    int i,n,m,a,b,k;
    freopen("sortaret.in","r",stdin);
    freopen("sortaret.out","w",stdout);
    n=getNr();
    m=getNr();
    while(m--)
    {
        a=getNr();
        b=getNr();
        L[a].push_back(b);
        rad[b]++;
    }
    queue <int> q;
    for(k=1; k<=n; k++)
        if(!rad[k])
            q.push(k);
    while(!q.empty())
    {
        int x = q.front();
        q.pop();
        printf("%d ", x);
        for(auto it : L[x]){
            rad[it]--;
            if(!rad[it])
                q.push(it);
        }
    }

    return 0;
}