Cod sursa(job #660902)

Utilizator federerUAIC-Padurariu-Cristian federer Data 13 ianuarie 2012 14:30:50
Problema Parcurgere DFS - componente conexe Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include<fstream>
#define Nmax 100001
using namespace std;

struct muchie
{int x, y;};
muchie G[Nmax];
long long n, m, i, Max, Min, C[Nmax], j, fr[Nmax], c;

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

void comp_conexe()
{
	for(i=1;i<=m;i++)
		if(C[G[i].x]!=C[G[i].y])
		{
			if(C[G[i].x]>C[G[i].y])
				Max=C[G[i].x], Min=C[G[i].y];
			else
				Max=C[G[i].y], Min=C[G[i].x];
			for(j=1;j<=n;j++)
				if(C[j]==Max) C[j]=Min;
		}
}
int main()
{
	fin>>n>>m;
	for(i=1;i<=m;i++)
		fin>>G[i].x>>G[i].y;
	for(i=1;i<=n;i++)C[i]=i;
	comp_conexe();
	for(i=1;i<=n;i++)
		if(!fr[C[i]])
		{
			fr[C[i]]=1;
			c++;
		}
	fout<<c<<'\n';
	fin.close();
	fout.close();
	return 0;
}