Cod sursa(job #1697941)

Utilizator Gabiap2015Apostol Gabriel Gabiap2015 Data 3 mai 2016 12:42:50
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include "iostream"
#include "fstream"
using namespace std;

int math[10][10], nod[10]; 
int m, n;


void dfs(int nod_start)
{
	nod[nod_start] = 1;
	for (int i = 1; i <= n; i++)
	if (!nod[i] && math[nod_start][i])
		dfs(i);
}

int main()
{
	int x, y;
	int numarator = 0;
	ifstream dfs_in;
	ofstream dfs_out;
	dfs_in.open("dfs.in");
	dfs_out.open("dfs.out");
	dfs_in >> n >> m;
	for (int i = 1; i <= m; i++)
	{
		dfs_in >> x >> y;
		math[x][y] = 1;
		math[y][x] = 1;
	}
	for (int i = 1; i <= n; i++)
	if (!nod[i])
	{
		numarator++;
		dfs(i);
	}
	dfs_out << numarator;
	return 0;
}