結果

問題 No.2236 Lights Out On Simple Graph
ユーザー tenten
提出日時 2023-03-06 19:35:06
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,908 ms / 4,000 ms
コード長 3,268 bytes
コンパイル時間 2,925 ms
コンパイル使用メモリ 85,612 KB
実行使用メモリ 211,596 KB
最終ジャッジ日時 2024-09-18 01:55:38
合計ジャッジ時間 41,386 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        long[] front = new long[m / 2];
        long[] rear = new long[m - m / 2];
        for (int i = 0; i < m / 2; i++) {
            front[i] = (1L << (sc.nextInt() - 1)) + (1L << (sc.nextInt() - 1));
        }
        for (int i = 0; i < m - m / 2; i++) {
            rear[i] = (1L << (sc.nextInt() - 1)) + (1L << (sc.nextInt() - 1));
        }
        long base = 0;
        for (int i = 0; i < n; i++) {
            if (sc.nextInt() == 1) {
                base += (1L << i);
            }
        }
        long[] dp1 = new long[1 <<(m / 2)];
        HashMap<Long, Integer> counts1 = new HashMap<>();
        counts1.put(base, 0);
        dp1[0] = base;
        for (int i = 1; i < (1 << (m / 2)); i++) {
            for (int j = 0; j < m / 2; j++) {
                if ((i & (1 << j)) == 0) {
                    continue;
                }
                dp1[i] = dp1[i ^ (1 << j)] ^ front[j];
                counts1.put(dp1[i], Math.min(counts1.getOrDefault(dp1[i], m), getPop(i)));
                break;
            }
        }
        int ans = Integer.MAX_VALUE;
        long[] dp2 = new long[1 <<(m - m / 2)];
        for (int i = 0; i < (1 << (m - m / 2)); i++) {
            for (int j = 0; j < m - m / 2; j++) {
                if ((i & (1 << j)) == 0) {
                    continue;
                }
                dp2[i] = dp2[i ^ (1 << j)] ^ rear[j];
                break;
            }
            ans = Math.min(ans, counts1.getOrDefault(dp2[i], m + 1) + getPop(i));
        }
        if (ans > m) {
            System.out.println(-1);
        } else {
            System.out.println(ans);
        }
    }
    
    static int getPop(int x) {
        int pop = 0;
        while (x > 0) {
            pop += x % 2;
            x >>= 1;
        }
        return pop;
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0