Cod sursa(job #3329396)

Utilizator st3fann_24Nica Stefan st3fann_24 Data 13 decembrie 2025 09:27:08
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define INF 2e9

std::ifstream f("sortaret.in");
std::ofstream g("sortaret.out");

std::vector<int> v[50001];
int n, m, d[50001];
int main(){
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);

    f >> n >> m;
    for(int i = 1, x, y; i <= m; ++i)
        f >> x >> y, d[y] ++,
        v[x].push_back(y);
    
    std::queue<int> q;
    for(int i = 1; i <= n; ++i)
        if(d[i] == 0)
            q.push(i);
    for(; !q.empty(); q.pop()){
        int p = q.front();
        g << p << ' ';
        for(auto i : v[p])
            if(d[i] >= 1){
                -- d[i];
                if(d[i] == 0)
                    q.push(i);
            }
    }
    return 0;   
}