Cod sursa(job #3338850)

Utilizator SuperMihai26Patrascu Mihai SuperMihai26 Data 5 februarie 2026 10:31:34
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <bits/stdc++.h>
#define oo 100000001
using namespace std;
ifstream in("topsort.in");
ofstream out("topsort.out");
int n,m;
bitset<100001>viz(0);
set<int> Lista[100001];
stack<int> ST;

void DFS_Top(int x){
    viz[x]=1;
    for(int i:Lista[x])
        if(!viz[i])
            DFS_Top(i);
    ST.push(x);
}

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

int main(){
    in>>n>>m;
    int x,y;
    for(int i=1;i<=m;++i){
        in>>x>>y;
        Lista[x].insert(y);
    }
    Drum();
    while(!ST.empty()){
        out<<ST.top()<<' ';
        ST.pop();
    }
}