結果
問題 | No.1577 織姫と彦星2 |
ユーザー | tenten |
提出日時 | 2024-01-11 12:50:48 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 479 ms / 2,000 ms |
コード長 | 2,857 bytes |
コンパイル時間 | 3,032 ms |
コンパイル使用メモリ | 92,244 KB |
実行使用メモリ | 63,564 KB |
最終ジャッジ日時 | 2024-09-27 20:33:03 |
合計ジャッジ時間 | 21,625 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 57 ms
50,120 KB |
testcase_01 | AC | 56 ms
50,012 KB |
testcase_02 | AC | 55 ms
50,348 KB |
testcase_03 | AC | 57 ms
49,900 KB |
testcase_04 | AC | 57 ms
50,012 KB |
testcase_05 | AC | 210 ms
56,148 KB |
testcase_06 | AC | 130 ms
54,920 KB |
testcase_07 | AC | 427 ms
63,104 KB |
testcase_08 | AC | 270 ms
58,728 KB |
testcase_09 | AC | 479 ms
63,564 KB |
testcase_10 | AC | 246 ms
58,724 KB |
testcase_11 | AC | 120 ms
54,384 KB |
testcase_12 | AC | 326 ms
61,080 KB |
testcase_13 | AC | 225 ms
59,768 KB |
testcase_14 | AC | 364 ms
61,244 KB |
testcase_15 | AC | 220 ms
56,544 KB |
testcase_16 | AC | 408 ms
63,516 KB |
testcase_17 | AC | 311 ms
58,800 KB |
testcase_18 | AC | 63 ms
50,416 KB |
testcase_19 | AC | 361 ms
61,400 KB |
testcase_20 | AC | 344 ms
61,276 KB |
testcase_21 | AC | 398 ms
61,380 KB |
testcase_22 | AC | 373 ms
61,068 KB |
testcase_23 | AC | 236 ms
58,784 KB |
testcase_24 | AC | 440 ms
63,300 KB |
testcase_25 | AC | 199 ms
55,996 KB |
testcase_26 | AC | 260 ms
58,844 KB |
testcase_27 | AC | 355 ms
61,220 KB |
testcase_28 | AC | 339 ms
61,228 KB |
testcase_29 | AC | 274 ms
59,012 KB |
testcase_30 | AC | 298 ms
59,192 KB |
testcase_31 | AC | 410 ms
61,156 KB |
testcase_32 | AC | 106 ms
51,760 KB |
testcase_33 | AC | 84 ms
51,412 KB |
testcase_34 | AC | 273 ms
58,620 KB |
testcase_35 | AC | 370 ms
61,520 KB |
testcase_36 | AC | 398 ms
61,176 KB |
testcase_37 | AC | 463 ms
63,508 KB |
testcase_38 | AC | 462 ms
63,440 KB |
testcase_39 | AC | 261 ms
58,780 KB |
testcase_40 | AC | 262 ms
58,900 KB |
testcase_41 | AC | 415 ms
61,284 KB |
testcase_42 | AC | 321 ms
59,204 KB |
testcase_43 | AC | 333 ms
59,088 KB |
testcase_44 | AC | 110 ms
52,080 KB |
testcase_45 | AC | 182 ms
55,684 KB |
testcase_46 | AC | 196 ms
55,788 KB |
testcase_47 | AC | 376 ms
60,792 KB |
testcase_48 | AC | 247 ms
59,084 KB |
testcase_49 | AC | 289 ms
58,920 KB |
testcase_50 | AC | 221 ms
58,208 KB |
testcase_51 | AC | 138 ms
54,432 KB |
testcase_52 | AC | 441 ms
63,448 KB |
testcase_53 | AC | 408 ms
63,440 KB |
testcase_54 | AC | 289 ms
59,184 KB |
testcase_55 | AC | 429 ms
63,336 KB |
testcase_56 | AC | 216 ms
56,716 KB |
testcase_57 | AC | 160 ms
55,688 KB |
ソースコード
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[] values = new int[n + 2]; HashMap<Integer, Integer> idxes = new HashMap<>(); ArrayList<ArrayList<Integer>> graph = new ArrayList<>(); for (int i = 0; i < n + 2; i++) { values[i] = sc.nextInt(); idxes.put(values[i], i); graph.add(new ArrayList<>()); } for (int i = 0; i < n + 2; i++) { for (int j = 0; j < 30; j++) { if (idxes.containsKey(values[i] ^ (1 << j))) { graph.get(i).add(idxes.get(values[i] ^ (1 << j))); } } } ArrayDeque<Path> deq = new ArrayDeque<>(); int[] costs = new int[n + 2]; Arrays.fill(costs, Integer.MAX_VALUE); deq.add(new Path(0, 0)); while (deq.size() > 0) { Path p = deq.poll(); if (costs[p.idx] <= p.value) { continue; } costs[p.idx] = p.value; for (int x : graph.get(p.idx)) { deq.add(new Path(x, p.value + 1)); } } if (costs[1] == Integer.MAX_VALUE) { System.out.println(-1); } else { System.out.println(costs[1] - 1); } } static class Path { int idx; int value; public Path(int idx, int value) { this.idx = idx; this.value = value; } } } 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(); } }