Cod sursa(job #1646147)

Utilizator tidehyonBosoi Bogdan tidehyon Data 10 martie 2016 15:14:17
Problema Sortare topologica Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 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  m, k ,n;
void Citire()
{
    in >> n >> m;
    int 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] << " ";
    in.close();
    out.close();
    return 0;
}