Pagini recente » Cod sursa (job #2788547) | Profil summer21 | Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #3314578)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dfs.in");
ofstream fout("dfs.out");
int height[100005], viz[100005], x, y, n, m, cc;
int fnd(int x){
if(x==viz[x])return x;
return fnd(viz[x]);
}
void unite(int a, int b){
a=fnd(a);
b=fnd(b);
if(height[a]>height[b]){
viz[b]=a;
}
else{
viz[a]=b;
}
if(height[a]==height[b]){
height[b]++;
}
}
int main()
{
fin>>n>>m;
cc=n;
for(int i=1; i<=n; i++){
viz[i]=i;
height[i]=1;
}
for(int i=1; i<=n; i++){
fin>>x>>y;
if(viz[x]!=viz[y]){
unite(x, y);
cc--;
}
}
fout<<cc;
return 0;
}