Pagini recente » Cod sursa (job #2687089) | Cod sursa (job #2903502)
#include <vector>
#include <fstream>
#include <bitset>
using namespace std;
const int N = 1e5;
vector <int> a[N + 1];
bitset<N + 1>viz;
int n;
ifstream in("dfs.in");
ofstream out("dfs.out");
void dfs(int x)
{
viz[x] = 1;
for (auto y : a[x]) {
if (!viz[y]) {
dfs(y);
}
}
}
int main()
{
int m, x,nc=0;
in >> n >> m ;
for (int y = 0; y < m; y++)
{
int i, j;
in >> i >> j;
a[i].push_back(j);
a[j].push_back(i);
}
for (int y = 1; y <= n; y++) {
if (!viz[y]) {
dfs(y);
nc++;
}
}
out << nc;
return 0;
}