Cod sursa(job #1131334)

Utilizator gabrielvGabriel Vanca gabrielv Data 28 februarie 2014 19:27:08
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

#define NMAX 50010

int N,M;
bool Gebraucht[NMAX];

vector < int > TSL, G[NMAX];

void Scannen()
{
    freopen("sortaret.in","r",stdin);
    scanf("%d%d",&N,&M);

    for(int i=1,x,y;i<=M;i++)
    {
        scanf("%d%d",&x,&y);
        G[x].push_back(y);
    }
}

void TopologicalSorting(int nod)
{
    Gebraucht[nod]=true;

    for(vector < int > ::iterator it=G[nod].begin();it!=G[nod].end();++it)
        if(!Gebraucht[*it])
            TopologicalSorting(*it);

    TSL.push_back(nod);
}

void Losen()
{
    for(int i=1;i<=N;i++)
        if(!Gebraucht[i])
            TopologicalSorting(i);

    reverse(TSL.begin(),TSL.end());
}

void Drucken()
{
    freopen("sortaret.out","w",stdout);

    for(vector < int > ::iterator it=TSL.begin();it!=TSL.end();++it)
        printf("%d ",(*it));
}

int main()
{
    Scannen();
    Losen();
    Drucken();

    return 0;
}