Cod sursa(job #2654466)

Utilizator StefanSanStanescu Stefan StefanSan Data 1 octombrie 2020 08:56:19
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include      <iostream>
#include      <fstream>
#include      <algorithm>
#include      <queue>
#include      <vector>
#include      <stack>


using namespace std;

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

int n, m;

vector < int > V[50005];
stack  < int > ST;

bool vizitat[50005];

void dfs(int poz) {
	vizitat[poz] = true;
	for (int i = 0; i < V[poz].size(); i++)
		if (vizitat[V[poz][i]] == false) dfs(V[poz][i]);
	ST.push(poz);
}

int main() {
	ios::sync_with_stdio(false);
	in.tie(NULL), out.tie(NULL);
	
	in >> n >> m;
	for (int i = 1; i <= m; i++) {
		int x, y;
		in >> x >> y;
		V[x].push_back(y);
	}

	for (int i = 1; i <= n; i++) 
		if (vizitat[i] == false) dfs(i);
	
	while (!ST.empty()) {
		out << ST.top() << ' ';
		ST.pop();
	}

	return 0;
	 
}