Cod sursa(job #3370794)

Utilizator lefterache_stefanLefterache Stefan lefterache_stefan Data 27 octombrie 2026 11:59:46
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb


#include <bits/stdc++.h>
using namespace std;

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

const int MAXIM = 50005;
int n, m;

vector<int> graf[MAXIM];
deque<int> solutie;
bool vazut[MAXIM];

void dfs(int nod) {
	if (vazut[nod]) {
		return;
	}

	vazut[nod] = true;

	for (int x : graf[nod]) {
		dfs(x);
	}

	solutie.push_front(nod);
}

static inline void citire() {
	fin >> n >> m;

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

static inline void rezolvare() {
	for (int i = 1; i <= n; i++) {
		if (!vazut[i]) {
			dfs(i);
		}
	}
}

static inline void afisare() {
	for (int x : solutie) {
		fout << x << ' ';
	}
	fout << '\n';
}

int main() {
	citire();
	rezolvare();
	afisare();
	return 0;
}