Cod sursa(job #3195745)
| Utilizator | Data | 21 ianuarie 2024 16:24:12 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.56 kb |
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
vector<int> l[200001];
bool viz[100001];
void dfs(int node){
for(int n:l[node]){
if(viz[n]==false){
viz[n]=true;
dfs(n);
}
}
}
int main() {
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n,m,x,y;
fin>>n>>m;
for(int i=1;i<=m;i++){
fin>>x>>y;
l[x].push_back(y);
l[y].push_back(x);
}
int s=0;
for(int i=1;i<=n;i++){
if(viz[i]==false)
dfs(i),s++;
}
fout<<s;
}
