Cod sursa(job #2664846)

Utilizator gavra_bogdanBogdan Gavra gavra_bogdan Data 29 octombrie 2020 16:08:50
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.54 kb
#include <fstream>
#include <vector>

const int nmax = 5e4 + 5;

std::vector<int>l[nmax], ans;

int main() {
	std::ifstream fin("sortaret.in");
	std::ofstream fout("sortaret.out");
	int n, m, u, v;
	fin >> n >> m;
	std::vector<int>in(n);
	for (int i = 0; i < m; i++) {
		fin >> u >> v;
		l[--u].push_back(--v);
		in[v]++;
	}
	for (int i = 0; i < n; i++) if (!in[i]) ans.push_back(i);
	for (int i = 0; i < n; i++)
		for (auto x : l[ans[i]]) {
			in[x]--;
			if (!in[x]) ans.push_back(x);
		}
	for (int x : ans) fout << x + 1 << " ";
}