結果

問題 No.2236 Lights Out On Simple Graph
ユーザー tentententen
提出日時 2023-04-13 18:12:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 3,665 ms / 4,000 ms
コード長 3,691 bytes
コンパイル時間 3,234 ms
コンパイル使用メモリ 85,352 KB
実行使用メモリ 344,288 KB
最終ジャッジ日時 2024-04-17 22:07:58
合計ジャッジ時間 17,173 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
50,536 KB
testcase_01 AC 52 ms
50,160 KB
testcase_02 AC 52 ms
50,096 KB
testcase_03 AC 1,737 ms
231,448 KB
testcase_04 AC 3,521 ms
265,000 KB
testcase_05 AC 49 ms
50,180 KB
testcase_06 AC 3,665 ms
343,196 KB
testcase_07 AC 2,265 ms
321,520 KB
testcase_08 AC 49 ms
50,524 KB
testcase_09 AC 49 ms
50,500 KB
testcase_10 AC 482 ms
83,772 KB
testcase_11 AC 143 ms
61,152 KB
testcase_12 AC 736 ms
99,108 KB
testcase_13 AC 136 ms
55,836 KB
testcase_14 AC 1,700 ms
163,812 KB
testcase_15 AC 139 ms
58,344 KB
testcase_16 AC 183 ms
63,480 KB
testcase_17 AC 245 ms
65,708 KB
testcase_18 AC 83 ms
51,460 KB
testcase_19 AC 308 ms
74,760 KB
testcase_20 AC 1,361 ms
168,632 KB
testcase_21 AC 342 ms
76,676 KB
testcase_22 AC 2,390 ms
305,576 KB
testcase_23 AC 2,537 ms
262,584 KB
testcase_24 AC 1,811 ms
244,708 KB
testcase_25 AC 543 ms
105,656 KB
testcase_26 AC 2,515 ms
317,316 KB
testcase_27 AC 1,222 ms
129,564 KB
testcase_28 AC 1,978 ms
236,516 KB
testcase_29 AC 308 ms
73,124 KB
testcase_30 AC 1,964 ms
283,772 KB
testcase_31 AC 2,506 ms
321,816 KB
testcase_32 AC 2,430 ms
281,596 KB
testcase_33 AC 1,849 ms
237,696 KB
testcase_34 AC 2,514 ms
322,080 KB
testcase_35 AC 2,226 ms
216,404 KB
testcase_36 AC 1,091 ms
196,740 KB
testcase_37 AC 2,027 ms
269,632 KB
testcase_38 AC 524 ms
74,484 KB
testcase_39 AC 406 ms
69,276 KB
testcase_40 AC 1,202 ms
119,300 KB
testcase_41 AC 336 ms
75,820 KB
testcase_42 AC 639 ms
90,128 KB
testcase_43 AC 146 ms
56,276 KB
testcase_44 AC 1,216 ms
180,312 KB
testcase_45 AC 2,847 ms
344,288 KB
testcase_46 AC 2,600 ms
260,168 KB
testcase_47 AC 1,954 ms
255,112 KB
testcase_48 AC 2,531 ms
247,376 KB
testcase_49 AC 2,456 ms
321,640 KB
testcase_50 AC 106 ms
55,024 KB
testcase_51 AC 729 ms
115,012 KB
testcase_52 AC 129 ms
58,460 KB
testcase_53 AC 49 ms
50,516 KB
testcase_54 AC 1,954 ms
249,640 KB
testcase_55 AC 590 ms
97,956 KB
testcase_56 AC 510 ms
83,248 KB
testcase_57 AC 871 ms
113,100 KB
testcase_58 AC 592 ms
83,304 KB
testcase_59 AC 589 ms
95,708 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