結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
37,328 KB
testcase_01 AC 46 ms
37,024 KB
testcase_02 AC 44 ms
37,052 KB
testcase_03 AC 2,105 ms
248,484 KB
testcase_04 WA -
testcase_05 AC 49 ms
37,148 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 46 ms
37,188 KB
testcase_09 AC 45 ms
37,276 KB
testcase_10 AC 550 ms
76,352 KB
testcase_11 AC 166 ms
47,392 KB
testcase_12 AC 879 ms
94,308 KB
testcase_13 AC 133 ms
47,328 KB
testcase_14 AC 1,847 ms
161,120 KB
testcase_15 AC 259 ms
47,500 KB
testcase_16 AC 360 ms
51,204 KB
testcase_17 AC 461 ms
56,888 KB
testcase_18 AC 126 ms
39,292 KB
testcase_19 AC 396 ms
66,360 KB
testcase_20 WA -
testcase_21 AC 335 ms
67,428 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 2,343 ms
261,244 KB
testcase_25 WA -
testcase_26 AC 2,707 ms
311,128 KB
testcase_27 AC 1,143 ms
122,556 KB
testcase_28 AC 1,991 ms
259,188 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 2,069 ms
209,628 KB
testcase_36 AC 1,143 ms
186,720 KB
testcase_37 AC 1,917 ms
288,204 KB
testcase_38 AC 503 ms
63,276 KB
testcase_39 AC 352 ms
58,812 KB
testcase_40 AC 1,218 ms
108,952 KB
testcase_41 AC 316 ms
64,112 KB
testcase_42 AC 565 ms
75,344 KB
testcase_43 AC 151 ms
46,896 KB
testcase_44 AC 1,427 ms
179,144 KB
testcase_45 WA -
testcase_46 AC 3,113 ms
267,180 KB
testcase_47 WA -
testcase_48 AC 2,478 ms
256,764 KB
testcase_49 WA -
testcase_50 AC 105 ms
43,380 KB
testcase_51 AC 700 ms
114,360 KB
testcase_52 AC 129 ms
44,516 KB
testcase_53 AC 47 ms
37,448 KB
testcase_54 AC 2,074 ms
261,964 KB
testcase_55 AC 541 ms
86,304 KB
testcase_56 AC 435 ms
70,832 KB
testcase_57 AC 742 ms
97,624 KB
testcase_58 AC 554 ms
70,412 KB
testcase_59 AC 620 ms
82,152 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[] 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