Cod sursa(job #2020838)

Utilizator UWantMyNameGheorghe Vlad Camil UWantMyName Data 11 septembrie 2017 19:45:42
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <bits/stdc++.h>
#define in "sortaret.in"
#define out "sortaret.out"
using namespace std;
ifstream fin(in);
ofstream fout(out);

int n,m;
vector <int> L[50003];
int sol[50003],p;
int viz[50003];

void Citire()
{
    int i,x,y;

    fin >> n >> m;
    for (i = 1; i <= m; i++)
    {
        fin >> x >> y;
        L[x].push_back(y);
    }
}

void DFS_TF(int k)
{
    viz[k] = 1;
    for (auto i : L[k])
        if (viz[i] == 0) DFS_TF(i);
    sol[++p] = k;
}

void Rezolvare()
{
    int i;

    for (i = 1; i <= n; i++)
        if (viz[i] == 0) DFS_TF(i);
    for (i = n; i >= 1; i--)
        fout << sol[i] << " ";
    fout << "\n";
}

int main()
{
    Citire();
    Rezolvare();

    fin.close();
    fout.close();
    return 0;
}