Cod sursa(job #2501831)

Utilizator razvanradulescuRadulescu Razvan razvanradulescu Data 30 noiembrie 2019 11:37:03
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream f("sortaret.in");
ofstream g("sortaret.out");

vector<int> graf[100005];
stack<int> S;
int viz[100005], s, n, m;

void citire()
{
    int x, y;
    f>>n>>m;
    for(int i = 0; i<m; ++i)
    {
        f>>x>>y;
        graf[x].push_back(y);
    }
}

void dfs(int nod)
{
    viz[nod] = 1;
    for(auto &v : graf[nod])
    {
        if(viz[v] == 0)
        {
            dfs(v);
        }
    }
    S.push(nod);
}

void rez()
{
    for (int i = 1; i <= n; ++i) {
        if(viz[i] == 0)
        {
            dfs(i);
        }
    }
    while(!S.empty())
    {
        g<<S.top()<<" ";
        S.pop();
    }
}

int main() {
    citire();
    rez();
    return 0;
}