Cod sursa(job #2091254)
| Utilizator | Data | 19 decembrie 2017 13:40:52 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.65 kb |
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
void DFS(int x);
int n, m, x, nr, uz[100005];
vector<int> L[100005];
int main()
{
int i, j, x, y, c;
fin >> n >> m;
for (i=1;i<=m;i++)
{
fin >> x >> y;
L[x].push_back(y);
L[y].push_back(x);
}
for (i=1;i<=n;i++)
if (!uz[i])
{
DFS(i);
nr++;
}
fout << nr << '\n';
return 0;
}
void DFS(int x)
{
int i;
uz[x] = 1;
for (i=0;i<L[x].size();i++)
if (!uz[L[x][i]])
DFS(L[x][i]);
}
