Cod sursa(job #3158441)

Utilizator Dragos13Dragos Dragos13 Data 18 octombrie 2023 18:54:50
Problema Parcurgere DFS - componente conexe Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");

int timp = 0;
int viz[100005];
vector<vector<int>>ls(100005);
void dfs(vector<vector<int>>ls,int s) {
	viz[s] = 1;
	
	for (int x : ls[s]) {
		if (viz[x] == 0) {
			dfs(ls,x);
		}
		
	}
	

}


int main() {
	int n, m;
	in >> n >> m;
	int contor = 0;
	while (m--) {
		int a, b;
		in >> a >> b;
		ls[a].push_back(b);
		ls[b].push_back(a);
	}
	
	for (int i = 1; i <=n; i++)
	{
		if (viz[i] == 0)
		{
			dfs( ls,i);
			contor++;
		}
	}
	out << contor;
	return 0;
}