Pagini recente » Cod sursa (job #2390641) | Cod sursa (job #1382345) | Cod sursa (job #204164) | Cod sursa (job #2704455) | Cod sursa (job #2596190)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n, m, sol;
vector<int> graph[100005];
bitset<100005> check;
void dfs(int now);
int main()
{
fin >> n >> m;
while(m--){
int x, y;
fin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
for(int i = 1; i <= n; ++i){
if(!check[i]){
++sol;
dfs(i);
}
}
fout << sol;
return 0;
}
void dfs(int now){
check[now] = true;
for(auto next:graph[now]){
if(check[next]) continue;
dfs(next);
}
}