Pagini recente » Cod sursa (job #3316898) | Cod sursa (job #3318370) | Cod sursa (job #3317159) | Cod sursa (job #3318369) | Cod sursa (job #3317149)
#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;
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;
}