Cod sursa(job #3149496)
| Utilizator | Data | 9 septembrie 2023 12:36:10 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.65 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
const int NMAX = 1e5 + 5;
vector<int> G[NMAX];
int n, m;
bool checked[NMAX];
void dfs(int nod){
checked[nod] = true;
for(auto i : G[nod]){
if(!checked[i]){
dfs(i);
}
}
}
int main(){
f >> n >> m;
for(int i = 1; i <= m; ++i){
int x, y;
f >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
int cnt = 0;
for(int i = 1; i <= n; ++i){
if(!checked[i]){
dfs(i);
cnt++;
}
}
g << cnt;
return 0;
}