Cod sursa(job #492952)

Utilizator wallyMocanu Valentin wally Data 16 octombrie 2010 15:25:11
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.54 kb
#include<fstream.h>
#include<iostream.h>
int s[100],vf,viz[100];
void df (int a[100][100], int n, int x)
{
	int i;
	vf++;
	s[vf]=x;
	viz[x]=1;
	for (i=1;i<=n;i++)
		if (a[s[vf]][i]==1 and viz[i]==0)
			df (a,n,i);
}
int main ()
{
	ifstream f ("dfs.in");
	ofstream g ("dfs.out");
	int i,n,a[100][100],k=0,m,y,z;
	f>>n>>m;
	for (i=1;i<=m;i++)
	{
		f>>y>>z;
		a[y][z]=a[z][y]=1;
	}
	i=1;
	while (i<=n)
	{
		if (viz[i]==0)
		{
			df (a,n,i);
			k++;
		}
		else
			i++;
	}
	g<<k-1;
	g.close ();
	f.close ();
	return 0;
}