Cod sursa(job #1843342)

Utilizator remus88Neatu Remus Mihai remus88 Data 8 ianuarie 2017 17:02:12
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#define Nmax 100009

using namespace std;
ifstream f("sortaret.in");
ofstream g("sortaret.out");

int n,m,s,x,y;
bitset <Nmax> viz;
vector <int> G[Nmax],sol;

void Dfs(int node)
{
    viz[node]=1;
    for (auto x : G[node])
        if (!viz[x]) Dfs(x);
    sol.push_back(node);
}

int main()
{
    f>>n>>m>>s;
    for (int i=1; i<=m; ++i)
    {
        f>>x>>y;
        G[x].push_back(y);
    }
    for (int i=1; i<=n; ++i)
        if (!viz[i]) Dfs(i);
    for (auto x : sol) g<<x<<' ';
    g<<'\n';
    //for (auto x : cc) g<<x<<' ';
    //g<<'\n';
    f.close();
    g.close();
    return 0;
}