Cod sursa(job #2501799)

Utilizator razvanradulescuRadulescu Razvan razvanradulescu Data 30 noiembrie 2019 11:22:57
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("sortaret.in");
ofstream g("sortaret.out");

vector<int> graf[100005];
queue<int> Q;
int grIntern[100005], s, n, m;

void citire()
{
    int x, y;
    f>>n>>m;
    for(int i = 0; i<m; ++i)
    {
        f>>x>>y;
        graf[x].push_back(y);
    }
}

void lee()
{
    while(!Q.empty())
    {
        int nod = Q.front();
        Q.pop();
        for(auto &v : graf[nod])
        {
            grIntern[v]--;
            if(grIntern[v] == 0)
                Q.push(v);
        }
        g<<nod<<" ";
    }
}

void rez()
{
    for(int i = 1; i<=n; ++i)
    {
        for(auto &v : graf[i])
        {
            grIntern[v]++;
        }
    }
    for(int i = 1; i<=n; ++i) {
        if(grIntern[i] == 0)
            Q.push(i);
    }
    lee();
}

int main() {
    citire();
    rez();
    return 0;
}