Cod sursa(job #1896248)

Utilizator andrei_diaconuAndrei Diaconu andrei_diaconu Data 28 februarie 2017 16:15:19
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <fstream>
#include <queue>

#define NMax 50001

using namespace std;

ifstream f("sortaret.in");
ofstream g("sortaret.out");

int nodes, edges, intDeg[NMax];
queue<int> QU;
vector<int> G[NMax];

int main()
{
	f >> nodes >> edges;

	int n1, n2;
	for (int i = 1; i <= edges; i++) {
		f >> n1 >> n2;
		G[n1].push_back(n2);

		intDeg[n2]++;
	}

	for (int i = 1; i <= nodes; i++)
		if (intDeg[i] == 0)
			QU.push(i);

	while (!QU.empty()) {
		int crtNode = QU.front();
		QU.pop();
		g << crtNode << ' ';

		for (auto &node : G[crtNode]) {
			intDeg[node]--;

			if (intDeg[node] == 0)
				QU.push(node);
		}
	}
}