Cod sursa(job #2541517)

Utilizator MarcGrecMarc Grec MarcGrec Data 8 februarie 2020 15:23:19
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#define MAX_N 50000

#include <fstream>
#include <list>
#include <bitset>
using namespace std;

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

int n, m;
list<int> L, G[MAX_N + 1], Gt[MAX_N + 1];
bitset<MAX_N + 1> V;

void Df(int nod);

int main()
{
	fin >> n >> m;
	
	for (int i = 0, x, y; i < m; ++i)
	{
		fin >> x >> y;
		G[x].push_back(y);
		Gt[y].push_back(x);
	}
	
	for (int i = 1; i <= n; ++i)
	{
		if (!V[i])
		{
			Df(i);
		}
	}
	
	for (int nod : L)
	{
		fout << nod << ' ';
	}
	
	fin.close();
	fout.close();
	return 0;
}

void Df(int nod)
{
	V[nod] = true;
	
	for (int pred : Gt[nod])
	{
		if (!V[pred])
		{
			Df(pred);
		}
	}
	
	L.push_back(nod);
}