Cod sursa(job #1193063)

Utilizator yololy97Olaru Bogdan-Ioan yololy97 Data 30 mai 2014 19:46:35
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.54 kb
#include <cstdio>
#include <vector>
using namespace std;
int x, y, i, n, m, used[100001], nr;
vector<int> V[100001];
void dfs(int x){
	if(used[x] == true)
		return;
	used[x] = true;
	for(int j = 0; j < V[x].size(); ++j)
		dfs(V[x][j]);
}
int main(){
	freopen("dfs.in", "r", stdin);
	freopen("dfs.out", "w", stdout);
	scanf("%d %d ", &n, &m);
	for(i = 1; i <= m; ++i)
		scanf("%d %d ", &x, &y),
		V[x].push_back(y),
		V[y].push_back(x);
	for(i = 1; i <= n; ++i){
		if(used[i] == false)
			++nr;
		dfs(i);
	}
	printf("%d\n", nr);
}