Cod sursa(job #2880097)
| Utilizator | Data | 29 martie 2022 13:59:22 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.45 kb |
#include<fstream>
#include<algorithm>
#include<vector>
using namespace std;
ifstream f ("dfs.in");
ofstream g("dfs.out");
vector <int> a[100001];
int i,j,n,m,v[100001],k,x,y;
void dfs(int p)
{v[p]=1;
for(int q=0;q<a[p].size();q++)
{if(v[a[p][q]]==0)
dfs(a[p][q]);
}
}
int main()
{f>>n>>m;
for(i=1;i<=m;i++)
{f>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
for(j=1;j<=n;j++)
if(v[j]==0)
{k++;
dfs(j);
}
g<<k;
}
