Cod sursa(job #2189234)

Utilizator NToniBoSSNicolae Tonitza NToniBoSS Data 27 martie 2018 20:58:54
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.39 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],ap[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;
}

void dfs(int k)
{
    f[k]=1;
    for(auto it : L[k])
        if(!f[it])
            dfs(it);
    TONI_BO$$.push_back(k);
}

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]++;
        ap[a]=1;
        ap[b]=1;
    }
    for(k=1; k<=n; k++)
        if(!rad[k] && ap[k])
            dfs(k);
    while(!TONI_BO$$.empty())
    {
        printf("%d ",TONI_BO$$.back());
        TONI_BO$$.pop_back();
    }

    return 0;
}