Pagini recente » Cod sursa (job #309583) | Cod sursa (job #2031051) | Clasament noaptea_burlacilor | Cod sursa (job #1612556) | Cod sursa (job #2787457)
#include <fstream>
#include <vector>
using namespace std;
ifstream in("dfs.in");
ofstream out("dfs.out");
vector<int> g[100001];
int n, m, k;
bool f[100001];
void read()
{
int x, y;
in >> n >> m;
for(int i = 0; i < m; ++i)
{
in >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
}
void dfs(int s)
{
f[s] = 1;
for(auto i : g[s])
if(!f[i]) dfs(i);
}
int main()
{
read();
for(int i = 1; i <= n; ++i)
if(!f[i]) k++, dfs(i);
out << k;
return 0;
}