Cod sursa(job #3039231)

Utilizator ioana3317ioanapopescu ioana3317 Data 28 martie 2023 12:23:06
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

const int N =50000 + 2;
int n, m, t[N], tm;
vector <int> lg[N];

void dfs(int nd){
    if(t[nd] == 0){
        tm= nd;
    }
    else{
        dfs( t[nd] );
    }
}

void dfs2(int nd){
    out << nd << " ";
    for(auto i: lg[nd] ){
        dfs2(i);
    }
}

int main()
{
    in >> n >> m;
    for(int i=1; i<=m; i++){
        int x, y;
        in >> x >> y;
        lg[x].push_back(y);
        t[y] = x;
    }

    dfs(1);
    dfs2(tm);

    return 0;
}