Cod sursa(job #1649702)

Utilizator tudormaximTudor Maxim tudormaxim Data 11 martie 2016 14:43:20
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;

ifstream fin ("sortaret.in");
ofstream fout ("sortaret.out");

const int nmax = 5e4+5;
vector <int> g[nmax], sol;
bitset <nmax> viz;

void dfs(int dad)
{
    vector <int> :: iterator son;
    viz[dad]=true;
    for(son=g[dad].begin(); son!=g[dad].end(); son++)
        if(viz[*son]==false) dfs(*son);
    sol.push_back(dad);
}
int main()
{
    ios_base::sync_with_stdio(false);
    int n, m, x, y, i;
    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]==false) dfs(i);
    for(i=n-1; i>=0; i--)
        fout << sol[i] << " ";
    fin.close();
    fout.close();
    return 0;
}