Cod sursa(job #2370893)

Utilizator CarmenRo33Anghel Ionela Carmen CarmenRo33 Data 6 martie 2019 14:20:53
Problema Sortare topologica Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

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

const int Nmax=50005;
const int Mmax=100005;
int N,M;
int tf[Nmax],k,Nod[Nmax];
vector <int> Muchii[Nmax];
bool viz[Nmax];

void DFS(int nod)
{
    viz[nod]=true;
    ++k;
    unsigned int i=0;
    for(i=0;i<Muchii[nod].size();i++, ++k)
    {
        int vecin=Muchii[nod][i];
        if(!viz[vecin])
          DFS(vecin);
    }
    if(!i) ++k;

    tf[nod]=k;
}


void read()
{
    fin>>N>>M;
    int x,y;
    for(int i=0;i<M;i++)
     {
        fin>>x>>y;
        Muchii[x].push_back(y);
     }

    for(int i=1;i<=N;i++)
        {
            Nod[i]=i;
            if(!viz[i])
              DFS(i);
        }

    for(int i=1; i<N;i++)
     for(int j=i+1;j<=N;j++)
        if(tf[Nod[i]]<tf[Nod[j]])
        {
            int aux=Nod[i];
            Nod[i]=Nod[j];
            Nod[j]=aux;

        }

    for(int i=1;i<=N;i++)
        fout<<Nod[i]<<" ";

}
int main()
{
    read();

    return 0;
}