Cod sursa(job #411295)

Utilizator RegeleUmbrelorPopescu Mihai RegeleUmbrelor Data 4 martie 2010 20:18:47
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include<stdio.h>
#include<vector>
using namespace std;
const int NMAX=100005;
int viz[NMAX],nr,n,m;
vector <int> a[NMAX];

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

void dfs(int x)
{
	viz[x]=1;
	unsigned int i;
	for(i=0;i<a[x].size();++i)
		if(viz[a[x][i]]==0)
			dfs(a[x][i]);
}

void rez()
{
	int i;
	for(i=1;i<=n;++i)
		if(viz[i]==0)
		{	
			dfs(i);
			nr++;
		}
	printf("%d", nr);
}

int main()
{
	citire();
	rez();
	return 0;
}