Language: Java

import java.util.Scanner;
 
public class Main {
    int n;
    int q;
    int[] p;
    int[] rank;
 
    public static boolean same(int x, int y) {
        return findSet(x) == findSet(y);
    }
 
    public static void unite(int x, int y) {
        int i = findSet(x);
        int j = findSet(y);
 
        if (i == j){
	    return;
	}
        if (rank[i] > rank[j]){
            p[j] = p[i];
        }
        else {
            p[i] = p[j];
            if(rank[i] == rank[j]){
            rank[j]++;
            }
        }	
    }
 
    public static int findSet(int i) {
       if(p[i] == i){ 
           return i;
        }else{
          return p[i] = findSet(p[i]);
        }
    }
 
    public static void DisjointSet() {
        p = new int[n];
        rank = new int[n];
 
        for (int i = 0; i < n; i++) {
            p[i] = i;
            rank[i] = 0;
        }
    }
 
 
    public static void main (String[] args){
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        q = scanner.nextInt();
 
        DisjointSet();
        for (int i = 0; i < q; i++) {
            int com = scanner.nextInt();
            int x = scanner.nextInt();
            int y = scanner.nextInt();
 
            if (com == 0){
                unite(x, y);
            }
            if (com == 1){
                if (same(x, y)){
                    System.out.println(1);
                }else{
                    System.out.println(0);
		}
            }
        }
    }
}

Compiler output:
Main.java:20: error: non-static variable rank cannot be referenced from a static context
        if (rank[i] > rank[j]){
            ^
Main.java:20: error: non-static variable rank cannot be referenced from a static context
        if (rank[i] > rank[j]){
                      ^
Main.java:21: error: non-static variable p cannot be referenced from a static context
            p[j] = p[i];
            ^
Main.java:21: error: non-static variable p cannot be referenced from a static context
            p[j] = p[i];
                   ^
Main.java:24: error: non-static variable p cannot be referenced from a static context
            p[i] = p[j];
            ^
Main.java:24: error: non-static variable p cannot be referenced from a static context
            p[i] = p[j];
                   ^
Main.java:25: error: non-static variable rank cannot be referenced from a static context
            if(rank[i] == rank[j]){
               ^
Main.java:25: error: non-static variable rank cannot be referenced from a static context
            if(rank[i] == rank[j]){
                          ^
Main.java:26: error: non-static variable rank cannot be referenced from a static context
            rank[j]++;
            ^
Main.java:32: error: non-static variable p cannot be referenced from a static context
       if(p[i] == i){ 
          ^
Main.java:35: error: non-static variable p cannot be referenced from a static context
          return p[i] = findSet(p[i]);
                 ^
Main.java:35: error: non-static variable p cannot be referenced from a static context
          return p[i] = findSet(p[i]);
                                ^
Main.java:40: error: non-static variable p cannot be referenced from a static context
        p = new int[n];
        ^
Main.java:40: error: non-static variable n cannot be referenced from a static context
        p = new int[n];
                    ^
Main.java:41: error: non-static variable rank cannot be referenced from a static context
        rank = new int[n];
        ^
Main.java:41: error: non-static variable n cannot be referenced from a static context
        rank = new int[n];
                       ^
Main.java:43: error: non-static variable n cannot be referenced from a static context
        for (int i = 0; i < n; i++) {
                            ^
Main.java:44: error: non-static variable p cannot be referenced from a static context
            p[i] = i;
            ^
Main.java:45: error: non-static variable rank cannot be referenced from a static context
            rank[i] = 0;
            ^
Main.java:52: error: non-static variable n cannot be referenced from a static context
        n = scanner.nextInt();
        ^
Main.java:53: error: non-static variable q cannot be referenced from a static context
        q = scanner.nextInt();
        ^
Main.java:56: error: non-static variable q cannot be referenced from a static context
        for (int i = 0; i < q; i++) {
                            ^
22 errors