結果

問題 No.2236 Lights Out On Simple Graph
ユーザー tentententen
提出日時 2023-04-13 18:12:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 3,655 ms / 4,000 ms
コード長 3,691 bytes
コンパイル時間 2,513 ms
コンパイル使用メモリ 95,820 KB
実行使用メモリ 342,244 KB
最終ジャッジ日時 2024-10-09 15:52:50
合計ジャッジ時間 76,159 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
50,156 KB
testcase_01 AC 54 ms
50,504 KB
testcase_02 AC 52 ms
50,316 KB
testcase_03 AC 1,846 ms
231,112 KB
testcase_04 AC 3,546 ms
262,164 KB
testcase_05 AC 54 ms
50,196 KB
testcase_06 AC 3,655 ms
309,456 KB
testcase_07 AC 2,583 ms
317,328 KB
testcase_08 AC 53 ms
50,332 KB
testcase_09 AC 54 ms
50,420 KB
testcase_10 AC 556 ms
84,992 KB
testcase_11 AC 158 ms
58,900 KB
testcase_12 AC 874 ms
100,048 KB
testcase_13 AC 144 ms
56,100 KB
testcase_14 AC 1,582 ms
162,684 KB
testcase_15 AC 137 ms
57,992 KB
testcase_16 AC 167 ms
62,268 KB
testcase_17 AC 239 ms
64,824 KB
testcase_18 AC 82 ms
51,508 KB
testcase_19 AC 299 ms
73,392 KB
testcase_20 AC 1,280 ms
167,936 KB
testcase_21 AC 322 ms
76,512 KB
testcase_22 AC 2,251 ms
301,828 KB
testcase_23 AC 2,194 ms
253,460 KB
testcase_24 AC 2,002 ms
241,300 KB
testcase_25 AC 590 ms
104,696 KB
testcase_26 AC 2,682 ms
316,484 KB
testcase_27 AC 1,293 ms
129,904 KB
testcase_28 AC 1,871 ms
236,228 KB
testcase_29 AC 264 ms
73,144 KB
testcase_30 AC 2,104 ms
299,524 KB
testcase_31 AC 2,492 ms
304,164 KB
testcase_32 AC 2,427 ms
280,040 KB
testcase_33 AC 1,886 ms
233,396 KB
testcase_34 AC 2,746 ms
317,620 KB
testcase_35 AC 2,180 ms
215,144 KB
testcase_36 AC 1,021 ms
195,088 KB
testcase_37 AC 2,083 ms
269,748 KB
testcase_38 AC 538 ms
74,520 KB
testcase_39 AC 464 ms
69,616 KB
testcase_40 AC 1,204 ms
116,776 KB
testcase_41 AC 299 ms
75,300 KB
testcase_42 AC 554 ms
88,632 KB
testcase_43 AC 154 ms
56,232 KB
testcase_44 AC 1,186 ms
179,648 KB
testcase_45 AC 2,978 ms
342,244 KB
testcase_46 AC 2,800 ms
255,600 KB
testcase_47 AC 2,310 ms
243,928 KB
testcase_48 AC 2,526 ms
247,204 KB
testcase_49 AC 2,505 ms
317,392 KB
testcase_50 AC 100 ms
55,060 KB
testcase_51 AC 797 ms
111,936 KB
testcase_52 AC 127 ms
55,920 KB
testcase_53 AC 53 ms
50,608 KB
testcase_54 AC 2,079 ms
249,796 KB
testcase_55 AC 663 ms
95,788 KB
testcase_56 AC 496 ms
82,752 KB
testcase_57 AC 801 ms
112,780 KB
testcase_58 AC 668 ms
83,028 KB
testcase_59 AC 647 ms
95,620 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();
        ArrayList<Long> valueA = new ArrayList<>();
        ArrayList<Long> valueB = new ArrayList<>();
        for (int i = 0; i < m; i++) {
            long v = (1L << (sc.nextInt() - 1)) + (1L << (sc.nextInt() - 1));
            if (i % 2 == 0) {
                valueA.add(v);
            } else {
                valueB.add(v);
            }
        }
        long ans = 0;
        for (int i = 0; i < n; i++) {
            ans += (sc.nextLong() << i);
        }
        int sizeA = valueA.size();
        long[] dpA = new long[1 << sizeA];
        dpA[0] = ans;
        HashMap<Long, Integer> countsA = new HashMap<>();
        countsA.put(ans, 0);
        for (int i = 0; i < (1 << sizeA); i++) {
            int pop = getPop(i);
            for (int j = 0; j < sizeA; j++) {
                if ((i & (1 << j)) > 0) {
                    dpA[i] = dpA[i ^ (1 << j)] ^ valueA.get(j);
                    break;
                }
            }
            if (!countsA.containsKey(dpA[i]) || countsA.get(dpA[i]) > pop) {
                countsA.put(dpA[i], pop);
            }
        }
        int sizeB = valueB.size();
        long[] dpB = new long[1 << sizeB];
        HashMap<Long, Integer> countsB = new HashMap<>();
        countsB.put(0L, 0);
        for (int i = 0; i < (1 << sizeB); i++) {
            int pop = getPop(i);
            for (int j = 0; j < sizeB; j++) {
                if ((i & (1 << j)) > 0) {
                    dpB[i] = dpB[i ^ (1 << j)] ^ valueB.get(j);
                    break;
                }
            }
            if (!countsB.containsKey(dpB[i]) || countsB.get(dpB[i]) > pop) {
                countsB.put(dpB[i], pop);
            }
        }
        int result = Integer.MAX_VALUE;
        for (long x : countsA.keySet()) {
            if (countsB.containsKey(x)) {
                result = Math.min(result, countsA.get(x) + countsB.get(x));
            }
        }
        if (result > n) {
            System.out.println(-1);
        } else {
            System.out.println(result);
        }
    }
    
    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