Cod sursa(job #2639953)

Utilizator Iustin01Isciuc Iustin - Constantin Iustin01 Data 4 august 2020 16:18:38
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <bits/stdc++.h>
#define oo 500005
using namespace std;

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


vector < int > g[oo], L;
int n, m, x, y;
bitset < oo > c;

void dfs(int k){
    c[k] = true;
    for(int i = 0; i < g[k].size(); i++){
        int vec = g[k][i];
        if(!c[vec])
            dfs(vec);
    }
    L.push_back(k);
}

int main(){
    in>>n>>m;
    for(int i = 1; i <= m; i++){
        in>>x>>y;
        g[x].push_back(y);
    }
    for(int i = 1; i <= n; i++)
        if(!c[i])
            dfs(i);

    for(int i = L.size() - 1; i >= 0; i--)
        out<<L[i]<<" ";
}