import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
try {
Scanner scanner = new Scanner(reader);
while (scanner.hasNextInt()) {
int m = scanner.nextInt();
int n = scanner.nextInt();
Distance[] distances = new Distance[n];
int sum = 0;
for (int i = 0; i < n; i++) {
int from = scanner.nextInt();
int to = scanner.nextInt();
int distance = scanner.nextInt();
distances[i] = new Distance(from, to, distance);
sum += distance;
}
WeightedQuickUnion quickUnion = new WeightedQuickUnion(m + 1);
Arrays.sort(distances);
int totalDistance = 0;
for (Distance distance : distances) {
if (quickUnion.find(distance.from, distance.to)) {
continue;
}
totalDistance += distance.distance;
quickUnion.union(distance.from, distance.to);
}
writer.write((sum-totalDistance) + "\n");
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
static class Distance implements Comparable<Distance> {
int from, to, distance;
Distance(int from, int to, int distance) {
this.from = from;
this.to = to;
this.distance = distance;
}
@Override
public int compareTo(Distance o) {
int distanceDiff = distance - o.distance;
if (distanceDiff != 0) {
return distanceDiff;
}
int fromDiff = from - o.from;
if (fromDiff != 0) {
return fromDiff;
}
return to - o.to;
}
}
static class QuickUnion extends FindUnion {
public QuickUnion(int n) {
super(n);
}
@Override
public void union(int p, int q) {
int i = getRoot(p);
int j = getRoot(q);
array[i] = j;
groups--;
}
@Override
public boolean find(int a, int b) {
return getRoot(a) == getRoot(b);
}
protected int getRoot(int a) {
if (a == array[a]) {
return a;
}
return getRoot(array[a]);
}
}
static abstract class FindUnion {
protected final int[] array;
protected int groups;
protected FindUnion(int n) {
this.array = new int[n];
for (int i = 1; i < array.length; i++) {
array[i] = i;
}
groups = n;
}
public abstract void union(int p, int q);
public abstract boolean find(int p, int q);
public int getGroups() {
return groups;
}
}
static class WeightedQuickUnion extends QuickUnion {
final int[] size;
public WeightedQuickUnion(int n) {
super(n);
size = new int[n];
for (int i = 0; i < size.length; i++) {
size[i] = 1;
}
}
@Override
public void union(int p, int q) {
int i = getRoot(p);
int j = getRoot(q);
if (size[i] < size[j]) {
array[i] = j;
size[j] += size[i];
} else {
array[j] = i;
size[i] += size[j];
}
groups--;
}
}
}
My code returns a compiler error. In my IDE though, it shows no errors, and no warnings except "Unchecked cast from Bag[] to Bag<Main.Edge>[]". The code runs fine on my machine. What are some reasons why the judge would find a different compiling error?
My code also contains different classes (I'm guessing that this is the reason for the error.)
Is
import java.util.*;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
while(n != 0 && m != 0) {
solve(n, m, sc);
n = sc.nextInt();
m = sc.nextInt();
}
}
static class Edge implements Comparable<Edge> {
int a, b, w;
public Edge(int a, int b, int w) {
this.a = a;
this.b = b;
this.w = w;
}
public int compareTo(Edge e) {
return this.w - e.w;
}
public String toString() {
return "[(" + a + ", " + b + ") : " + w + "]";
}
}
static void solve(int n, int m, Scanner sc) {
Edge[] edges = new Edge[m];
Edge[] tree = new Edge[n - 1];
int total = 0;
for (int i = 0; i < m; i++) {
edges[i] = new Edge(sc.nextInt(), sc.nextInt(), sc.nextInt());
total += edges[i].w;
}
parent = new int[n];
rank = new int[n];
int s = 0;
Arrays.sort(edges);
for (int i = 0; i < n; i++) { parent[i] = i; }
for (Edge e : edges) { if (join(e.a, e.b)) { tree[s++] = e; }
if (s >= n -1) break;}
int output = 0;
for (Edge e : tree) {
output += e.w;
}
System.out.println(total - output);
}
static int[] parent;
static int[] rank;
static boolean join(int x, int y) {
/*
int fx = find(x);
int fy = find(y);
if(rank[x] < rank[y])
parent[x] = fy;
else {
parent[y] = fx;
if (rank[x] != rank[y]) {
rank[x]++;
}
}
return fx != fy;
*/
int xrt = find(x);
int yrt = find(y);
if (rank[xrt] > rank[yrt]) {
parent[yrt] = xrt;
} else {
parent[xrt] = yrt;
if (xrt != yrt) {
rank[xrt]++;
}
}
return xrt != yrt;
}
static int find(int x) {
return parent[x] == x ? x : (parent[x] = find(parent[x]));
}
}