Pagini recente » Cod sursa (job #2610331) | Cod sursa (job #1221792) | Cod sursa (job #972953) | Cod sursa (job #1067560) | Cod sursa (job #467566)
Cod sursa(job #467566)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const char iname[] = "dfs.in";
const char oname[] = "dfs.out";
const int nmax = 100005;
ifstream fin(iname);
ofstream fout(oname);
int i, j, N, M, viz[nmax], cnt, x, y;
vector<int> Nod[nmax];
void read()
{
fin >> N;
fin >> M;
for(i = 1; i <= M; i ++)
{
fin >> x >> y;
Nod[x].push_back(y);
Nod[y].push_back(x);
}
}
void DFS(int curr)
{
vector<int>::iterator it;
viz[curr] = 1;
for(it = Nod[curr].begin(); it != Nod[curr].end(); ++it)
if(!viz[*it]) DFS(*it);
}
int main()
{
read();
for(i = 1; i <= N; i ++)
{
if(!viz[i])
{
cnt ++;
DFS(i);
}
}
fout << cnt;
return 0;
}