Cod sursa(job #2661655)

Utilizator andreea.bucurBucur Andreea andreea.bucur Data 22 octombrie 2020 14:29:40
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream fin ("sortaret.in");
ofstream fout ("sortaret.out");
struct nod
{
    int info;
    nod * urm;
}*p[50001];
int n;
nod * adresa;
int color[50001];
void add(int x, int y)
{
    nod * aux = new nod;
    aux->info=y;
    aux->urm=p[x];
    p[x]=aux;
}
void DFS(int node)
{
    //cout<<"node= "<<node<<"\n";
    color[node]=1;
    for(nod*t= p[node]; t; t=t->urm)
    {
        if(color[t->info]==0) DFS(t->info);
    }

    //if(adresa) cout<<"adresa->info= "<<adresa->info<<"\n";
    //else cout<<"adresa->info= NULL\n";
    //cout<<"afis->info= "<<node<<"\n";
    nod * afis = new nod;
    afis->info=node;
    afis->urm=adresa;
    adresa=afis;
}
void S_Topologica()
{
    int i;
    for(i=1; i<=n; i++)
    {
        if(color[i]==0) DFS(i);
    }
}
void Write()
{
    for(nod * t = adresa; t; t=t->urm)
    {
        fout<<t->info<<' ';
    }
}
int main()
{
    int m, i, x, y;
    fin>>n>>m;
    for(i=1; i<=m; i++)
    {
        fin>>x>>y;
        //exista arc de la nodul x la nodul y, deci x apare inaintea lui y
        add(x, y); //il adaug pe y la cei la care duce x
    }
    S_Topologica();
    Write();
}