Cod sursa(job #2600237)

Utilizator cristia_razvanCristia Razvan cristia_razvan Data 12 aprilie 2020 12:30:43
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.55 kb
#include <bits/stdc++.h>
using namespace std;

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


int n, m, comp = 0;
bitset<100005> viz;
vector <int> edge[100005];

void DFS(int nod)
{
	viz[nod] = 1;
	for (auto i : edge[nod]) 
		if (!viz[i])
			DFS(i);
}

int main()
{
	int x, y;
	fin >> n >> m;
	while (m--)
	{
		fin >> x >> y;
		edge[x].push_back(y);
		edge[y].push_back(x);
	}

	for (int i = 1; i <= n; i++)
		if (!viz[i])
		{
			comp++;
			DFS(i);
		}
	fout << comp << "\n";
	fin.close();
	fout.close();
	return 0;
}