Cod sursa(job #2091700)
| Utilizator | Data | 20 decembrie 2017 08:50:36 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.63 kb |
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int n, m, comp;
bool viz[1000000];
vector <int> a[1000000];
ifstream in("dfs.in");
ofstream out("dfs.out");
void dfs(int node){
if(viz[node] == 1) return;
for(auto i : a[node]){
if(viz[i] == 0) {
comp++;
viz[i] = 1;
}
}
}
int main()
{
int x, y;
in >> n >> m;
for(int i = 1; i <= m; i++){
in >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
for(int i = 1; i <= m; i++){
dfs(i);
}
out << comp;
return 0;
}
