Cod sursa(job #2323532)

Utilizator CryshanaGanea Carina Cryshana Data 19 ianuarie 2019 12:02:11
Problema Sortare topologica Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

const int N = 50001, M = 100001;
vector <int> a[N];
int pred[N], stopol[N], nr;
bool viz[N];

void dfs (int x )
{
    viz[x] = true;
    for (auto y : a[x])
    {
        dfs(y);
    }
    stopol[++nr] = x;
}

int main()
{
    int n, m;
    in >> n >> m;
    for ( int i = 0; i < m; i++ )
    {
        int x, y;
        in >> x >> y;
        a[x].push_back(y);
        //viz[y] = true;
    }
    for ( int i = 1; i <= n; i++ )
    {
        if ( viz[i] == false )
        {
            dfs(i);
        }
    }
    for ( int i = nr; i > 0; i-- )
    {
        out << stopol[i] << " ";
    }
    return 0;
}