Cod sursa(job #2905962)

Utilizator cosmin1805Iacobai Cosmin Andrei cosmin1805 Data 24 mai 2022 17:50:57
Problema Sortare topologica Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
#include <vector>
#include <fstream>
#include <queue>
#include <iostream>
using namespace std;
const int N = 50000;
vector <int> a[N + 1];
queue<int>q;
int nrp[N];
int n;
ifstream in("sortaret.in");
ofstream out("sortaret.out");
int check(int x) {
    for (auto y : a[x]) {
        nrp[y]--;
        if (nrp[y] == 0) {
            q.push(y);
            return y;
        }
    }
    return -1;
}
int main()
{
    int m;
    in >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int y, x;
        in >> x >> y;
        a[x].push_back(y);
        nrp[y]++;
    }
    for (int i = 1; i <= n; i++) {
        if (nrp[i] == 0) {
            q.push(i);
        }
    }
    while (!q.empty()) {
        int x = q.front();
        out << x << " ";
        q.pop();
        for (auto y : a[x]) {
            nrp[y]--;
            if (nrp[y] == 0) {
                q.push(y);
            }
        }
    }
    in.close();
    out.close();
    return 0;
}