Cod sursa(job #3376426)
| Utilizator | Data | 17 noiembrie 2026 17:57:54 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.72 kb |
#include <bits/stdc++.h>
using namespace std;
vector<bool> seen(1e6+1);
vector<vector<int>> adj(1e6+1);
ifstream fin("dfs.in");
ofstream fout("dfs.out");
void dfs(int n){
if (seen[n]) return;
seen[n] = true;
for (auto u : adj[n]){
dfs(u);
}
}
int main(){
int n, m;
fin >> n >> m;
while (m--){
int x, y;
fin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
int cnt=0;
for (int i = 1; i <= n; i++){
if (!seen[i]){
cnt++;
dfs(i);
}
}
fout << cnt;
return 0;
}
