結果
問題 | No.2236 Lights Out On Simple Graph |
ユーザー |
![]() |
提出日時 | 2023-04-13 18:12:21 |
言語 | Java (openjdk 23) |
結果 |
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 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 57 |
ソースコード
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();}}