Pagini recente » Cod sursa (job #182896) | Cod sursa (job #947419)
Cod sursa(job #947419)
#include <vector>
#include <cstdlib>
#include <fstream>
using namespace std;
const int NMAX = 100011;
bool was[NMAX];
vector<int> G[NMAX];
inline void DFS(int x)
{
was[x] = true;
for(auto& y : G[x])
{
if(!was[y])
{
DFS(y);
}
}
}
int main()
{
int N, M, x, y, count;
ifstream in("dfs.in");
ofstream out("dfs.out");
for(in >> N >> M; M; --M)
{
in >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
count = 0;
for(x = 1; x <= N; ++x)
{
if(!was[x])
{
++count;
DFS(x);
}
}
out << count << '\n';
}