結果

問題 No.2236 Lights Out On Simple Graph
ユーザー tentententen
提出日時 2023-03-06 19:35:06
言語 Java21
(openjdk 21)
結果
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
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
50,048 KB
testcase_01 AC 49 ms
50,052 KB
testcase_02 AC 50 ms
50,264 KB
testcase_03 AC 1,521 ms
202,224 KB
testcase_04 AC 414 ms
117,460 KB
testcase_05 AC 52 ms
50,448 KB
testcase_06 AC 574 ms
165,592 KB
testcase_07 AC 868 ms
117,548 KB
testcase_08 AC 49 ms
50,440 KB
testcase_09 AC 48 ms
50,464 KB
testcase_10 AC 525 ms
87,512 KB
testcase_11 AC 122 ms
57,672 KB
testcase_12 AC 490 ms
98,920 KB
testcase_13 AC 138 ms
58,056 KB
testcase_14 AC 740 ms
127,048 KB
testcase_15 AC 101 ms
54,716 KB
testcase_16 AC 143 ms
57,904 KB
testcase_17 AC 203 ms
65,284 KB
testcase_18 AC 57 ms
50,240 KB
testcase_19 AC 188 ms
62,212 KB
testcase_20 AC 696 ms
133,468 KB
testcase_21 AC 196 ms
63,248 KB
testcase_22 AC 1,908 ms
207,664 KB
testcase_23 AC 1,330 ms
190,608 KB
testcase_24 AC 860 ms
138,700 KB
testcase_25 AC 274 ms
75,248 KB
testcase_26 AC 1,192 ms
201,936 KB
testcase_27 AC 918 ms
125,476 KB
testcase_28 AC 966 ms
141,964 KB
testcase_29 AC 171 ms
61,068 KB
testcase_30 AC 1,405 ms
205,560 KB
testcase_31 AC 1,506 ms
191,132 KB
testcase_32 AC 1,103 ms
169,776 KB
testcase_33 AC 1,492 ms
202,216 KB
testcase_34 AC 1,805 ms
211,596 KB
testcase_35 AC 1,331 ms
196,388 KB
testcase_36 AC 900 ms
164,664 KB
testcase_37 AC 1,211 ms
176,600 KB
testcase_38 AC 590 ms
102,352 KB
testcase_39 AC 284 ms
67,936 KB
testcase_40 AC 363 ms
90,816 KB
testcase_41 AC 145 ms
60,016 KB
testcase_42 AC 338 ms
78,440 KB
testcase_43 AC 108 ms
55,196 KB
testcase_44 AC 699 ms
133,380 KB
testcase_45 AC 1,163 ms
150,216 KB
testcase_46 AC 1,243 ms
152,632 KB
testcase_47 AC 776 ms
141,044 KB
testcase_48 AC 1,897 ms
194,412 KB
testcase_49 AC 846 ms
117,356 KB
testcase_50 AC 99 ms
54,928 KB
testcase_51 AC 421 ms
67,728 KB
testcase_52 AC 113 ms
55,808 KB
testcase_53 AC 53 ms
50,432 KB
testcase_54 AC 797 ms
110,348 KB
testcase_55 AC 487 ms
78,264 KB
testcase_56 AC 322 ms
73,028 KB
testcase_57 AC 415 ms
77,124 KB
testcase_58 AC 351 ms
73,288 KB
testcase_59 AC 335 ms
73,812 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