Cod sursa(job #588275)

Utilizator AnteusPatrascoiu Mihai Anteus Data 7 mai 2011 15:26:52
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.49 kb
#include <fstream.h>
#include <vector.h>
#define r 100001
ifstream fin("dfs.in");
ofstream fout("dfs.out");
vector <int> v[r];
int s[r],n,i,k;

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

void dfs(int x) {
int i;
s[x]=1;
for (i=0;i<v[x].size();i++)
	if (!s[v[x][i]])
		dfs(v[x][i]);
}

int main() {
citire();
for (i=1;i<=n;i++)
	if (!s[i])
	{	
		k++;
		dfs(i);
	}

fout<<k;
return 0;
}