Cod sursa(job #530307)

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

bool contor[NMAX];
int sol;

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;
		
		int k = 1;
		
		for(unsigned int j = 0; j < lista[i].size(); ++j)
		{
			if(!contor[lista[i][j]])
				contor[lista[i][j]] = 1;
			
			++k;
		}
		
		if(k)
			++sol;
	}
	
	g << sol;
	
	f.close();
	g.close();
	
	return 0;
	
}