Cod sursa(job #3268627)

Utilizator Silviu643Dumitrescu Silviu Florian Silviu643 Data 16 ianuarie 2025 16:06:03
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream in("sortaret.in");
ofstream out("sortaret.out");
vector <int> v[50005];
int n,m,x,y,r[50005],cnt;
bool f[50005];
void dfs(int s)
{
    cnt++;
    r[cnt]=s;
    f[s]=1;
    for(int i:v[s])
    {
        if(!f[i])
            dfs(i);

    }

}
int main()
{
    in>>n>>m;
    for(int i=1;i<=m;i++)
        {in>>x>>y;
        v[x].push_back(y);
        }
    for(int i=1;i<=n;i++)
        sort(v[i].begin(),v[i].end());
    for(int i=1;i<=n;i++)
    {
        if(!f[i])
            dfs(i);
    }
    for(int i=1;i<=n;i++)
        out<<r[i]<<" ";
    return 0;
}