Cod sursa(job #3203247)

Utilizator AlexMoto2006Motoasca Alexandru-Lucian AlexMoto2006 Data 13 februarie 2024 13:08:52
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream cin("dfs.in");
ofstream cout("dfs.out");
vector<vector<int>> v;
int n,m;
int max1 = 100000;
int viz[100001];

void dfs(int x)
{
	viz[x] = 1;
	for (int i = 0; i < v[x].size(); i++)
	{
		if (viz[v[x][i]] == 0)
		{
			dfs(v[x][i]);
		}
	}
}

int main()
{
	int nr = 0;
	cin >> n >> m;
	int x, y;
	for(int i=1;i<=m;i++)
	{
		cin >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	for (int i = 1; i <= n; i++)
	{
		if (viz[i] == 0)
		{
			nr++;
			dfs(i);
		}
	}
	cout<< nr;
    return 0;
}