Cod sursa(job #2667213)

Utilizator albertyoAlbert Mindrescu albertyo Data 3 noiembrie 2020 02:57:22
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.64 kb
#include <fstream>
#include <vector>
#define N 100005
using namespace std;

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

int n, m, viz[N], S[N], top;
vector <int> muchii[N];

void DFS(int nod) {

    viz[nod] = 1;
    for( auto i:muchii[nod] )
        if(viz[i] == 0)
            DFS(i);

    S[++top] = nod;
}

int main()
{
    int i, x, y;
    fin >> n >> m;
    for( i = 1; i <= m; i++) {
        fin >> x >> y;
        muchii[x].push_back(y);
    }

    for (i = 1; i <= n; i++)
        if(viz[i] == 0)
            DFS(i);

    for(i = top; i >= 1; i--)
        fout << S[i] << " ";

    return 0;
}