Cod sursa(job #2189153)

Utilizator NToniBoSSNicolae Tonitza NToniBoSS Data 27 martie 2018 19:31:17
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <vector>
#include <cstring>
#include <iterator>
#define LIM 1<<17
/// TONI BO$$ was here
/// #MLC

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

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)
{
    printf("%d ",k);
    f[k]=1;
    for(auto it : L[k])
        if(!f[it])
            dfs(it);
}

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);
        f[b]++;
    }
    for(k=1; k<=n && f[k]; k++);
    memset(f,0,sizeof f);
    dfs(k);

    return 0;
}