Pagini recente » Cod sursa (job #2577455) | Cod sursa (job #2371702) | Cod sursa (job #778155) | Cod sursa (job #1944701) | Cod sursa (job #1087713)
#include <fstream>
#include <vector>
using namespace std;
const int N = 1 + 1e5;
vector<int> graph[N];
bool use[N];
ifstream in("dfs.in");
ofstream out("dfs.out");
void dfs(int x){
use[x] = true;
for (vector<int> :: iterator it = graph[x].begin() ; it != graph[x].end() ; it++)
if (!use[ *it ])
dfs( *it );
}
int main(){
int n, m, x, y, ans = 0;
in >> n >> m;
while (m--){
in >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
for (int i = 1 ; i <= n ; i++)
if (!use[i]){
dfs(i);
ans++;
}
out << ans << "\n";
return 0;
}