Pagini recente » Cod sursa (job #2301318) | Cod sursa (job #2752419) | Cod sursa (job #1663595) | Cod sursa (job #1557154) | Cod sursa (job #2952533)
#include <iostream>
#include <vector>
#include <bitset>
#include <fstream>
using namespace std;
const int N = 1e5;
const int M = 2e5;
vector <int> a[N + 1];
bitset <N + 1> viz;
void dfs(int x){
viz[x] = 1;
for(auto y:a[x])
if(!viz[y])
dfs(y);
}
int main()
{
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n, m;
fin >> n >> m;
for(int i = 0; i < m; i++){
int x, y;
fin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
int nrc = 0;
for(int i = 1; i <= n; i++){
if(!viz[i]){
nrc++;
dfs(i);
}
}
fout << nrc;
return 0;
}