Language: C++

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        int n,m;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer in=new StringTokenizer(br.readLine());
        n=Integer.parseInt(in.nextToken());
        m=Integer.parseInt(in.nextToken());
        Union_Find tree=new Union_Find(n);
        for(int i=0;i<m;i++){
            in=new StringTokenizer(br.readLine());
            int a=Integer.parseInt(in.nextToken()),b=Integer.parseInt(in.nextToken());
            tree.Union(a,b);
        }
        System.out.println(tree.get_tree(n));
    }
}
class Union_Find{
    private int[] t,s;
    public Union_Find(int max_length){
        t=new int[max_length+1];
        s=new int[max_length+1];
        for(int i=0;i<=max_length;i++){
            t[i]=i;
            s[i]=1;
        }
    }
    void Union(int x,int y){
        if (same(x, y)) return;
        int tx = Find(x), ty = Find(y);
        if (s[tx] < s[ty]) {
            s[ty] += s[tx];
            t[tx] = ty;
        }
        else if (s[tx] > s[ty]) {
            s[tx] += s[ty];
            t[ty] = tx;
        }
        else if (tx > ty) {
            t[tx] = ty;
            s[ty] += s[tx];
        }
        else {
            t[ty] = tx;
            s[tx] += s[ty];
        }
    }
    int Find(int n) {
        if (t[n] == n)
            return n;
        else
            return t[n] = Find(t[n]);
    }
    boolean same(int x, int y) {
        return Find(x) == Find(y);
    }
    int get_Size(int a) {
        return s[a];
    }
    int get_tree(int size){
        HashSet<Integer> st=new HashSet<>();
        for(int i=0;i<size;i++){
            st.add(t[i+1]);
        }
        return st.size();
    }
}


Compiler output:
a.cc:1:1: error: 'import' does not name a type
    1 | import java.io.BufferedReader;
      | ^~~~~~
a.cc:1:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:2:1: error: 'import' does not name a type
    2 | import java.io.IOException;
      | ^~~~~~
a.cc:2:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:3:1: error: 'import' does not name a type
    3 | import java.io.InputStreamReader;
      | ^~~~~~
a.cc:3:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:4:1: error: 'import' does not name a type
    4 | import java.util.HashSet;
      | ^~~~~~
a.cc:4:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:5:1: error: 'import' does not name a type
    5 | import java.util.StringTokenizer;
      | ^~~~~~
a.cc:5:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:7:1: error: expected unqualified-id before 'public'
    7 | public class Main {
      | ^~~~~~
a.cc:25:12: error: expected ':' before 'int'
   25 |     private int[] t,s;
      |            ^~~~
      |            :
a.cc:25:16: error: expected unqualified-id before '[' token
   25 |     private int[] t,s;
      |                ^
a.cc:26:11: error: expected ':' before 'Union_Find'
   26 |     public Union_Find(int max_length){
      |           ^~~~~~~~~~~
      |           :
a.cc:60:5: error: 'boolean' does not name a type; did you mean 'bool'?
   60 |     boolean same(int x, int y) {
      |     ^~~~~~~
      |     bool
a.cc:73:2: error: expected ';' after class definition
   73 | }
      |  ^
      |  ;
a.cc: In constructor 'Union_Find::Union_Find(int)':
a.cc:27:9: error: 't' was not declared in this scope
   27 |         t=new int[max_length+1];
      |         ^
a.cc:28:9: error: 's' was not declared in this scope
   28 |         s=new int[max_length+1];
      |         ^
a.cc: In member function 'void Union_Find::Union(int, int)':
a.cc:35:13: error: 'same' was not declared in this scope
   35 |         if (same(x, y)) return;
      |             ^~~~
a.cc:37:13: error: 's' was not declared in this scope
   37 |         if (s[tx] < s[ty]) {
      |             ^
a.cc:39:13: error: 't' was not declared in this scope; did you mean 'ty'?
   39 |             t[tx] = ty;
      |             ^
      |             ty
a.cc:43:13: error: 't' was not declared in this scope; did you mean 'ty'?
   43 |             t[ty] = tx;
      |             ^
      |             ty
a.cc:46:13: error: 't' was not declared in this scope; did you mean 'ty'?
   46 |             t[tx] = ty;
      |             ^
      |             ty
a.cc:50:13: error: 't' was not declared in this scope; did you mean 'ty'?
   50 |             t[ty] = tx;
      |             ^
      |             ty
a.cc: In member function 'int Union_Find::Find(int)':
a.cc:55:13: error: 't' was not declared in this scope
   55 |         if (t[n] == n)
      |             ^
a.cc: In member function 'int Union_Find::get_Size(int)':
a.cc:64:16: error: 's' was not declared in this scope
   64 |         return s[a];
      |                ^
a.cc: In member function 'int Union_Find::get_tree(int)':
a.cc:67:9: error: 'HashSet' was not declared in this scope
   67 |         HashSet<Integer> st=new HashSet<>();
      |         ^~~~~~~
a.cc:67:17: error: 'Integer' was not declared in this scope
   67 |         HashSet<Integer> st=new HashSet<>();
      |                 ^~~~~~~
a.cc:67:26: error: 'st' was not declared in this scope; did you mean 'std'?
   67 |         HashSet<Integer> st=new HashSet<>();
      |                          ^~
      |                          std
a.cc:67:33: error: 'HashSet' does not name a type
   67 |         HashSet<Integer> st=new HashSet<>();
      |                                 ^~~~~~~
a.cc:67:41: error: expected primary-expression before '>' token
   67 |         HashSet<Integer> st=new HashSet<>();
      |                                         ^
a.cc:67:43: error: expected primary-expression before ')' token
   67 |         HashSet<Integer> st=new HashSet<>();
      |                                           ^
a.cc:69:20: error: 't' was not declared in this scope
   69 |             st.add(t[i+1]);
      |                    ^