Pagini recente » Cod sursa (job #2313475) | Cod sursa (job #2920023) | Cod sursa (job #535390) | Cod sursa (job #3173308) | Cod sursa (job #736532)
Cod sursa(job #736532)
#include <vector>
#include <fstream>
#include <cstdlib>
using namespace std;
const int N_MAX=100011;
bool was[N_MAX];
vector<int> G[N_MAX];
inline void DFS(int x)
{
was[x]=true;
vector<int>::const_iterator it=G[x].begin(), iend=G[x].end();
for(; it < iend; ++it)
if(false == was[*it])
DFS(*it);
}
int main()
{
int N, M, x, y, count=0;
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);
}
for(x=1; x <= N; ++x)
if(false == was[x])
{
++count;
DFS(x);
}
out<<count<<'\n';
return EXIT_SUCCESS;
}