Cod sursa(job #2394281)

Utilizator AndreiTudorSpiruAndrei Spiru AndreiTudorSpiru Data 1 aprilie 2019 15:07:09
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
#include <fstream>

using namespace std;
ifstream f("sortaret.in");
ofstream g("sortaret.out");
int n,m,fv[50005];
struct nod
{
    int vf;
    nod *urm;
};
nod *l[50005];
nod *adresa;
void read()
{

    int i,x,y;
    for(i=1;i<=m;i++)
    {
        f>>x>>y;
        nod *p = new nod;
        p->vf = y;
        p->urm = l[x];
        l[x] = p;
    }
}
void push(int i)
{
  nod *p=new nod;
  p->vf=i;
  p->urm=adresa;
  adresa=p;
}
void df(int i)
{
  fv[i]=1;
  for(nod *p=l[i];p!=NULL;p=p->urm)
  {
      if(fv[p->vf]==0)
      {
          df(p->vf);
      }
  }
  push(i);
}
void solve()
{   int i;
    for(i=1;i<=n;i++)
    {
        if(fv[i]==0)
            df(i);
    }
}


int main()
{
    f>>n>>m;
    int i;
    for(i=1;i<=n;i++)
    {
       l[i]=NULL;
    }
    adresa=NULL;
    read();
    solve();
     while(adresa!=NULL)
     {
         g<<adresa->vf<<" ";
         adresa=adresa->urm;
     }


    return 0;
}