Cod sursa(job #395074)

Utilizator unknownliviuMaria Liviu Valentin unknownliviu Data 12 februarie 2010 01:28:30
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include<cstdio>
#include<vector>
using namespace std;
const int N=100010;
vector<int> a[N];
int n,m,s;
void read()
{
	freopen("dfs.in","r",stdin);
	int x,y;
	scanf("%d%d" ,&n,&m);
	s=m;
	while(m--)
	{
		scanf("%d%d" ,&x,&y);
		a[x].push_back(y);
		a[y].push_back(x);
	}
}
char viz[N];
 void dfs (int nod)
{
	viz [nod]=1;
	int i;
	for (i=0; i < a [nod].size (); ++i)
	if (!viz [a [nod] [i]])
	dfs (a [nod] [i]);
}
int main()
{
	read();
	freopen("dfs.out","w",stdout);
	int c=0;
	for(int i=1;i<=n;i++)
		if(!viz[i])
		{
			dfs(i);
			c++;
		}
	printf("%d",c);
	return 0;
}