Cod sursa(job #2679413)

Utilizator Andrei21AAnea Andrei Andrei21A Data 30 noiembrie 2020 15:16:01
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <fstream>
#include <vector>

using namespace std;

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

int n, m, x, y, nr;
vector <int> l[10005];
bool viz[10005];


void DFS(int nod)
{
	int i;
	viz[nod] = 1;
	for ( i = 0; i < size(l[nod]); i++)
	{
		if (viz[l[nod][i]] == 0)
			DFS(l[nod][i]);
	}
}

void Citire()
{
	fin >> n >> m;
	for (int i = 1; i <= m; i++)
	{
		fin >> x >> y;
		l[x].push_back(y);
		l[y].push_back(x);
	}
}

void Rezolvare()
{
	int i;
	for (i = 1; i <= n; i++)
	{
		if (viz[i] == 0)
		{
			nr++;
			DFS(i);
		}
	}
	fout << nr;
}
int main()
{
	Citire();
	Rezolvare();
	return 0;
}