Cod sursa(job #222657)

Utilizator adrian69adrian horia adrian69 Data 24 noiembrie 2008 09:16:27
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <iostream>
#include<stdio.h>
using namespace std;
#define maxn 50100
int m,n;
typedef struct nod{
    int vf;
    nod *urm;
}*PNOD,NOD;
PNOD L[maxn];
PNOD adresa;
int color[maxn];
void Push( int nod )  
 {  
      PNOD p = new NOD;  
      p->vf = nod;  
      p->urm = adresa;  
      adresa=p;  
 }  
void df(int nod)
{
    color[nod]=1;
    for ( PNOD p = L[nod]; p; p = p->urm )  
          if ( color[p->vf] == 0 )  
               df( p->vf );  
      color[nod] = 2;  
      Push( nod );  
    }
void s_top()
{int i;
 for(i=1;i<=n;++i)
  if(color[i]==0)
   df(i);
    }
void add(int i,int j)
{PNOD p=new NOD;
 p->vf=j;
 p->urm=L[i];
 L[i]=p;
    }
void read()
{freopen("sortaret.in","r",stdin);
 scanf("%d %d\n",&n,&m);
 int x,y;
 for(;m>0;m--)
 {scanf("%d %d",&x,&y);
  add(x,y);
        } 
}
void write()
{freopen("sortaret.out","w",stdout);
 for(PNOD p=adresa;p;p=p->urm)
   printf("%d ",p->vf);
        
}
int main()
{read();
 s_top();
 write(); 
return 0;
}