Cod sursa(job #1802013)

Utilizator radu.leonardoThe Doctor radu.leonardo Data 9 noiembrie 2016 19:54:15
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include<fstream>
#include<iostream>
#define mmax 100010
#define nmax 50005
#define buff_size 250010
using namespace std;
FILE*f=freopen("sortaret.in","r",stdin);
FILE*g=freopen("sortaret.out","w",stdout);
struct nod{int inf;nod *urm;} *L[nmax];
bool viz[nmax];int n,m;

char buff[buff_size],buffw[buff_size]="";int pos=0,posw=0;

inline void read(int &nr)
{
    while(buff[pos] < '0' || buff[pos] > '9') if(++pos == buff_size) fread(buff, buff_size, buff_size, stdin), pos = 0;
    nr = 0;
    while('0' <= buff[pos] && buff[pos] <= '9')
    {
        nr = nr * 10 + buff[pos] - '0';
        if(++pos == buff_size) fread(buff,buff_size, buff_size, stdin), pos = 0;
    }
}


inline void add(int x,int y)
{nod *nou;
 nou=new nod;
 nou->inf=x;
 nou->urm=L[y];
 L[y]=nou;
}

inline void dfs(int x)
{nod *p;
 viz[x]=1;
  for(p=L[x];p;p=p->urm)
       if(!viz[p->inf])
            dfs(p->inf);
posw += sprintf(buffw + posw,"%d ", x);

}

int main()
{   int i,x,y;
    fread(buff,buff_size, buff_size, stdin);
    read(n);read(m);
    for(i=1;i<=m;i++) read(x),read(y),add(x,y);
    for(i=1;i<=n;i++)   if(!viz[i])    dfs(i);
    fwrite(buffw, 1, posw, stdout);

}