Language: C++

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
typedef long long ll;
typedef pair<ll,ll> P;
using vi=vector<ll>;
using vvi=vector<vector<ll>>;
using vc=vector<char>;
using vvc=vector<vector<char>>;
const int inf=1000000007;
const int mod=1000000007;
const int max_n=100000;
void chmin(auto &a,auto b){if(b<a)a=b;}
void chmax(auto &a,auto b){if(a<b)a=b;}
vi parent(max_n);
vi depth(max_n,0);
int root(int i){if(par[i]==i)return i;return find(par[i]);}
void heigou(int x,y){
  x=root(x);y=root(y);
  if(x==y)return;
  if(depth[x]<depth[y])parent[x]=y;
  else if(depth[x]>depth[y])parent[y]=x;
  else{parent[y]=x;depth[x]++;}
}
int main(){
  int n,m,ans=0;cin>>n>>m;
  vi v;
  rep(i,parent.size())parent[i]=i;
  rep(i,m){
    int a,b,c;cin>>a>>b>>c;
    heigou(a,b);
  }
  rep(i,n){
    v.push_back(root(i));
  }
  sort(v.begin(),v.end());
  rep(i,n-1){
    if(v[i]!=v[i+1])ans++;
  }
  cout<<ans<<endl;
}

Compiler output:
a.cc:13:12: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
   13 | void chmin(auto &a,auto b){if(b<a)a=b;}
      |            ^~~~
a.cc:13:20: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
   13 | void chmin(auto &a,auto b){if(b<a)a=b;}
      |                    ^~~~
a.cc:14:12: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
   14 | void chmax(auto &a,auto b){if(a<b)a=b;}
      |            ^~~~
a.cc:14:20: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
   14 | void chmax(auto &a,auto b){if(a<b)a=b;}
      |                    ^~~~
a.cc: In function 'int root(int)':
a.cc:17:20: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   17 | int root(int i){if(par[i]==i)return i;return find(par[i]);}
      |                    ^~~
      |                    __pstl::execution::v1::par
In file included from /usr/include/c++/14/pstl/glue_algorithm_defs.h:15,
                 from /usr/include/c++/14/algorithm:86,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/pstl/execution_defs.h:43:45: note: '__pstl::execution::v1::par' declared here
   43 | _GLIBCXX17_INLINE constexpr parallel_policy par{};
      |                                             ^~~
a.cc:17:51: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   17 | int root(int i){if(par[i]==i)return i;return find(par[i]);}
      |                                                   ^~~
      |                                                   __pstl::execution::v1::par
/usr/include/c++/14/pstl/execution_defs.h:43:45: note: '__pstl::execution::v1::par' declared here
   43 | _GLIBCXX17_INLINE constexpr parallel_policy par{};
      |                                             ^~~
a.cc: At global scope:
a.cc:18:19: error: 'y' has not been declared
   18 | void heigou(int x,y){
      |                   ^
a.cc: In function 'void heigou(int, int)':
a.cc:19:13: error: 'y' was not declared in this scope
   19 |   x=root(x);y=root(y);
      |             ^