Cod sursa(job #3316694)
| Utilizator | Data | 19 octombrie 2025 23:33:56 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.52 kb |
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int v[100001];
void dfs(int s, vector< vector <int> > &g, int n){
v[s] = 1;
for(int i : g[s]){
if(!v[i]){
dfs(i, g, n);
}
}
}
int main(){
int n, m, x, y, c=0;
fin >> n >> m;
vector< vector<int> > g(n+1);
for(int i = 0; i < m; i++){
fin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for(int i = 1; i <= n; i++){
if(!v[i]){
dfs(i, g, n);
c++;
}
}
fout << c;
return 0;
}
