Cod sursa(job #673375)

Utilizator comananamariaComan Ana-Maria comananamaria Data 4 februarie 2012 12:56:09
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include<fstream>

using namespace std;

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

int n,m,viz[100001],cc;

struct nod{
	int info;
	nod *next;
};

nod *LA[100001];

void add(int x, int y){
	nod *n=new(nod);
	n->info=y;
	n->next=LA[x];
	LA[x]=n;
	n=new(nod);
	n->info=x;
	n->next=LA[y];
	LA[y]=n;
}

void citire(){
	in>>n>>m;
	for(int i=1;i<=m;i++){
		int x,y;
		in>>x>>y;
		add(x,y);
	}
}

void df(int nodd){
	//out<<nod<<" ";
	nod *nc=LA[nodd];
	while(nc){
		int y=nc->info;
		if(viz[y]==0){
			//out<<i<<" ";
			viz[y]=1;
			df(y);
		}
		nc=nc->next;
	}
}

int main(){
	citire();
	for(int i=1;i<=n;i++)
		if(!viz[i]){
			cc++;
			viz[i]=1;
			df(i);
		}
	out<<cc;
	return 0;
}