Cod sursa(job #3340304)

Utilizator ioanxhIoan Budeanu ioanxh Data 13 februarie 2026 16:58:43
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include<bits/stdc++.h>
#define nl '\n'
using namespace std;
const string file="sortaret";
ifstream f(file+".in");
ofstream g(file+".out");
//#define f cin
//#define g cout
const int DD=50001;
int n,m;
vector<int>v[DD],r;
int deg[DD];
int main(){
    f>>n>>m;
    for (int i=1; i<=m; ++i) {
        int x,y;
        f>>x>>y;
        v[x].push_back(y);
        deg[y]++;
    }
    queue<int>q;
    for (int i=1; i<=n; ++i) if (deg[i]==0) q.push(i);
    while (!q.empty()) {
        int x=q.front();
        q.pop();
        r.push_back(x);
        for (auto e:v[x]) {
            deg[e]--;
            if (deg[e]==0) q.push(e);
        }
    }
    for (auto e:r) g<<e<<' ';
    system("pause");
    return 0;
}