Cod sursa(job #2929353)

Utilizator DKMKDMatei Filibiu DKMKD Data 25 octombrie 2022 17:53:33
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.61 kb
#include <bits/stdc++.h>
#define N 50005
#define INF 0x3f3f3f3f

using namespace std;

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

int n, m;
vector<int>g[N];
bitset<N>mrk;
stack<int>S;
void read() {
	int x, y;
	fin >> n >> m;
	for (int i = 1; i <= m; ++i) {
		fin >> x >> y;
		g[x].push_back(y);
	}
}
void dfs(int x) {
	mrk[x] = 1;
	for (auto i : g[x])
		if(!mrk[i])
			dfs(i);
	S.push(x);
}
void TS() {
	for (int i = 1; i <= n; ++i)
		if (!mrk[i])
			dfs(i);
	while (!S.empty()) {
		fout << S.top() << " ";
		S.pop();
	}
}
int main() {
	read();
	TS();
	return 0;
}