Cod sursa(job #2112587)

Utilizator alxcl12Albu Alexandru alxcl12 Data 23 ianuarie 2018 17:45:46
Problema Sortare topologica Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <fstream>

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

using namespace std;

struct nod
{
    int inf;
    nod* leg;
};

nod* adr[60000];
int pred[60000];
int n,m;

void add(int x,int y)
{
    nod* p=new nod;
    p->inf=y;
    p->leg=adr[x];
    adr[x]=p;
}

void citire()
{
    f>>n>>m;
    int p,q;
    for(int i=1;i<=m;i++)
    {
        f>>p>>q;
        add(p,q);
        pred[q]++;
    }
}


int main()
{
    citire();
    for(int i=1;i<=n;i++)
    {
        int j=1;
        while(pred[j]) j++;
        g<<j<<' ';
        pred[j]--;
        nod* q;
        q=adr[j];
        while(q)
        {
            pred[q->inf]--;
            q=q->leg;
        }
    }
    return 0;
}