Cod sursa(job #2339503)

Utilizator aurelionutAurel Popa aurelionut Data 9 februarie 2019 00:14:34
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <fstream>
#include <bitset>
#include <vector>

using namespace std;

const int NMAX = 100005;
const int MMAX = 200005;
int n, m;
vector <int> graph[NMAX];
bitset <MMAX> viz;

void DFS(int node)
{
	viz[node] = 1;
	for (auto i : graph[node])
		if (viz[i] == 0)
			DFS(i);
}

int main()
{
	ifstream fin("dfs.in");
	ofstream fout("dfs.out");
	fin >> n >> m;
	for (int i = 1;i <= m;++i)
	{
		int x, y;
		fin >> x >> y;
		graph[x].push_back(y);
		graph[y].push_back(x);
	}
	int ans = 0;
	for (int i = 1;i <= n;++i)
	{
		if (viz[i] == 0)
		{
			DFS(i);
			++ans;
		}
	}
	fout << ans << "\n";
	fin.close();
	fout.close();
}