Pagini recente » Cod sursa (job #2594508) | Cod sursa (job #804873) | Cod sursa (job #2321265) | Cod sursa (job #2665228) | Cod sursa (job #2348389)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
vector <int> graph[100000];
int viz[100000];
void DFS(int node) {
int i;
viz[node] = 1;
int lim = graph[node].size();
for( i = 0; i < lim; i++ )
if( !viz[ graph[node][i] ] )
DFS(graph[node][i]);
}
int main()
{
int n, m;
f >> n >> m;
int i;
for( i = 0; i < m; i++ ) {
int x, y;
f >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
int compConexe = 0;
for( i = 1; i <= n; i++ ) {
if( !viz[i] ) {
compConexe++;
DFS(i);
}
}
g << compConexe;
return 0;
}