Cod sursa(job #2425575)

Utilizator justicebringerArghire Gabriel justicebringer Data 24 mai 2019 21:45:32
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <bits/stdc++.h>

using namespace std;

vector <int> VIZITAT(50010);
queue <int> RETURNEZ;
vector <int> cev[50010];

void dfs(int start){
    VIZITAT[start] = true;

    for(auto it: cev[start]){
        if(!VIZITAT[it]){
            dfs(it);
        }
    }

    RETURNEZ.push(start);
}

int main()
{
    ifstream fin("sortaret.in");
    int noduri, muchii;

    fin >> noduri >> muchii;

    for(int i = 1; i <= muchii; i++){
        int x, y;
        fin >> x >> y;
        cev[x].push_back(y);
    }

    for(int i = 1; i <= noduri; i++)
        VIZITAT[i] = false;

    for(int i = 1; i <= noduri; i++)
        if(!VIZITAT[i])
            dfs(i);

    ofstream fout("sortaret.out");
    while(!RETURNEZ.empty()){
        fout << RETURNEZ.front() << " ";
        cout << RETURNEZ.front() << " ";
        RETURNEZ.pop();
    }
    











    return 0;
}