Language: Java

import java.util.Scanner;
 
public class Main {
    public static int N;
    public static int M;
    public static int[] p;
    public static int[] rank;
    public static int[] w;
 
    public static boolean same(int x, int y) {
        return findSet(x) == findSet(y);
    }
 
    public static void unite(int a, int b, int w) {
        int i = findSet(a);
        int j = findSet(b);
	w -= w[b] - w[a];
	
        if (i == j){
	    return;
	}
        if (rank[i] > rank[j]){
            p[j] = p[i];
	    w[j] = w;
        } else {
            p[i] = p[j];
	    w[x] = -w;
            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];
	w = new int[N];
 
        for (int i = 0; i < N; i++) {
            p[i] = i;
            rank[i] = 0;
	    w[i] = 0;
        }
    }
 
 
    public static void main (String[] args){
        Scanner scanner = new Scanner(System.in);
	while(true){   
        N = scanner.nextInt();
        M = scanner.nextInt();
	if(N==0 & M == 0) break;

        DisjointSet();
	Solve();
	}
    }
    
    public static void Solve(){
        for (int i = 0; i < M; i++) {
	    if(scanner.next().equals("!")){
		int a = scanner.nextInt();
		int b = scanner.nextInt();
		int w = scanner.nextInt();
		
		a--;
		b--;
		unite(a, b, w);
	    }else{
	        if(!same(a,b)){
		    System.out.println("UNKNOWN");
		}else{
		    System.out.println(w[b]-w[a]);
		}
	    }
	}
    }
}

Compiler output:
Main.java:17: error: array required, but int found
	w -= w[b] - w[a];
	      ^
Main.java:17: error: array required, but int found
	w -= w[b] - w[a];
	             ^
Main.java:24: error: array required, but int found
	    w[j] = w;
	     ^
Main.java:27: error: cannot find symbol
	    w[x] = -w;
	      ^
  symbol:   variable x
  location: class Main
Main.java:27: error: array required, but int found
	    w[x] = -w;
	     ^
Main.java:69: error: cannot find symbol
	    if(scanner.next().equals("!")){
	       ^
  symbol:   variable scanner
  location: class Main
Main.java:70: error: cannot find symbol
		int a = scanner.nextInt();
		        ^
  symbol:   variable scanner
  location: class Main
Main.java:71: error: cannot find symbol
		int b = scanner.nextInt();
		        ^
  symbol:   variable scanner
  location: class Main
Main.java:72: error: cannot find symbol
		int w = scanner.nextInt();
		        ^
  symbol:   variable scanner
  location: class Main
Main.java:78: error: cannot find symbol
	        if(!same(a,b)){
	                 ^
  symbol:   variable a
  location: class Main
Main.java:78: error: cannot find symbol
	        if(!same(a,b)){
	                   ^
  symbol:   variable b
  location: class Main
Main.java:81: error: cannot find symbol
		    System.out.println(w[b]-w[a]);
		                         ^
  symbol:   variable b
  location: class Main
Main.java:81: error: cannot find symbol
		    System.out.println(w[b]-w[a]);
		                              ^
  symbol:   variable a
  location: class Main
13 errors