Cod sursa(job #2282290)
| Utilizator | Data | 13 noiembrie 2018 16:15:30 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.65 kb |
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
vector <int> a[100002];
int n,m;
bool viz[100002];
void citire()
{
fin>>n>>m;
int x,y;
for(int i=0; i<m; i++)
{
fin>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
fin.close();
}
void dfs(int x)
{
viz[x]=true;
for(auto y:a[x])
{
if(!viz[y])
dfs(y);
}
}
int main()
{
citire();
int nr=0;
for(int i=1; i<=n; i++)
if(viz[i]==false)
{
dfs(i);
++nr;
}
fout<<nr;
return 0;
}
