Cod sursa(job #2760067)

Utilizator andreiiorgulescuandrei iorgulescu andreiiorgulescu Data 22 iunie 2021 18:58:44
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>

using namespace std;

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

int n,m;
bool viz[50005];
vector<int>a[50005];
vector<int>tps;
void citire()
{
    in >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int x,y;
        in >> x >> y;
        a[x].push_back(y);
    }
}

void DFS(int p)
{
    viz[p] = true;
    for (int i = 0; i < a[p].size(); i++)
        if (viz[a[p][i]] == false)
            DFS(a[p][i]);
    tps.push_back(p);
}

int main()
{
    citire();
    for (int i = 1; i <= n; i++)
        if (viz[i] == false)
            DFS(i);
    reverse(tps.begin(),tps.end());
    for (int i = 0; i < tps.size(); i++)
        out << tps[i] << " ";
    return 0;
}