Cod sursa(job #2642535)

Utilizator sebimihMihalache Sebastian sebimih Data 15 august 2020 20:22:02
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

const int N = 50005;

vector<int> g[N], SirSortat;
bool vizitat[N];

void df(int nod)
{
	vizitat[nod] = true;

	for (int neigh : g[nod])
	{
		if (!vizitat[neigh])
		{
			df(neigh);
		}
	}

	SirSortat.push_back(nod);
}

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

	for (int i = 0; i < m; i++)
	{
		int x, y;
		fin >> x >> y;
		g[x].push_back(y);
	}

	for (int i = 1; i <= n; i++)
	{
		if (!vizitat[i])
		{
			df(i);
		}
	}

	for (int i = SirSortat.size() - 1; i >= 0; i--)
	{
		fout << SirSortat[i] << ' ';
	}
}