Cod sursa(job #3229895)

Utilizator AlexandruIoan20Moraru Ioan Alexandru AlexandruIoan20 Data 17 mai 2024 21:05:17
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.49 kb
#include <iostream>
#include <fstream>
using namespace std; 

ifstream fin("sortaret.out"); 
ofstream fout("sortaret.out");
int n, m, a[5001][5001], viz[50001], x, y; 

void dfs(int x) {
	fout << x << ' ';  
	viz[x] = 1; 
	for(int j = 1; j <= n; j++)
		if(a[x][j] == 1 && !viz[j]) dfs(j); 
}

int main () { 
	fin >> n >> m;
	for(int i = 1; i <= m; i++) { 
		fin >> x >> y; 
		a[x][y] = a[y][x] = 1;  
	}
	
	for(int i = 1; i <= n; i++)
		if(!viz[i]) dfs(i); 
	fin.close(); 
	fout.close(); 
	return 0; 
}