Cod sursa(job #2175482)

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

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

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 = 1; i <= k; i++)
        fout << sol[i] << " ";

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