Cod sursa(job #2458496)

Utilizator LeperBearMicu Alexandru LeperBear Data 20 septembrie 2019 19:08:49
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include <fstream>

using namespace std;

ifstream f("sortaret.in");
ofstream g("sortaret.out");

int N,M,rez[50000],i;
bool a[50001][50001],viz[50001];

void sorttop(int ns){
    viz[ns]=1;
    rez[i++]=ns;
    for (int j=1;j<=N;j++)
        if (a[ns][j] && !viz[j]){
            sorttop(j);
        }
    while (viz[ns]==1 && ns<=N) ns++;
    if (ns!=N+1) sorttop(ns);
}

int main()
{
    f<<N<<M;
    for (int j=1;j<=M;j++){
        int x1,x2;
        f>>x1>>x2;
        a[x1][x2]=1;
    }
    sorttop(1);
    for (int j=0;j<i;j++) g<<rez[j]<<" ";
    return 0;
}