Cod sursa(job #1947026)

Utilizator ButmalaiDanButmalai Dan ButmalaiDan Data 30 martie 2017 17:57:35
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.38 kb
#include<bits/stdc++.h>
using namespace std;
vector<int> a[200100];
int n,m,x,y,v[100100],r;
void d(int x){
	v[x]=1;
	for(vector<int>::iterator it = a[x].begin(); it != a[x].end(); it++)
		if (!v[*it])d(*it);
}
int main(){
	cin >> n >> m;
	while(m--){
		cin >> x >> y;
		a[x].push_back(y);
		a[y].push_back(x);
	}
	for(x = 1; x <= n; x++)
		if(!v[x])r++,d(x);
	cout << r;
}