Cod sursa(job #1983945)

Utilizator Mihai_PredaPreda Mihai Dragos Mihai_Preda Data 22 mai 2017 22:40:15
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

const int nMax = 50005;

int n, m;
vector<int> vecini[nMax];
bool viz[nMax];
bool hasFather[nMax];
vector<int> rasp;

void citire()
{
    ifstream in("sortaret.in");
    in >> n >> m;
    int x, y;
    for(int i = 1; i <= m; ++i)
    {
        in >> x >> y;
        vecini[x].push_back(y);
        hasFather[y] = true;
    }
}

void Dfs(int current)
{
    viz[current] = true;
    for(auto v:vecini[current])
        if(viz[v] == false)
        {
            rasp.push_back(v);
            Dfs(v);
        }
}

void rezolvare()
{
    for(int i = 1; i <= n; ++i)
        if(hasFather[i] == false && viz[i] == false)
        {
            rasp.push_back(i);
            Dfs(i);
        }
}

void afisare()
{
    ofstream out("sortaret.out");
    for(int i = 0; i < n; ++i)
        out << rasp[i] << " ";
    out.close();
}

int main()
{
    citire();
    rezolvare();
    afisare();
    return 0;
}