Cod sursa(job #2427548)

Utilizator PrekzursilAndrei Visalon Prekzursil Data 31 mai 2019 23:23:37
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
vector<bool> vis(100005, false);
vector<int> graph[100005];
void top(int v, vector<int> graph[], stack<int>& st) {
	vis[v] = true;
	for (auto it : graph[v]) {
		if (vis[it] == false)
			top(it, graph, st);
	}
	st.push(v);
}
int main() {
	ifstream f("sortaret.in");
	int n, m;
	f >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int a, b;
		f >> a >> b;
		graph[a].push_back(b);
	}
	f.close();
	stack<int> st;
	for (int i = 1; i <= n; i++)
	{
		if (vis[i] == false)
		{
			top(i, graph,st);
		}
	}
	ofstream g("sortaret.out");
	while (!st.empty())
	{
		g << st.top() << " ";
		st.pop();
	}
	g.close();
}