Cod sursa(job #2175535)

Utilizator andreimuthMuth Andrei andreimuth Data 16 martie 2018 17:40:54
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
#include <fstream>
#include <vector>
#define nmax 50001
using namespace std;
ifstream fin ("sortaret.in");
ofstream fout ("sortaret.out");
vector < int > g[nmax], sol;
int n, m, i, x, y;
int viz[nmax];

void dfs (int nod)
{
    viz[nod] = 1;
    //sol.push_back (nod);
    int i;
    for (i = 0; i < g[nod].size (); i++)
    {
        if (!viz[g[nod][i]])
            dfs (g[nod][i]);
    }
    sol.push_back (nod);

}

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

    for (i = 1; i <= n; i++)
    {
        if (viz[i] == 0)
            dfs (i);
    }

   for (i = sol.size () - 1; i >= 0; i--)
    fout << sol[i] << " ";

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