Cod sursa(job #3248575)
| Utilizator | Data | 12 octombrie 2024 10:42:30 | |
|---|---|---|---|
| Problema | Parcurgere DFS - componente conexe | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.66 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
#define SIZE 100001
#define cin fin
#define cout fout
int n, m;
bool v[SIZE];
vector<int> a[SIZE];
int rez;
void dfs(int p){
// int s=a[p].size();
v[p]=1;
for(auto i: a[p]){
if(!v[i]){
dfs(i);
}
}
}
int main()
{
cin>>n>>m;
for(int i=1; i<=m; i++){
int x, y;
cin>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
for(int k=1; k<=n; k++){
if(!v[k]){
dfs(k);
rez++;
}
}
cout<<rez;
return 0;
}
