Cod sursa(job #1540849)

Utilizator gabi.cristacheGabi Cristache gabi.cristache Data 3 decembrie 2015 13:18:08
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
// infoarenaDFSnonRec.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <fstream>
#include <vector>
#include <stack>
#include <assert.h>

#define MaxN 50000

using namespace std;

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

int N, M;
vector<int> G[MaxN];
bool visited[MaxN];
stack<int> res;

int max(int a, int b) {
	return a > b ? a : b;
}

void dfs(int n) {
	visited[n] = true;
	for (int i = 0; i < G[n].size(); ++i) {
		if (!visited[G[n][i]])
			dfs(G[n][i]);
	}
	res.push(n);
}

int main()
{
	fin >> N >> M;
	for (int i = 0; i < M; ++i) {
		int x, y;
		fin >> x >> y;
		G[x].push_back(y);
		G[y].push_back(x);
	}

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

	while (!res.empty()) {
		fout << res.top() << ' ';
		res.pop();
	}

	return 0;
}