Cod sursa(job #2973738)

Utilizator alexscanteieScanteie Alexandru alexscanteie Data 1 februarie 2023 19:51:09
Problema Sortare topologica Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <fstream>
using namespace std;

ifstream fin("sortaret.in");
ofstream fout("sortaret.out");

struct nod
{
    int info;
    nod* urm;
};

void adaugareFinal(nod *&h, int x)
{
    if (h == NULL)
    {
        h = new nod;
        h->info = x;
        h->urm = NULL;
    } else
    {
        nod* p = h;
        while (p->urm)
            p = p->urm;

        nod* nou = new nod;
        nou->info = x;
        nou->urm = NULL;
        p->urm = nou;
    }
}

int main()
{
    int n, m;
    fin >> n >> m;

    int v[100] = {};
    nod* w[100] = {};

    for (int i = 0; i < m; i++)
    {
        int x, y;
        fin >> x >> y;
        adaugareFinal(w[x], y);
        v[y]++;
    }

    for (int i = 0; i < n; i++)
    {
        int j;
        for (j = 1; j <= n; j++)
            if (v[j] == 0)
                break;


        fout << j << " ";
        v[j] = -1;

        nod* p = w[j];
        while (p)
        {
            v[p->info]--;
            p = p->urm;
        }
    }
}