Cod sursa(job #3235174)

Utilizator Cezar2009Cezar Mihai Titihazan Cezar2009 Data 15 iunie 2024 21:05:49
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
//https://infoarena.ro/problema/bfs
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

ifstream fin("dfs.in");
ofstream fout("dfs.out");

vector<vector<int>> gr;
bool b[100010];
int n, m;
void dfs(int vf)
{
	//cout << vf << " ";
	b[vf] = true;
	for (int x : gr[vf])
	{
		if (!b[x])
		{
			dfs(x);
		}
	}
}
int main()
{
	int i, rez=0;
	fin >> n >> m;
	gr.resize(n + 1);
	for (i = 1; i <= m; ++i)
	{
		int a, b;
		fin >> a >> b;
		gr[a].push_back(b);
		gr[b].push_back(a);
	}
	for (i = 1; i <= n; ++i)
	{
		//cout << i << " " << b[i] << "\n";
		if (b[i] == 0)
		{
			++rez;
			dfs(i);
		}
	}
	fout << rez;
	
	return 0;
}