Language: C++

#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("inline")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#include <bits/stdc++.h>
#define int long long
#define double long double
#define x first
#define y second
#define yy cout<<"Yes\n"
#define nn cout<<"No\n"
#define rt return
#define ct continue
#define br break
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define mmod 1000000007
#define pmod 998244353
using namespace std;
int gcd(int a,int b){
    while (b) b^=a^=b^=a%=b;
    return a;
}
int lcm(int a,int b){
    return a*b/gcd(a,b);
}
int bigmod(int b,int p,int m){
    int ans=1;
    b%=m;
    if (!b) return 0;
    while (p){
        if (p&1) ans=(ans*b)%m;
        p>>=1;
        b=(b*b)%m;
    }
    return ans;
}
int ncr(int n,int r){
	if (n<r) return 0ll;
	int ans=1,k=min(n-r,r);
	for (int i=1; i<=k; i++) ans=ans*(n-i+1)/i;
	return ans;
}
double eu(int x1,int y1,int x2,int y2){
	int dx=abs(x1-x2),dy=abs(y1-y2);
	return sqrt(dx*dx+dy*dy);
}
int ham(string s,string t){
	int ans=0;
	for (int i=0; i<s.size(); i++) ans+=(s[i]!=t[i]);
	return ans;
}
int ce(int a,int b){
	if (a%b==0) return a/b;
	else return a/b+1;
}
bool cmp(){
	return 1;
}
int dsu[100001];
int prt(int n){
	if (dsu[n]==n) return n;
	return dsu[n]=prt(dsu[n]);
}
void unn(int x,int y){
	if (prt(x)==prt(y)) rt;
	dsu[prt(x)]=dsu[prt(y)];
}
void solve(){
	int n,m;
	cin>>n>>m;
	for (int i=1; i<=n; i++) dsu[i]=i;
	for (int i=0; i<m; i++){
		int x,y;
		cin>>x>>y;
		unn(x,y);
	}
	set <int> s;
	for (int i=1; i<=n; i++){
		s.insert(dsu[i]);
	}
	cout<<s.size()-1;
	
	
	
}
signed main(){
	//freopen("input.in","r",stdin);
	//freopen("output.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    //cin>>t;
    while (t--) solve();
}

Compiler output:
a.cc:12:51: warning: bad option '-funsafe-loop-optimizations' to pragma 'optimize' [-Wpragmas]
   12 | #pragma GCC optimize("-funsafe-loop-optimizations")
      |                                                   ^
a.cc:29:20: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   29 | int gcd(int a,int b){
      |                    ^
a.cc:33:20: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   33 | int lcm(int a,int b){
      |                    ^
a.cc:36:29: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   36 | int bigmod(int b,int p,int m){
      |                             ^
a.cc:47:20: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   47 | int ncr(int n,int r){
      |                    ^
a.cc:53:38: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   53 | double eu(int x1,int y1,int x2,int y2){
      |                                      ^
a.cc:57:26: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   57 | int ham(string s,string t){
      |                          ^
a.cc:62:19: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   62 | int ce(int a,int b){
      |                   ^
a.cc:66:10: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   66 | bool cmp(){
      |          ^
a.cc:70:14: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   70 | int prt(int n){
      |              ^
a.cc:74:21: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   74 | void unn(int x,int y){
      |                     ^
a.cc:78:12: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   78 | void solve(){
      |            ^
a.cc:96:13: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   96 | signed main(){
      |             ^
In file included from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:13:
/usr/include/c++/14/bits/allocator.h: In destructor 'std::_Rb_tree<long long int, long long int, std::_Identity<long long int>, std::less<long long int>, std::allocator<long long int> >::_Rb_tree_impl<std::less<long long int>, true>::~_Rb_tree_impl()':
/usr/include/c++/14/bits/allocator.h:182:7: error: inlining failed in call to 'always_inline' 'std::allocator< <template-parameter-1-1> >::~allocator() noexcept [with _Tp = std::_Rb_tree_node<long long int>]': target specific option mismatch
  182 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /usr/include/c++/14/map:62,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:152:
/usr/include/c++/14/bits/stl_tree.h:658:16: note: called from here
  658 |         struct _Rb_tree_impl
      |                ^~~~~~~~~~~~~