Cod sursa(job #3264686)

Utilizator Manolea_Teodor_StefanManolea Teodor Stefan Manolea_Teodor_Stefan Data 23 decembrie 2024 13:17:56
Problema Sortare topologica Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <bits/stdc++.h>
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")


using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
const int nmax = 50001;
int n,m;
array<set<int>,nmax> g;
int v[nmax];
int main() {
    queue<int> q;
    fin >> n >> m;
    while (m--) {
        int a,b;
        fin >> a >> b;
        g[a].insert(b);
        v[b]++;
    }
    for (int i = 1; i <= n; i++) {
        if (!v[i])
            q.push(i);
    }

    while (!q.empty()) {
        const auto x = q.front();
        q.pop();
        for (const int y : g[x]) {
            v[y]--;
            if (!v[y])
                q.push(y);
        }
        fout << x << ' ';
    }

    return 0;
}