Cod sursa(job #2179078)

Utilizator cc4infinityCojocaru Catalin cc4infinity Data 19 martie 2018 22:17:24
Problema Sortare topologica Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>

using namespace std;

int i,j,m,n,x,y,a,b;
struct tab
{
    queue<int> l;
    int nr;
};
tab t[5000];
queue <int> nod;
int main()
{
    ifstream fin("sortaret.in");
    ofstream fout("sortaret.out");
    fin>>n>>m;
    for (i=1;i<=m;i++)
    {
        fin>>a>>b;
        t[a].l.push(b);
        t[b].nr++;
    }
    for (i=1;i<=n;i++)
        if (t[i].nr==0) nod.push(i);
    while (!nod.empty())
    {
        x=nod.front();
        fout<<x<<" ";
        while (!t[x].l.empty())
        {
            t[t[x].l.front()].nr--;
            if (t[t[x].l.front()].nr==0) nod.push(t[x].l.front());
            t[x].l.pop();
        }
        nod.pop();
    }
    return 0;
}