Pagini recente » Cod sursa (job #635207) | Cod sursa (job #2908795) | Cod sursa (job #2211667) | Cod sursa (job #696234) | Cod sursa (job #2422880)
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
vector <int> graph[100009];
int viz[100009], n, m;
void read() {
f >> n >> m;
for(int i = 0; i < m; i++) {
int x, y;
f >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
}
void dfs( int node ) {
int lim = graph[node].size();
viz[node] = 1;
for(int i = 0; i < lim; i++) {
if( !viz[ graph[node][i] ] )
dfs(graph[node][i]);
}
}
int noCompConex() {
int i;
int nrCom = 0;
for( i = 1; i <= n; i++ )
if( !viz[i] ) {
nrCom++;
dfs(i);
}
return nrCom;
}
using namespace std;
int main() {
read();
g << noCompConex();
return 0;
}