Mai intai trebuie sa te autentifici.

Cod sursa(job #712768)

Utilizator giuliutzagiulia petre giuliutza Data 13 martie 2012 19:41:09
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
# include <iostream>
# include <fstream>
# include <vector>
using namespace std;

ifstream in("dfs.in");
ofstream out("dfs.out");

# define N 100000
# define M 200000
int n,m;
vector <int> a[N];
bool S[N];

void citire()
{
	int x,y,i;
	in>>n>>m;
	for(i=1;i<=m;i++)
	{
		in>>x>>y;
		a[x].push_back(y);
		a[y].push_back(x);
	}
}

void df(int x)
{
	int i,y;
	for(i=0;i<a[x].size();i++){
		y=a[x][i];
		if(S[y]==false)
		{
			S[y]=true;
			df(y);
		}
	}
}

int main()
{
	int i,nr=0;
	citire();
	for(i=1;i<=n;i++)
	{
		if(S[i]==false)
		{
			S[i]=true;
			df(i);
			nr++;
		}
	}
	out << nr << "\n";
	return 0;
}