|
| 1 | +```java |
| 2 | +import java.sql.Array; |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +public class Main { |
| 7 | + |
| 8 | + static BufferedReader br; |
| 9 | + static BufferedWriter bw; |
| 10 | + static StringTokenizer st; |
| 11 | + |
| 12 | + static int N, M; |
| 13 | + static long[] toggle; |
| 14 | + static List<long[]> results; |
| 15 | + static List<long[]> leftResults; |
| 16 | + static Map<Long, Integer> rightResults; |
| 17 | + |
| 18 | + public static void main(String[] args) throws Exception { |
| 19 | + |
| 20 | + input(); |
| 21 | + solve(); |
| 22 | + |
| 23 | + } |
| 24 | + |
| 25 | + public static void input() throws Exception { |
| 26 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 27 | + |
| 28 | + st = new StringTokenizer(br.readLine()); |
| 29 | + N = Integer.parseInt(st.nextToken()); |
| 30 | + M = Integer.parseInt(st.nextToken()); |
| 31 | + toggle = new long[N]; |
| 32 | + for(int i=0;i<N;i++) toggle[i] = (1L<<i); |
| 33 | + for(int i=0;i<M;i++) { |
| 34 | + st = new StringTokenizer(br.readLine()); |
| 35 | + int a = Integer.parseInt(st.nextToken()) - 1; |
| 36 | + int b = Integer.parseInt(st.nextToken()) - 1; |
| 37 | + toggle[a] |= (1L<<b); |
| 38 | + toggle[b] |= (1L<<a); |
| 39 | + } |
| 40 | + |
| 41 | + br.close(); |
| 42 | + } |
| 43 | + |
| 44 | + public static void solve() throws Exception { |
| 45 | + bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 46 | + |
| 47 | + results = new ArrayList<>(); |
| 48 | + bck(0, N/2, 0, 0); |
| 49 | + leftResults = new ArrayList<>(); |
| 50 | + leftResults.addAll(results); |
| 51 | + |
| 52 | + results = new ArrayList<>(); |
| 53 | + bck(N/2, N, 0, 0); |
| 54 | + rightResults = new TreeMap<>(); |
| 55 | + for(long[] info : results) { |
| 56 | + long status = info[0]; |
| 57 | + int cnt = (int)info[1]; |
| 58 | + if(!rightResults.containsKey(status) || rightResults.get(status) > cnt) rightResults.put(status, cnt); |
| 59 | + } |
| 60 | + |
| 61 | + long ans = N; |
| 62 | + for(long[] info : leftResults) { |
| 63 | + long leftStatus = info[0]; |
| 64 | + int cntLeft = (int)info[1]; |
| 65 | + long target = ((1L<<N)-1) ^ leftStatus; |
| 66 | + int cntRight = rightResults.getOrDefault(target, 12345); |
| 67 | + ans = Math.min(ans, cntLeft + cntRight); |
| 68 | + } |
| 69 | + |
| 70 | + bw.write(ans + "\n"); |
| 71 | + |
| 72 | + bw.close(); |
| 73 | + } |
| 74 | + |
| 75 | + public static void bck(int pos, int tar, int cnt, long status) { |
| 76 | + if(pos == tar) { |
| 77 | + results.add(new long[]{status,cnt}); |
| 78 | + return; |
| 79 | + } |
| 80 | + bck(pos+1, tar, cnt, status); |
| 81 | + bck(pos+1, tar, cnt+1, status ^ toggle[pos]); |
| 82 | + } |
| 83 | + |
| 84 | +} |
| 85 | +``` |
0 commit comments