Cod sursa(job #715936)

Utilizator andreifirstCioara Andrei Ioan andreifirst Data 17 martie 2012 23:07:06
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.56 kb
#include <fstream>
using namespace std;

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

struct muchie {int n; muchie *a;};

muchie *v[100005], *p;
bool viz[100005];
int x, y, n, m, i, j, t;

void dfs (int fx){
	muchie *fp=v[fx];
	viz[fx]=1;
	
	while (fp!=NULL){
		if (viz[fp -> n] == 0) dfs(fp -> n);
		fp=fp->a;
	}
}
	
int main (){
	f>>n>>m;
	for (i=1; i<=m; i++){
		f>>x>>y;
		p=new muchie; p->n=y; p->a=v[x]; v[x]=p;
		p=new muchie; p->n=x; p->a=v[y]; v[y]=p;
	}
	
	for (i=1; i<=n; i++) if (viz[i]==0) {
		t++;
		dfs(i);
	}
	g<< t;
}