Cod sursa(job #753626)

Utilizator Lokycatalin petre Loky Data 30 mai 2012 09:54:21
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <cstdio>
#include <vector>

using namespace std;

int sel[100100],m,n,x,y,nr,i;
vector <int> ::iterator it;
vector <int> g[100000];

void df(int x)
{
    vector <int> ::iterator it;
	sel[x]=1;
	for (it=g[x].begin();it!=g[x].end();it++)
		if (sel[*it]==0)
			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);
		g[x].push_back(y);
		g[y].push_back(x);
	}

	for (i=1;i<=n;i++)
		if (sel[i]==0)
		{
			nr++;
			sel[i]=1;
			df(i);
		}

	printf("%d",nr);

	fclose(stdin);
	fclose(stdout);
	return 0;
}