Cod sursa(job #2492529)
| Utilizator | Data | 14 noiembrie 2019 21:07:22 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 50 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.65 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int mtA[1001][1001], v[1001];
int N;
void dfs (int i) {
v[i] = 1;
for(int j = 1; j <= N; j++) {
if(mtA[i][j] == 1 && v[j] == 0)
dfs(j);
}
}
int main()
{
int M, nrComp = 0;
f >> N >> M;
while (M--) {
int x, y;
f >> x >> y;
if(x != y) {
mtA[x][y] = 1;
mtA[y][x] = 1;
}
}
for(int i = 1; i <= N; i++) {
if(v[i] == 0) {
nrComp++;
dfs(i);
}
}
g << nrComp;
return 0;
}
