Cod sursa(job #728968)

Utilizator andreifirstCioara Andrei Ioan andreifirst Data 29 martie 2012 09:52:20
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.52 kb
#include <fstream>
#include <list>
using namespace std;

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

list <int> v[100005];
bool viz[100005];
int i, j, n, m, t, x, y;

void dfs (int fx){
	viz[fx]=1;
	
	list <int>::iterator it=v[fx].begin();
	
	while (it!=v[fx].end()){
		if (viz[*it]==0) dfs (*it);
		it++;
	}
}

int main(){
	f>>n>>m;
	for (i=1; i<=m; i++){
		f>>x>>y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	
	for (i=1; i<=n; i++){
		if (viz[i]==0) {
			t++;
			dfs(i);
		}
	}
	
	g<<t;
}