Cod sursa(job #2842496)

Utilizator butasebiButa Gabriel-Sebastian butasebi Data 31 ianuarie 2022 22:50:13
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <bits/stdc++.h>
using namespace std;
stack <int> Stack;
vector <int> v[50005];
bool viz[50005];
void DFS(int x)
{
    viz[x] = true;
    for(int i = 0; i < v[x].size(); i ++)
    {
        int aux = v[x][i];
        if(viz[aux] == false)
            DFS(aux);
    }
    Stack.push(x);
}
int i, n, m, x, y;
int main()
{
    ifstream f("sortaret.in");
    ofstream g("sortaret.out");
    f >> n >> m;
    for(i = 1; i <= m; i ++)
    {
        f >> x >> y;
        v[x].push_back(y);
    }
    for(i = 1; i <= n; i ++)
        if(viz[i] == false)
            DFS(i);
    while(!Stack.empty())
    {
        g << Stack.top() << " ";
        Stack.pop();
    }
    return 0;
}