Cod sursa(job #2704827)
| Utilizator | Data | 11 februarie 2021 13:26:45 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.62 kb |
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
vector <int>G[100002];
int n,m,i,x,y,nod,nrc;
bool viz[100002];
void DF(int x)
{
viz[x]=1;
for(int j=0;j<G[x].size();j++)
if(viz[G[x][j]]==0)
DF(G[x][j]);
}
int main()
{
fin>>n>>m;
for(i=1;i<=m;i++)
{
fin>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
for(i=1;i<=n;i++)
if(viz[i]==0)
{
nrc++;
DF(i);
}
fout<<nrc;
fin.close();
fout.close();
return 0;
}
