Language: Java

import java.util.Scanner;
import java.util.Arrays;

public class Main { //HDL-style Disjoint Set
	int N; // int_maxsize > 10000 (constrain)
	int Q; // int_maxsize > 110000 (worstcase = 10000initial set  + 100000 new set)
	boolean Com; //com only have two kind;
	int V1;
	int V2;
	int V3;
	int[] S1;
	int[] S2;
	Scanner scanner;
	public static void main(String[] args){
		scanner = new Scanner(System.in);
		N = scanner.nextInt();
		S1 = new int[N];
		S2 = new int[N];
		reset();
		Q = scanner.nextInt();
		FSM();
	}
	//end of main
	private static void reset(){
		Com = false;
		V1 = 0;
		V2 = 0;
		V3 = 0;
		for(int i = 0; i<N; i++){
			S1[i]=i;
		}
	}
	//end of reset
	private static void FSM(){
		while(Q>0){
			fetch();
			dispatch();
			Q--;
		}
	}
	//end of FSM
	private static void fetch(){
		Com = (scanner.nextInt() > 0)?true:false;
		//V1 = scanner.nextInt();
		//V2 = scanner.nextInt();
	}
	//end of feteh
	private static void dispatch(){
		if(Com){
			V1 = scanner.nextInt();
			V2 = scanner.nextInt();
			query();
		}else {
			V1 = scanner.nextInt();
			V2 = scanner.nextInt();
			V3 = scanner.nextInt();
			relate();
		}
	}
	//end of dispatch
	private static void relate(){
		if(S1[V1] == S1[V2]){
			return;
		}else{
			merge();
		}
	}
	//end of unite
	private static void query(){
		if(S1[V1] == S1[V2]){
			System.out.println(S2[V2] - S2[V1]);
		}else{
			System.out.println("?");
		}
	}
	//end of same
	private static void merge(){
		V3 = S2[V2] - V3 - S2[V1];
		V1 = S1[V1];// use variable 1 to store the set code of V1
		V2 = S1[V2];// use variable 2 to store the set code of V2
		for(int i = 0; i< N;i++)
		{
			if(S1[i] == V1){
				//System.out.println("V1 = "+ V1 + "V2 = " + V2+ "V3 = " + V3);
				S1[i] = V2;
				S2[i] = S2[i] + V3;
				//System.out.println("S2[i] = "+ S2[i]);
			}else{
				continue;
			}
		}
	}
}	




Compiler output:
Main.java:15: error: non-static variable scanner cannot be referenced from a static context
		scanner = new Scanner(System.in);
		^
Main.java:16: error: non-static variable N cannot be referenced from a static context
		N = scanner.nextInt();
		^
Main.java:16: error: non-static variable scanner cannot be referenced from a static context
		N = scanner.nextInt();
		    ^
Main.java:17: error: non-static variable S1 cannot be referenced from a static context
		S1 = new int[N];
		^
Main.java:17: error: non-static variable N cannot be referenced from a static context
		S1 = new int[N];
		             ^
Main.java:18: error: non-static variable S2 cannot be referenced from a static context
		S2 = new int[N];
		^
Main.java:18: error: non-static variable N cannot be referenced from a static context
		S2 = new int[N];
		             ^
Main.java:20: error: non-static variable Q cannot be referenced from a static context
		Q = scanner.nextInt();
		^
Main.java:20: error: non-static variable scanner cannot be referenced from a static context
		Q = scanner.nextInt();
		    ^
Main.java:25: error: non-static variable Com cannot be referenced from a static context
		Com = false;
		^
Main.java:26: error: non-static variable V1 cannot be referenced from a static context
		V1 = 0;
		^
Main.java:27: error: non-static variable V2 cannot be referenced from a static context
		V2 = 0;
		^
Main.java:28: error: non-static variable V3 cannot be referenced from a static context
		V3 = 0;
		^
Main.java:29: error: non-static variable N cannot be referenced from a static context
		for(int i = 0; i<N; i++){
		                 ^
Main.java:30: error: non-static variable S1 cannot be referenced from a static context
			S1[i]=i;
			^
Main.java:35: error: non-static variable Q cannot be referenced from a static context
		while(Q>0){
		      ^
Main.java:38: error: non-static variable Q cannot be referenced from a static context
			Q--;
			^
Main.java:43: error: non-static variable Com cannot be referenced from a static context
		Com = (scanner.nextInt() > 0)?true:false;
		^
Main.java:43: error: non-static variable scanner cannot be referenced from a static context
		Com = (scanner.nextInt() > 0)?true:false;
		       ^
Main.java:49: error: non-static variable Com cannot be referenced from a static context
		if(Com){
		   ^
Main.java:50: error: non-static variable V1 cannot be referenced from a static context
			V1 = scanner.nextInt();
			^
Main.java:50: error: non-static variable scanner cannot be referenced from a static context
			V1 = scanner.nextInt();
			     ^
Main.java:51: error: non-static variable V2 cannot be referenced from a static context
			V2 = scanner.nextInt();
			^
Main.java:51: error: non-static variable scanner cannot be referenced from a static context
			V2 = scanner.nextInt();
			     ^
Main.java:54: error: non-static variable V1 cannot be referenced from a static context
			V1 = scanner.nextInt();
			^
Main.java:54: error: non-static variable scanner cannot be referenced from a static context
			V1 = scanner.nextInt();
			     ^
Main.java:55: error: non-static variable V2 cannot be referenced from a static context
			V2 = scanner.nextInt();
			^
Main.java:55: error: non-static variable scanner cannot be referenced from a static context
			V2 = scanner.nextInt();
			     ^
Main.java:56: error: non-static variable V3 cannot be referenced from a static context
			V3 = scanner.nextInt();
			^
Main.java:56: error: non-static variable scanner cannot be referenced from a static context
			V3 = scanner.nextInt();
			     ^
Main.java:62: error: non-static variable S1 cannot be referenced from a static context
		if(S1[V1] == S1[V2]){
		   ^
Main.java:62: error: non-static variable V1 cannot be referenced from a static context
		if(S1[V1] == S1[V2]){
		      ^
Main.java:62: error: non-static variable S1 cannot be referenced from a static context
		if(S1[V1] == S1[V2]){
		             ^
Main.java:62: error: non-static variable V2 cannot be referenced from a static context
		if(S1[V1] == S1[V2]){
		                ^
Main.java:70: error: non-static variable S1 cannot be referenced from a static context
		if(S1[V1] == S1[V2]){
		   ^
Main.java:70: error: non-static variable V1 cannot be referenced from a static context
		if(S1[V1] == S1[V2]){
		      ^
Main.java:70: error: non-static variable S1 cannot be referenced from a static context
		if(S1[V1] == S1[V2]){
		             ^
Main.java:70: error: non-static variable V2 cannot be referenced from a static context
		if(S1[V1] == S1[V2]){
		                ^
Main.java:71: error: non-static variable S2 cannot be referenced from a static context
			System.out.println(S2[V2] - S2[V1]);
			                   ^
Main.java:71: error: non-static variable V2 cannot be referenced from a static context
			System.out.println(S2[V2] - S2[V1]);
			                      ^
Main.java:71: error: non-static variable S2 cannot be referenced from a static context
			System.out.println(S2[V2] - S2[V1]);
			                            ^
Main.java:71: error: non-static variable V1 cannot be referenced from a static context
			System.out.println(S2[V2] - S2[V1]);
			                               ^
Main.java:78: error: non-static variable V3 cannot be referenced from a static context
		V3 = S2[V2] - V3 - S2[V1];
		^
Main.java:78: error: non-static variable S2 cannot be referenced from a static context
		V3 = S2[V2] - V3 - S2[V1];
		     ^
Main.java:78: error: non-static variable V2 cannot be referenced from a static context
		V3 = S2[V2] - V3 - S2[V1];
		        ^
Main.java:78: error: non-static variable V3 cannot be referenced from a static context
		V3 = S2[V2] - V3 - S2[V1];
		              ^
Main.java:78: error: non-static variable S2 cannot be referenced from a static context
		V3 = S2[V2] - V3 - S2[V1];
		                   ^
Main.java:78: error: non-static variable V1 cannot be referenced from a static context
		V3 = S2[V2] - V3 - S2[V1];
		                      ^
Main.java:79: error: non-static variable V1 cannot be referenced from a static context
		V1 = S1[V1];// use variable 1 to store the set code of V1
		^
Main.java:79: error: non-static variable S1 cannot be referenced from a static context
		V1 = S1[V1];// use variable 1 to store the set code of V1
		     ^
Main.java:79: error: non-static variable V1 cannot be referenced from a static context
		V1 = S1[V1];// use variable 1 to store the set code of V1
		        ^
Main.java:80: error: non-static variable V2 cannot be referenced from a static context
		V2 = S1[V2];// use variable 2 to store the set code of V2
		^
Main.java:80: error: non-static variable S1 cannot be referenced from a static context
		V2 = S1[V2];// use variable 2 to store the set code of V2
		     ^
Main.java:80: error: non-static variable V2 cannot be referenced from a static context
		V2 = S1[V2];// use variable 2 to store the set code of V2
		        ^
Main.java:81: error: non-static variable N cannot be referenced from a static context
		for(int i = 0; i< N;i++)
		                  ^
Main.java:83: error: non-static variable S1 cannot be referenced from a static context
			if(S1[i] == V1){
			   ^
Main.java:83: error: non-static variable V1 cannot be referenced from a static context
			if(S1[i] == V1){
			            ^
Main.java:85: error: non-static variable S1 cannot be referenced from a static context
				S1[i] = V2;
				^
Main.java:85: error: non-static variable V2 cannot be referenced from a static context
				S1[i] = V2;
				        ^
Main.java:86: error: non-static variable S2 cannot be referenced from a static context
				S2[i] = S2[i] + V3;
				^
Main.java:86: error: non-static variable S2 cannot be referenced from a static context
				S2[i] = S2[i] + V3;
				        ^
Main.java:86: error: non-static variable V3 cannot be referenced from a static context
				S2[i] = S2[i] + V3;
				                ^
62 errors