Pagini recente » Cod sursa (job #2326474) | Cod sursa (job #3120498) | Cod sursa (job #86296) | Cod sursa (job #118290) | Cod sursa (job #914332)
Cod sursa(job #914332)
#include <fstream>
#include <vector>
using namespace std;
const int N = 100001;
vector<int> graph[N];
bool use[N];
int n;
ifstream in("dfs.in");
ofstream out("dfs.out");
void dfs(int x){
use[x] = false;
for (unsigned int i = 0 ; i < graph[x].size() ; i++)
if (!use[ graph[x][i] ])
dfs( graph[x][i] );
}
int main(){
int m, x, y;
in >> n >> m;
while (m--){
in >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
int nr = 0;
for (int i = 1 ; i <= n ; i++)
if (!use[i]){
dfs(i);
++nr;
}
out << nr << "\n";
return 0;
}