Cod sursa(job #3122456)

Utilizator dora1810Olariu Alexandra Teodora dora1810 Data 19 aprilie 2023 10:03:32
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include<map>
#include<algorithm>
#include<string>
#include <queue>
#define maxiim 1e5;
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
int n, m,i;
vector<vector<int>>sir;
vector<bool>visited;

void stabilizare()
{
    sir.resize(n+1);
	visited.resize(n+1);
	for (int i = 0; i < n + 1; i++)
		visited[i] = false;
	
}
void DFS(int x)
{
	visited[x] = true;
	for (int k : sir[x])
		if (!visited[k])
			DFS(k);
}
int main()
{
	in >> n >> m;
	stabilizare();
	for (int i = 0; i < m; i++)
	{
		int x, y;
		in >> x >> y;
		sir[x].push_back(y);
		sir[y].push_back(x);
	}
	int nr = 0;
	for (int i = 1; i <= n; i++)
	{
		if (!visited[i]) {
			DFS(i);
			nr++;
		}
	}
	
	out << nr;
	return 0;
}