Cod sursa(job #2484023)

Utilizator XXMihaiXX969Gherghinescu Mihai Andrei XXMihaiXX969 Data 30 octombrie 2019 17:15:18
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

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

const int DIM = 50007;

vector <int>  SET, REZ, L[DIM];
bool viz[DIM];



void topsort(int x)
{
    SET.push_back(x);
    viz[x] = true;

    for(int i = 0 ; i < L[x].size() ; i++)
        if(viz[L[x][i]] == false)
         topsort(L[x][i]);

        REZ.push_back(x);
}

int main()
{
    int n,m;
    in >> n >> m;

    for(int i = 1; i <= m; i++)
    {
        int x, y;
        in >> x >> y;

        L[x].push_back(y);
    }

    for(int j = 1; j <= n; j++)
        if(viz[j] == false)
            topsort(j);
    for( int i = REZ.size() - 1 ;i >= 0; i--)
        out << REZ[i] <<" ";

    return 0;
}