Cod sursa(job #1801977)

Utilizator radu.leonardoThe Doctor radu.leonardo Data 9 noiembrie 2016 19:09:17
Problema Sortare topologica Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include<fstream>
#include<iostream>
#define mmax 100010
#define nmax 50005
#define buff_size 400001
using namespace std;
FILE*f=freopen("sortaret.in","r",stdin);
FILE*g=freopen("sortaret.out","w",stdout);
struct nod{int inf;int v[100];int lg;} 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,1, stdin), pos = 0;
    nr = 0;
    while('0' <= buff[pos] && buff[pos] <= '9')
    {
        nr = nr * 10 + buff[pos] - '0';
        if(++pos == buff_size) fread(buff, 1, buff_size, stdin), pos = 0;
    }
}


inline void add(int x,int y)
{ L[y].lg++;L[y].v[L[y].lg]=x;

}

inline void dfs(int x)
{int p,nr=L[x].lg;
 viz[x]=1;
  for(p=1;p<=nr;p++)
       if(!viz[L[x].v[p]])
            dfs(L[x].v[p]);
posw += sprintf(buffw + posw,"%d ", x);

}

int main()
{   int i,x,y;
    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, posw, 1, stdout);

}