Cod sursa(job #2538368)
| Utilizator | Data | 4 februarie 2020 18:12:49 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.54 kb |
#include <bits/stdc++.h>
using namespace std;
int n,m,nr;
ifstream f("dfs.in");
ofstream g("dfs.out");
vector <int> v[100];
bool vif[100];
void dfs(int x){
vif[x]=true;
for(auto i:v[x]){
if(vif[i]==false)dfs(i);
}
}
int main()
{f>>n>>m;
for(int i=1;i<=m;i++){
int x,y;
f>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
for(int k=1;k<=n;k++){
if(vif[k]==false){
nr++;
dfs(k);
}
}
cout<<nr;
return 0;
}
