Cod sursa(job #3250901)
| Utilizator | Data | 24 octombrie 2024 09:04:33 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.56 kb |
#include<bits/stdc++.h>
using namespace std;
int n,m;
vector<list<int>>a;
int viz[100001];
void DFS(int t)
{
viz[t]=1;
for(auto i:a[t])
if(!viz[i])
DFS(i);
}
int main()
{
int x,y;
int nrcomp=0;
cin>>n>>m;
a.resize(n+1);
for(int i=1;i<=m;i++)
{
cin>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
for(int nod=1;nod<=n;nod++)
if(viz[nod]==0)
{
nrcomp++;
DFS(nod);
}
cout<<nrcomp;
return 0;
}
