Exception in thread in this JAVA Code: import java.util.*; import java.io.*; public class Main { public String problemName = ""; class Pair { int first; int second; public Pair(int first, int second) { this.first = first;
Help me fix Exception in thread in this JAVA Code:
import java.util.*;
import java.io.*;
public class Main {
public String problemName = "";
class Pair {
int first;
int second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
}
public void run() throws Exception {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
int m = in.nextInt();
Pair connections[] = new Pair[m];
for (int i = 0; i < m; i++) {
connections[i] = new Pair(in.nextInt() - 1, in.nextInt() - 1);
}
int[] dp = new int[1<<n];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;
for (int bits = 0; bits < n; bits++) {
for (int mask = 0; mask < (1 << n); mask++) if (Integer.bitCount(mask) == bits) {
int add = 0;
for (Pair connection : connections) {
boolean first = (mask & (1<<connection.first)) > 0;
boolean second = (mask & (1<<connection.second)) > 0;
if (first != second) {
add++;
}
}
for (int i = 0; i < n; i++) if ((mask & (1<<i)) == 0) {
dp[mask | (1<<i)] = Math.min(dp[mask | (1<<i)], dp[mask] + add);
}
}
}
out.println(dp[(1<<n)*2 + 1]*3 - 1);
out.close();
in.close();
}
public static void main(String[] args) throws Exception {
new Main().run();
}
}
I am testing on this test case:
Step by step
Solved in 2 steps with 1 images