Cod sursa(job #2501762)
| Utilizator | Data | 30 noiembrie 2019 10:15:42 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.6 kb |
#include <fstream>
#include <vector>
using namespace std;
vector<int>graph[100005];
ifstream f("dfs.in");
ofstream g("dfs.out");
int n, m, x, y;
int viz[100005];
int nr;
void dfs(int i){
for(auto &v:graph[i])
if(viz[v]==0){
viz[v]=1;
dfs(v);
}
}
void citire(){
f>>n>>m;
for(int i=1; i<=m; i++){
f>>x>>y;
graph[x].push_back(y);
graph[y].push_back(x);
}
}
int main(){
citire();
for(int i=1; i<=n; i++)
if(viz[i]==0){
viz[i]=1;
dfs(i);
nr++;
}
g<<nr;
return 0;
}