Cod sursa(job #1305014)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 29 decembrie 2014 14:43:15
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
/// infoarena.ro/problema/dfs
#include <fstream>
#include <vector>

using namespace std;

const int maxn = 100005;

bool used[maxn];
vector <int> g[maxn];
int cc, n, m; // numarul componentelor conexe

inline void dfs(int node) {
	used[node] = 1;
	for(vector <int> :: iterator it = g[node].begin() ; it != g[node].end() ; ++ it)
		if(!used[*it])
			dfs(*it);
}

int main() {
	ifstream fin("dfs.in");
	ofstream fout("dfs.out");
	fin >> n >> m;
	while(m --) {
		int x, y;
		fin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	for(int i = 1 ; i <= n ; ++ i)
		if(!used[i]) {
			++ cc;
			dfs(i);
		}
	fout << cc << '\n';
}