Pagini recente » Cod sursa (job #159016) | Cod sursa (job #2132867) | Cod sursa (job #2407313) | Cod sursa (job #1896661) | Cod sursa (job #2195438)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream in ("dfs.in");
ofstream out ("dfs.out");
int const nmax = 100000;
vector < int > g[5 + nmax];
int seen[5 + nmax];
void flood(int node){
seen[node] = 1;
for(int h = 0 ; h < g[node].size() ;h++){
int to = g[node][h];
if(seen[to] == 0)
flood(to);
}
}
int main()
{
int n , m;
in >> n >> m;
for(int i = 1 ; i <= m ;i++){
int x , y;
in >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
int sum = 0;
for(int i = 1 ; i <= n ;i++)
if(seen[i] == 0){
flood(i);
seen[i] = 1;
sum ++ ;
}
out << sum;
return 0;
}