結果

問題 No.2236 Lights Out On Simple Graph
ユーザー tenten
提出日時 2023-07-06 15:09:50
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 3,581 bytes
コンパイル時間 4,409 ms
コンパイル使用メモリ 85,428 KB
実行使用メモリ 362,948 KB
最終ジャッジ日時 2024-07-20 10:25:27
合計ジャッジ時間 15,742 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41 WA * 16
権限があれば一括ダウンロードができます

ソースコード

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[] evValue = new long[(m + 1) / 2];
        long[] odValue = new long[m / 2];
        for (int i = 0; i < m; i++) {
            if (i % 2 == 0) {
                evValue[i / 2] = (1L << (sc.nextInt() - 1)) + (1L << (sc.nextInt() - 1));
            } else {
                odValue[i / 2] = (1L << (sc.nextInt() - 1)) + (1L << (sc.nextInt() - 1));
            }
        }
        long[] evDp = new long[1 << evValue.length];
        HashMap<Long, Integer> evCount = new HashMap<>();
        for (int i = 0; i < (1 << evValue.length); i++) {
            int pop = getPop(i);
            for (int j = 0; j < evValue.length; j++) {
                if ((i & (1 << j)) == 0) {
                    continue;
                }
                evDp[i] = evDp[i ^ (1 << j)] ^ evValue[j];
                break;
            }
            evCount.put(evDp[i], Math.min(evCount.getOrDefault(evDp[i], Integer.MAX_VALUE), pop));
        }
        long[] odDp = new long[1 << odValue.length];
        HashMap<Long, Integer> odCount = new HashMap<>();
        for (int i = 0; i < (1 << odValue.length); i++) {
            int pop = getPop(i);
            for (int j = 0; j < odValue.length; j++) {
                if ((i & (1 << j)) == 0) {
                    continue;
                }
                odDp[i] = odDp[i ^ (1 << j)] ^ odValue[j];
                break;
            }
            odCount.put(odDp[i], Math.min(odCount.getOrDefault(odDp[i], Integer.MAX_VALUE), pop));
        }
        long base = 0;
        for (int i = 0; i < n; i++) {
            base += (sc.nextInt() << i);
        }
        int ans = Integer.MAX_VALUE / 2;
        for (long x : evCount.keySet()) {
            ans = Math.min(ans, evCount.get(x) + odCount.getOrDefault(x ^ base, Integer.MAX_VALUE / 2));
        }
        if (ans >= Integer.MAX_VALUE / 2) {
            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