Cod sursa(job #3214846)

Utilizator Razvan23Razvan Mosanu Razvan23 Data 14 martie 2024 15:02:28
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <bits/stdc++.h>
using namespace std;

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

int n, m, st[50005], top;
vector<int> L[50005];
bitset<50005> v;

void DFS(int k)
{
    v[k] = 1;
    for(auto w : L[k])
        if(!v[w]) DFS(w);
    st[++top] = k;
}

int main()
{
    ios_base::sync_with_stdio(0);
    fin.tie(0);
    fout.tie(0);
    int i, x, y;
    fin >> n >> m;
    for(i=1;i<=m;i++)
    {
        fin >> x >> y;
        L[x].push_back(y);
    }
    for(i=1;i<=n;i++)
        DFS(i);
    for(i=n;i>=1;i--)
        fout << st[i] << " ";
    fin.close();
    fout.close();
    return 0;
}