Pagini recente » Cod sursa (job #2864705) | Cod sursa (job #403341) | Cod sursa (job #3326901) | Borderou de evaluare (job #1264961) | Cod sursa (job #3317152)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
vector<int> vis;
vector<vector<int>> L;
void DFS(int nod){
vis[nod] = 1;
for(int x : L[nod]){
if(vis[x] == 0)
DFS(x);
}
}
int main() {
int n, m;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
fin >> n >> m;
L.resize(n);
vis.resize(n);
// for(int i = 0; i < n; i++){
// L.push_back({});
// vis.push_back(0);
// }
for(int i = 0; i < m; i++){
int x,y;
fin >> x >> y;
L[x-1].push_back(y-1);
L[y-1].push_back(x-1);
}
// for(int i = 0; i < n; i++){
// cout << i << ": ";
// for(int x : L[i])
// cout << x << " ";
// cout << endl;
// }
int nr = 0;
for(int i = 0; i < n; i++){
if(vis[i]==0){
nr++;
DFS(i);
}
}
fout << nr;
fin.close();
fout.close();
return 0;
}