Cod sursa(job #2962036)

Utilizator RobertlelRobert Robertlel Data 7 ianuarie 2023 17:46:14
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <iostream>
#include <fstream>
#include <stack>
#include <vector>

using namespace std;

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

int nod , i , n , viz[50005] , v[50005] , k = 0 , m , x , y;

vector < int > g[50005];

void dfs (int nod)
{
    viz[nod] = 1;
    for (int i = 0 ; i < g[nod].size () ; i++)
    {
        if (viz[g[nod][i]] == 0)
            dfs (g[nod][i]);
    }
    k++;
    v[k] = nod;
}
int main()
{
    f >> n >> m;
    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] == 0)
            dfs (i);
    }
    while (k)
    {
        g1 << v[k] <<" ";
        k--;
    }
    return 0;
}