Cod sursa(job #2933473)

Utilizator DMR6476Erdic Dragos DMR6476 Data 5 noiembrie 2022 11:05:05
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.31 kb
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;
vector<vector<int> > theNeighbours;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
vector<int> solution;
bool found[100005];
void dfs(int node){
    found[node] = true;

    for(int i = 0; i < theNeighbours[node].size(); i++)
    {
        int newNode = theNeighbours[node][i];
        if(found[newNode] == false){
            dfs(newNode);
        }
    }

    solution.push_back(node);



}

int main()
{
    int n, m;
    cin>>n>>m;
    theNeighbours = vector<vector<int> > (n + 5);
    for(int i = 0; i < m; i++){
        int firstNode, secondNode;
        cin>>firstNode>>secondNode;
        theNeighbours[firstNode].push_back(secondNode);
    }


    for(int i = 1; i <= n; i++){
        if(found[i] == false) dfs(i);
    }
    for(int i = solution.size() - 1; i >= 0; i--){
        cout<<solution[i]<<" ";
    }
    return 0;
}