Cod sursa(job #3156403)
| Utilizator | Data | 11 octombrie 2023 16:28:26 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 0 |
| Compilator | cpp-32 | Status | done |
| Runda | Arhiva educationala | Marime | 0.6 kb |
#include <iostream>
#include <vector>
using namespace std;
const int nmax=100000;
vector<int> G[nmax+1];
int viz[nmax+1];
void DFS(int x)
{
viz[x]=1;
for(auto next: G[x])
{
if(!viz[next])
DFS(next);
}
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
int cc=0;
for(int i=1;i<=n;i++)
{
if(!viz[i])
{
cc++;
DFS(i);
}
}
cout<<cc;
return 0;
}
