Cod sursa(job #2862690)

Utilizator QwertyDvorakQwerty Dvorak QwertyDvorak Data 5 martie 2022 18:34:40
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define dbg(x) cout << #x <<": " << x << "\n";
using ll = long long;

const string myf = "dfs";
ifstream fin(myf + ".in");
ofstream fout(myf + ".out");

const  int mod = 1999999973;

int n, m;
vector<int> g[100005];
bitset<100005> viz;
void dfs(int x) {
	viz[x] = 1;
	for (auto i : x)
		if (!viz[i])
			dfs(i);
}
int main() {

	int x, y;
	fin >> n >> m;
	while (m--) {
		fin >> x >> y;
		g[x].pb(y);
		g[y].pb(x);
	}
	int ans = 0;
	for (int i = 1; i <= n; ++i)
		if (!viz[i]) {
			ans++;
			dfs(i);
		}
	fout << ans << '\n';
	fin.close();
	fout.close();
	return 0;
}