Cod sursa(job #2351960)

Utilizator HumikoPostu Alexandru Humiko Data 22 februarie 2019 20:53:45
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
//#include "pch.h"
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

int idx;
int f[50005];
int v[50005];
vector <int> graph[50005];

void dfs(int node)
{
	f[node] = 1;
	for (auto x : graph[node]) {
		if (!f[x]) {
			dfs(x);
		}
	}

	v[++idx] = node;
}

int main()
{
	ios::sync_with_stdio(false);
	fin.tie(0); fout.tie(0);

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

	for (int i = 1; i <= n; ++i) {
		if (!f[i]) {
			dfs(i);
		}
	}

	for (int i = n; i >= 1; --i) {
		fout << v[i] << " ";
	}
}