Cod sursa(job #1697365)

Utilizator Gabiap2015Apostol Gabriel Gabiap2015 Data 1 mai 2016 19:40:20
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
// DFS.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include "iostream"
int n, m;
int math[10][10], nod[10], numarator = 0;

void citire()
{
	freopen("dfs.in", "r", stdin);
	freopen("dfs.out", "w", stdout);
	scanf("%d %d", &n, &m);
	int i, x, y;
	for (i = 1; i <= m; i++)
	{
		scanf("%d %d", &x, &y);
		math[x][y] = math[y][x] = 1;
	}
}

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

int main()
{	
	citire();
	dfs();
	printf("%d\n", numarator);
	return 0;
}