Pagini recente » Cod sursa (job #165357) | Cod sursa (job #1503339) | Cod sursa (job #2467242) | Cod sursa (job #2473425) | Cod sursa (job #2749434)
#include <iostream>
#include <vector>
#include <bitset>
#include <fstream>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
#define NMAX 100005
int n, m, nr;
vector<int> tree[NMAX];
bitset check;
void read()
{
fin>>n>>m;
int a, b;
for(int i=0; i<m; i++)
{
fin>>a>>b;
tree[a].push_back(b);
tree[b].push_back(a);
}
}
void HowdyNeighbour(int node)
{
check.set(node);
for(auto &neighbour:tree[node])
if(!check[neighbour])
HowdyNeighbour(neighbour);
}
void walk()
{
for(int i=1; i<=n; i++)
if(!check[i])
{
nr++;
HowdyNeighbour(i);
}
}
int main()
{
read();
walk();
fout<<nr;
return 0;
}