Cod sursa(job #2890878)

Utilizator MihaiZ777MihaiZ MihaiZ777 Data 16 aprilie 2022 20:56:30
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

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

int n, m;
vector <int> graph[50005];
bool visited[50005];

void DFS(int node)
{
    fout << node << ' ';
    visited[node] = true;
    for (int currNode : graph[node])
    {
        if (visited[currNode])
        {
            continue;
        }
        DFS(currNode);
    }
}

int main()
{
    fin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int a, b;
        fin >> a >> b;

        graph[a].push_back(b);
    }

    for (int i = 1; i <= n; i++)
    {
        if (visited[i])
        {
            continue;
        }
        DFS(i);
    }
    
    fout << '\n';
}