Cod sursa(job #530310)

Utilizator Catah15Catalin Haidau Catah15 Data 7 februarie 2011 15:22:48
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>
#include <vector>
#define NMAX 100010
using namespace std;

bool contor[NMAX];
int sol, z;

int main()
{
	int n, m;
	
	vector <int> lista[NMAX];
	
	ifstream f("dfs.in");
	ofstream g("dfs.out");
	
	f >> n;
	
	for(f >> m; m; --m)
	{
		int x, y;
		
		f >> x >> y;
		
		lista[x].push_back(y);
		lista[y].push_back(x);
	}
	
	for(int i = 1; i <= n; ++i)
	{
		
		if(contor[i])continue;
		contor[i] = 1;
		
		
		for(unsigned int j = 0; j < lista[i].size(); ++j)
		{
			z = lista[i][j];
			
			if(!contor[z])
			{
				contor[z] = 1;
				
				for(unsigned int t = 0; t < lista[z].size(); ++t)
					contor[lista[z][t]] = 1;
			}
		
			++sol;
		}
	}
	
	g << sol;
	
	f.close();
	g.close();
	
	return 0;
	
}