Cod sursa(job #500504)

Utilizator chiar_nimeninimeni chiar_nimeni Data 12 noiembrie 2010 13:40:06
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
# include <stdio.h>
# include <string.h>
# include <vector>

# define pb push_back

using namespace std;

vector<int> a[100100];
vector<int>::iterator it;

int i,n,m,x,y,nr;
bool sel[100010];

void df (int x)
{
	vector<int>::iterator it;
	sel[x]=true;
	for (it=a[x].begin(); it!=a[x].end(); it++)
	{
		if (!sel[*it])
		df (*it);
	}
}

int main ()
{
freopen ("dfs.in","r",stdin);
freopen ("dfs.out","w",stdout);
scanf ("%d %d",&n,&m);
for (i=1; i<=m; i++)
{
	scanf ("%d %d",&x,&y);
	a[x].pb(y);
	a[y].pb(x);
}
memset(sel,false,sizeof(sel));
i=0;
nr=0;
while (i<n)
{
	i++;
	if (sel[i]==false)
	{
		nr++;
		df(i);
	}
}
printf ("%d\n",nr);
return 0;
}