Cod sursa(job #2422096)

Utilizator Sergiu.VictorTalmacel Sergiu Victor Sergiu.Victor Data 17 mai 2019 11:04:30
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;

ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
const int N = 50001;
int n,m;
vector<vector<int>>G(N+1);
stack<int> topSort;
void citire()
{
    fin>>n>>m;
    int x,y;
    for(int i=1;i<=n;i++)
    {
        fin>>x>>y;
        G[x].push_back(y);
    }

}
void sortare(int x,vector<bool>&viz)
{
    viz[x]=true;
    for(auto vecin:G[x])
        if(!viz[vecin]) sortare(vecin,viz);
    topSort.push(x);

}
void rez()
{
    citire();
    vector<bool> viz(N+1,false);
    for(int i=1;i<=n;i++)
        if(!viz[i]) sortare(i,viz);
    while(!topSort.empty())
    {
        fout<<topSort.top()<<" ";
        topSort.pop();
    }
}
int main() {
    rez();
//    for(int i=1;i<=n;i++) {
//        cout << i << ": ";
//        for (auto vecin:G[i])
//            fout << vecin << " ";
//        fout<<'\n';
//    }
    return 0;
}