Cod sursa(job #1646116)

Utilizator tidehyonBosoi Bogdan tidehyon Data 10 martie 2016 15:08:55
Problema Sortare topologica Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

ifstream in("sortaret.in");
ofstream out("sortaret.out");
vector < int > M[50005];
int Topo[50005], viz[50005];
int n, m, k;
void Citire()
{
    in >> n >> m;
    short x, y;
    for(short i = 1; i <= m; i++)
    {
        in >> x >> y;
        M[x].push_back(y);
    }
}

void DFS(int nod)
{
    viz[nod] = 1;
    for(short i = 0; i < M[nod].size(); i++)
        if(viz[M[nod][i]] == 0)
            DFS(M[nod][i]);
    Topo[++k] = nod;

}

void Rezolva()
{
    int i;
    k = 0;
    for(i = 1; i <= n; i++)
        if(viz[i] == 0)
            DFS(i);
}

int main()
{
    Citire();
    Rezolva();
    for(short i = k; i > 0; i--)
            out << Topo[i] << " ";
    return 0;
}