Cod sursa(job #2738652)

Utilizator hurjui12AlexandruHurjui Alexandru-Mihai hurjui12Alexandru Data 6 aprilie 2021 10:36:33
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

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

vector <int> v[50001];
int g[50001];
queue <int> q;

int main()
{
	int i, n, m, x, y, j;
	fin >> n >> m;
	for (i = 1; i<=m; i++)
	{
		fin >> x >> y;
		v[x].push_back(y);
		g[y]++;
	}
	for (i = 1; i<=n; i++)
		if (g[i] == 0)
			q.push(i);
	for (i = 1; i<=n; i++)
	{
		x = q.front();
		q.pop();
		fout << x << ' ';
		for (j = 0; j<v[x].size(); j++)
		{
			g[v[x][j]]--;
			if (g[v[x][j]] == 0)
				q.push(v[x][j]);
		}
	}
	return 0;
}