Cod sursa(job #3370125)

Utilizator JenJenCristache Ion JenJen Data 24 octombrie 2026 10:59:59
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>
#include <vector>
#include <bitset>
#include <algorithm>
using namespace std;

ifstream cin ("sortaret.in");
ofstream cout ("sortaret.out");

#define MAX 50005
int n, m, u, v;

vector <int> adj[MAX];
bitset<50005> viz;
vector <int> ve;

void dfs(int nod)
{
    viz[nod] = 1;
    for (const auto& x : adj[nod]) {
        if (!viz[x]) {
            dfs(x);
        }
    }

    ve.push_back(nod);
}


int main()
{
    cin >> n >> m;

    for (int i = 0; i < m; i++)
    {
        cin >> u >> v;
        adj[u].push_back(v);
    }

    for (int i = 1; i <= n; i++)
    {
        if(!viz[i])
        {
            dfs(i);
        }
    }

    reverse(ve.begin(), ve.end());

    for (const auto& x : ve)
    {
        cout << x << " ";
    }
    return 0;
}