Cod sursa(job #2446537)

Utilizator ioanaa_ghGhiorghi Ioana-Cristina ioanaa_gh Data 9 august 2019 14:55:40
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <bits/stdc++.h>

using namespace std;

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

stack < int > st;
vector < int > G[50005];
bool v[50005];
int n, m, x, y;

void Citire()
{
    int i;
    fin >> n >> m;
    for(i = 1; i <= m; i++)
    {
        fin >> x >> y;
        G[x].push_back(y);
    }
}

void DFS(int nod)
{
    v[nod] = 1;
    for(auto w : G[nod])
        if(v[w] == 0)
            DFS(w);
    st.push(nod);
}

void Rezolvare()
{
    int i;
    for(i = 1; i <= n; i++)
        if(v[i] == 0)
            DFS(i);
    while(!st.empty())
    {
        fout << st.top() << " ";
        st.pop();
    }
    fout << "\n";
    fout.close();
}

int main()
{
    Citire();
    Rezolvare();
    return 0;
}