Cod sursa(job #1536067)

Utilizator theodor.moroianuTheodor Moroianu theodor.moroianu Data 25 noiembrie 2015 17:06:18
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.62 kb
#include <fstream>
#include <vector>
using namespace std;

const int NMAX = 50000;

vector <int> v[NMAX];
int grad[NMAX];
bool trc[NMAX];
int n, m, x, y, j;
ofstream out("sortaret.out");

void stop(int x);

int main() {
	ifstream in("sortaret.in");
	in >> n >> m;
	for (int i = 0; i < m; i++) {
		in >> x >> y;
		v[x].push_back(y);
		grad[y]++;
	}
	for (int i = 1; i <= n; i++) {
		if (grad[i] == 0 && trc[i] == false)
			stop(i);
	}
	in.close();
	out.close();
	return 0;
}


void stop(int x) {
	trc[x] = 1;
	out << x << ' ';
	for (auto j : v[x]) {
		grad[j]--;
		if (grad[j] == 0)
			stop(j);
	}
}