Cod sursa(job #2706869)

Utilizator UnknownPercentageBuca Mihnea-Vicentiu UnknownPercentage Data 15 februarie 2021 22:13:25
Problema Parcurgere DFS - componente conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.45 kb
#include <bits/stdc++.h>
 
using namespace std;
 
ifstream fin("dfs.in");
ofstream fout("dfs.out");

vector <int> g[100001];
bitset <100001> v;
int answ;

void dfs(int k){
	v[k] = 1;
	for(int x : g[k])
		if(!v[x]) dfs(x);
}

int main(){
	int N, M;
	fin >> N >> M;
	while(M--){
		int x, y;
		fin >> x >> y;
		g[x].emplace_back(y);
		g[y].emplace_back(x);
	}

	for(int i = 1;i <= N;i++)
		if(!v[i]) answ++, dfs(i);
	fout << answ;

}