Cod sursa(job #3284441)

Utilizator CimpoesuFabianCimpoesu Fabian George CimpoesuFabian Data 11 martie 2025 17:20:52
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");

int n, m, viz[50001];
vector<int> G[50001];
int a[50001], k;

void DFS(int nod)
{
    viz[nod] = 1;
    for (auto next : G[nod])
        if (viz[next] == 0)
            DFS(next);
    a[++k] = nod;
}

int main()
{
    int i, x, y;
    fin >> n >> m;
    for (i = 1 ; i <= m ; i++)
    {
        fin >> x >> y;
        if (find(G[x].begin(), G[x].end(), y) == G[x].end())
            G[x].push_back(y);
    }
    for (i = 1 ; i <= n ; i++)
        if (viz[i] == 0)
            DFS(i);
    for (i = k ; i >= 1 ; i--)
        fout << a[i] << " ";
    return 0;
}