Cod sursa(job #2399078)

Utilizator iulian2304Popa Ionut Iulian iulian2304 Data 6 aprilie 2019 20:29:36
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
//Link : https://infoarena.ro/problema/sortaret

#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

ifstream in("sortaret.in");
ofstream out("sortaret.out");
#define cout out

vector<int> G[50005];
queue<int> Q;
int degOut[50005], m, n;

void TopSort(){
    int i, x;
    for(x = 1; x <= n; ++x)
        if(degOut[x] == 0)Q.push(x);
    while(!Q.empty()){
        x = Q.front();
        cout << x << ' ';
        Q.pop();
        for(auto it : G[x]){
            degOut[it]--;
            if(degOut[it] == 0)Q.push(it);
        }
    }

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int i, A, B;
    in >> n >> m;

    for(i = 0; i < m; ++i){
        in >> A >> B;
        G[A].push_back(B);
        degOut[B]++;
    }
    TopSort();

    in.close();
    out.close();
}