Cod sursa(job #2718791)
| Utilizator | Data | 9 martie 2021 10:25:44 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.59 kb |
#include <fstream>
#include <vector>
using namespace std;
ifstream fi("dfs.in");
ofstream fo("dfs.out");
int n,m,s,cc[100001];
vector<int>v[100001];
void DFS(int p, int nrcc)
{
size_t i;
cc[p] = nrcc;
for(i=0; i<v[p].size(); i++)
if(cc[v[p][i]] == 0)
DFS(v[p][i], nrcc);
}
int main()
{
int i,x,y, nrcc = 0;
fi >> n >> m;
while(fi >> x >> y)
{
v[x].push_back(y);
v[y].push_back(x);
}
for(i=1; i<=n; i++)
{
if(cc[i] == 0)
DFS(i, ++nrcc);
}
fo << nrcc;
return 0;
}
