Cod sursa(job #2814960)
| Utilizator | Data | 8 decembrie 2021 21:19:37 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 50 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.52 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("dfs.in");
ofstream g("dfs.out");
int a[1005][1005], n, m, viz[1005], c,x,y,i,j;
void citire()
{
f >> n >> m;
while(f>>x>>y)
{
a[x][y] = 1;
a[y][x] = 1;
}
}
void DFS(int nod)
{
int i;
viz[nod] = 1;
for (i = 1; i <= n; i++)
if (viz[i] != 1 && a[nod][i] == 1)
{
viz[i] = 1;
DFS(i);
}
}
int main()
{
citire();
int i;
for (i = 1; i <= n; i++)
if (viz[i]==0)
{ c++;
DFS(i); }
g << c;
}