Cod sursa(job #3238021)
| Utilizator | Data | 15 iulie 2024 14:38:42 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 10 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.65 kb |
#include <fstream>
#define cin fin
#define cout fout
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int n, m, sol;
bool A[101][101], viz[101];
void citire()
{
int x, y;
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
cin >> x >> y;
A[x][y] = A[y][x] = 1;
}
}
void dfs(int nod)
{
viz[nod] = 1;
for(int i = 1; i <= n; i++)
if(A[nod][i] && !viz[i])
dfs(i);
}
int main()
{
citire();
for(int i = 1; i <= n; i++)
if(!viz[i])
{
++sol;
dfs(i);
}
cout << sol;
return 0;
}
