import java.util.*; import java.io.*; /** * The complexity of this solution is something like O(n^n), * although it could easily be improved to O((n-1)^n). * * Try all possible condorcet scenarios and calculate the minimum * number of additional votes needed in each case. * In cases where all candidates are within 2 votes of reaching * condorcet (and no-one is ahead by more than 2), do a brute force * search of the states to get the minimum needed. Otherwise, * we will need just enough votes to get the person furthest from * beating their "target" in the scenario to pull ahead. * * @author Finn Lidbetter */ public class condorcet_finn { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] s = br.readLine().split(" "); int n = Integer.parseInt(s[0]); int m = Integer.parseInt(s[1]); // vs[i][j] is the number of votes for which i is preferred to j. // vs[i][j] = -vs[j][i] long[][] vs = new long[n][n]; for (int i=0; i0) { best = 0; System.out.println(0); return; } long req = 0; boolean skip = false; if (matchups[n-1]<=2 && matchups[0]>=-2) { // Brute force cases where the number of votes needed is small. req = search(matchups); } else { int diffSum = 0; for (int i=1; i q = new LinkedList<>(); HashSet vis = new HashSet<>(); q.offer(new State(initial)); vis.add(new State(initial)); q.offer(null); int depth = 0; int n = initial.length; while (q.size()>1) { State curr = q.poll(); if (curr==null) { q.offer(null); depth++; continue; } boolean good = true; for (int i=0; i