Language: C++

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>
#include <cassert>


using namespace std;
//using namespace atcoder;

#define REP(i, n) for (int i=0; i<(n); ++i)
#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)
#define FOR(i, a, n) for (int i=(a); i<(n); ++i)
#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()

#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    os << "[";
    REP(i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}

template<class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    return os << "(" << p.first << " " << p.second << ")";
}

template<class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;


// edit

struct UnionFind {
    vector<int> par, sz;

    UnionFind(int n) : par(n), sz(n, 1) {
        for (int i = 0; i < n; ++i) par[i] = i;
    }

    int root(int x) {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void merge(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return;
        if (sz[x] < sz[y]) swap(x, y);
        par[y] = x;
        sz[x] += sz[y];
        sz[y] = 0;
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    int size(int x) {
        return sz[root(x)];
    }
};

void solve() {
    int N, M;
    cin >> N >> M;
    UnionFind uf(N);
    REP(i, M) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        uf.merge(a, b);
    }
    set<int> st;
    for (int i = 0; i < N; ++i) {
        st.insert(uf.root(i));
    }
    cout << (int) st.size() - 1 << endl;

}
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>
#include <cassert>


using namespace std;
//using namespace atcoder;

#define REP(i, n) for (int i=0; i<(n); ++i)
#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)
#define FOR(i, a, n) for (int i=(a); i<(n); ++i)
#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()

#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    os << "[";
    REP(i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}

template<class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    return os << "(" << p.first << " " << p.second << ")";
}

template<class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;


// edit

struct UnionFind {
    vector<int> par, sz;

    UnionFind(int n) : par(n), sz(n, 1) {
        for (int i = 0; i < n; ++i) par[i] = i;
    }

    int root(int x) {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void merge(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return;
        if (sz[x] < sz[y]) swap(x, y);
        par[y] = x;
        sz[x] += sz[y];
        sz[y] = 0;
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    int size(int x) {
        return sz[root(x)];
    }
};

void solve() {
    int N, M;
    cin >> N >> M;
    UnionFind uf(N);
    REP(i, M) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        uf.merge(a, b);
    }
    set<int> st;
    for (int i = 0; i < N; ++i) {
        st.insert(uf.root(i));
    }
    cout << (int) st.size() - 1 << endl;

}


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
//    std::ifstream in("input.txt");
//    std::cin.rdbuf(in.rdbuf());

    solve();


    return 0;
}


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
//    std::ifstream in("input.txt");
//    std::cin.rdbuf(in.rdbuf());

    solve();


    return 0;
}


Compiler output:
a.cc:202:10: error: redefinition of 'template<class T> std::ostream& operator<<(std::ostream&, const std::vector<_Tp>&)'
  202 | ostream &operator<<(ostream &os, const vector<T> &v) {
      |          ^~~~~~~~
a.cc:53:10: note: 'template<class T> std::ostream& operator<<(std::ostream&, const std::vector<_Tp>&)' previously declared here
   53 | ostream &operator<<(ostream &os, const vector<T> &v) {
      |          ^~~~~~~~
a.cc:212:10: error: redefinition of 'template<class T, class U> std::ostream& operator<<(std::ostream&, const std::pair<_T1, _T2>&)'
  212 | ostream &operator<<(ostream &os, const pair<T, U> &p) {
      |          ^~~~~~~~
a.cc:63:10: note: 'template<class T, class U> std::ostream& operator<<(std::ostream&, const std::pair<_T1, _T2>&)' previously declared here
   63 | ostream &operator<<(ostream &os, const pair<T, U> &p) {
      |          ^~~~~~~~
a.cc:217:6: error: redefinition of 'template<class T> bool chmax(T&, const T&)'
  217 | bool chmax(T &a, const T &b) {
      |      ^~~~~
a.cc:68:6: note: 'template<class T> bool chmax(T&, const T&)' previously declared here
   68 | bool chmax(T &a, const T &b) {
      |      ^~~~~
a.cc:226:6: error: redefinition of 'template<class T> bool chmin(T&, const T&)'
  226 | bool chmin(T &a, const T &b) {
      |      ^~~~~
a.cc:77:6: note: 'template<class T> bool chmin(T&, const T&)' previously declared here
   77 | bool chmin(T &a, const T &b) {
      |      ^~~~~
a.cc:243:10: error: redefinition of 'const ll MOD'
  243 | const ll MOD = 1e9 + 7;
      |          ^~~
a.cc:94:10: note: 'const ll MOD' previously defined here
   94 | const ll MOD = 1e9 + 7;
      |          ^~~
a.cc:244:11: error: redefinition of 'const int INF'
  244 | const int INF = INT_MAX / 2;
      |           ^~~
a.cc:95:11: note: 'const int INF' previously defined here
   95 | const int INF = INT_MAX / 2;
      |           ^~~
a.cc:245:10: error: redefinition of 'const ll LINF'
  245 | const ll LINF = LLONG_MAX / 2;
      |          ^~~~
a.cc:96:10: note: 'const ll LINF' previously defined here
   96 | const ll LINF = LLONG_MAX / 2;
      |          ^~~~
a.cc:246:10: error: redefinition of 'const ld eps'
  246 | const ld eps = 1e-9;
      |          ^~~
a.cc:97:10: note: 'const ld eps' previously defined here
   97 | const ld eps = 1e-9;
      |          ^~~
a.cc:251:8: error: redefinition of 'struct UnionFind'
  251 | struct UnionFind {
      |        ^~~~~~~~~
a.cc:102:8: note: previous definition of 'struct UnionFind'
  102 | struct UnionFind {
      |        ^~~~~~~~~
a.cc:282:6: error: redefinition of 'void solve()'
  282 | void solve() {
      |      ^~~~~
a.cc:133:6: note: 'void solve()' previously defined here
  133 | void solve() {
      |      ^~~~~
a.cc:315:5: error: redefinition of 'int main()'
  315 | int main() {
      |     ^~~~
a.cc:301:5: note: 'int main()' previously defined here
  301 | int main() {
      |     ^~~~