Cod sursa(job #1168925)

Utilizator h2g2Ford Prefect h2g2 Data 9 aprilie 2014 21:55:28
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.51 kb
#include <iostream>
#include <fstream>
#include <vector>
#define nmax 100005
using namespace std;

int n, m, a, b, comps=0;
vector <int> v[nmax];
bool seen[nmax];

void dfs(int x) {
	seen[x] = true;

	for(int i=0; i<int(v[x].size()); i++)
		if(!seen[v[x][i]]) dfs(v[x][i]);
}

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

	f>>n>>m;
	for(int i=1; i<=m; i++) {
		f>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}

	for(int i=1; i<=n; i++) 
		if(!seen[i]) ++comps, dfs(i);

	g<<comps<<"\n";

	return 0;
}