Pagini recente » Cod sursa (job #2718733) | Cod sursa (job #105754) | Cod sursa (job #2129187) | Cod sursa (job #2961599) | Cod sursa (job #1690235)
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
ifstream fin ("dfs.in");
ofstream fout ("dfs.out");
const int nmax = 1e5+5;
vector <int> g[nmax];
bitset <nmax> viz;
void dfs(int dad) {
viz[dad]=true;
for(auto son : g[dad])
if(viz[son]==false) dfs(son);
}
int main() {
ios_base::sync_with_stdio(false);
int n, m, i, x, y, comp=0;
fin >> n >> m;
for(i=1; i<=m; i++) {
fin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for(i=1; i<=n; i++) {
if(viz[i]==false) {
comp++;
dfs(i);
}
}
fout << comp << "\n";
fin.close();
fout.close();
return 0;
}