結果

問題 No.2236 Lights Out On Simple Graph
ユーザー tentententen
提出日時 2023-03-06 19:35:06
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,289 ms / 4,000 ms
コード長 3,268 bytes
コンパイル時間 2,957 ms
コンパイル使用メモリ 91,260 KB
実行使用メモリ 215,932 KB
最終ジャッジ日時 2023-10-18 05:15:50
合計ジャッジ時間 51,747 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
53,640 KB
testcase_01 AC 58 ms
52,532 KB
testcase_02 AC 57 ms
52,516 KB
testcase_03 AC 1,844 ms
205,536 KB
testcase_04 AC 507 ms
120,308 KB
testcase_05 AC 55 ms
53,628 KB
testcase_06 AC 732 ms
173,740 KB
testcase_07 AC 1,125 ms
120,456 KB
testcase_08 AC 57 ms
53,620 KB
testcase_09 AC 56 ms
53,628 KB
testcase_10 AC 557 ms
90,764 KB
testcase_11 AC 153 ms
60,436 KB
testcase_12 AC 637 ms
101,696 KB
testcase_13 AC 155 ms
60,476 KB
testcase_14 AC 949 ms
133,588 KB
testcase_15 AC 118 ms
57,748 KB
testcase_16 AC 164 ms
61,564 KB
testcase_17 AC 267 ms
68,532 KB
testcase_18 AC 69 ms
54,240 KB
testcase_19 AC 247 ms
66,056 KB
testcase_20 AC 984 ms
135,636 KB
testcase_21 AC 247 ms
66,032 KB
testcase_22 AC 2,040 ms
208,560 KB
testcase_23 AC 1,553 ms
194,892 KB
testcase_24 AC 1,014 ms
141,632 KB
testcase_25 AC 368 ms
77,276 KB
testcase_26 AC 1,439 ms
205,444 KB
testcase_27 AC 1,128 ms
128,440 KB
testcase_28 AC 1,215 ms
147,996 KB
testcase_29 AC 199 ms
63,688 KB
testcase_30 AC 1,743 ms
208,308 KB
testcase_31 AC 1,536 ms
195,412 KB
testcase_32 AC 1,193 ms
172,220 KB
testcase_33 AC 1,782 ms
205,544 KB
testcase_34 AC 1,774 ms
215,932 KB
testcase_35 AC 1,741 ms
197,628 KB
testcase_36 AC 1,068 ms
168,520 KB
testcase_37 AC 1,528 ms
179,920 KB
testcase_38 AC 816 ms
105,984 KB
testcase_39 AC 311 ms
70,908 KB
testcase_40 AC 380 ms
93,704 KB
testcase_41 AC 179 ms
63,244 KB
testcase_42 AC 397 ms
81,948 KB
testcase_43 AC 124 ms
58,328 KB
testcase_44 AC 947 ms
136,552 KB
testcase_45 AC 1,374 ms
147,784 KB
testcase_46 AC 1,554 ms
160,980 KB
testcase_47 AC 931 ms
146,404 KB
testcase_48 AC 2,289 ms
214,252 KB
testcase_49 AC 1,175 ms
120,696 KB
testcase_50 AC 108 ms
58,008 KB
testcase_51 AC 483 ms
71,456 KB
testcase_52 AC 131 ms
59,060 KB
testcase_53 AC 55 ms
53,632 KB
testcase_54 AC 1,013 ms
113,584 KB
testcase_55 AC 596 ms
81,388 KB
testcase_56 AC 373 ms
75,948 KB
testcase_57 AC 543 ms
80,104 KB
testcase_58 AC 406 ms
76,336 KB
testcase_59 AC 383 ms
76,616 KB
権限があれば一括ダウンロードができます

ソースコード

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