結果
問題 | No.1577 織姫と彦星2 |
ユーザー | tenten |
提出日時 | 2023-03-25 09:53:53 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 622 ms / 2,000 ms |
コード長 | 2,961 bytes |
コンパイル時間 | 3,519 ms |
コンパイル使用メモリ | 86,372 KB |
実行使用メモリ | 61,772 KB |
最終ジャッジ日時 | 2024-09-18 17:59:17 |
合計ジャッジ時間 | 18,622 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 50 ms
36,924 KB |
testcase_01 | AC | 49 ms
37,500 KB |
testcase_02 | AC | 48 ms
36,924 KB |
testcase_03 | AC | 49 ms
37,192 KB |
testcase_04 | AC | 50 ms
37,184 KB |
testcase_05 | AC | 171 ms
47,292 KB |
testcase_06 | AC | 110 ms
42,988 KB |
testcase_07 | AC | 444 ms
57,924 KB |
testcase_08 | AC | 222 ms
50,100 KB |
testcase_09 | AC | 622 ms
61,772 KB |
testcase_10 | AC | 202 ms
48,456 KB |
testcase_11 | AC | 114 ms
41,404 KB |
testcase_12 | AC | 322 ms
53,616 KB |
testcase_13 | AC | 195 ms
47,656 KB |
testcase_14 | AC | 310 ms
55,140 KB |
testcase_15 | AC | 188 ms
48,464 KB |
testcase_16 | AC | 374 ms
57,476 KB |
testcase_17 | AC | 261 ms
52,920 KB |
testcase_18 | AC | 57 ms
37,516 KB |
testcase_19 | AC | 359 ms
56,860 KB |
testcase_20 | AC | 305 ms
55,772 KB |
testcase_21 | AC | 362 ms
57,744 KB |
testcase_22 | AC | 384 ms
56,768 KB |
testcase_23 | AC | 224 ms
50,156 KB |
testcase_24 | AC | 408 ms
57,920 KB |
testcase_25 | AC | 170 ms
47,188 KB |
testcase_26 | AC | 218 ms
49,632 KB |
testcase_27 | AC | 329 ms
53,856 KB |
testcase_28 | AC | 304 ms
55,272 KB |
testcase_29 | AC | 270 ms
50,288 KB |
testcase_30 | AC | 279 ms
50,740 KB |
testcase_31 | AC | 382 ms
57,424 KB |
testcase_32 | AC | 98 ms
40,200 KB |
testcase_33 | AC | 80 ms
39,064 KB |
testcase_34 | AC | 231 ms
51,192 KB |
testcase_35 | AC | 377 ms
57,252 KB |
testcase_36 | AC | 399 ms
56,580 KB |
testcase_37 | AC | 569 ms
60,568 KB |
testcase_38 | AC | 518 ms
59,408 KB |
testcase_39 | AC | 228 ms
51,876 KB |
testcase_40 | AC | 221 ms
49,648 KB |
testcase_41 | AC | 370 ms
56,448 KB |
testcase_42 | AC | 295 ms
52,792 KB |
testcase_43 | AC | 306 ms
52,504 KB |
testcase_44 | AC | 92 ms
39,820 KB |
testcase_45 | AC | 157 ms
46,520 KB |
testcase_46 | AC | 165 ms
47,320 KB |
testcase_47 | AC | 321 ms
54,512 KB |
testcase_48 | AC | 218 ms
48,808 KB |
testcase_49 | AC | 249 ms
51,364 KB |
testcase_50 | AC | 192 ms
49,140 KB |
testcase_51 | AC | 130 ms
43,916 KB |
testcase_52 | AC | 418 ms
59,256 KB |
testcase_53 | AC | 475 ms
57,904 KB |
testcase_54 | AC | 236 ms
49,772 KB |
testcase_55 | AC | 364 ms
57,432 KB |
testcase_56 | AC | 173 ms
48,948 KB |
testcase_57 | AC | 146 ms
46,040 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(); ArrayList<ArrayList<Integer>> graph = new ArrayList<>(); HashMap<Integer, Integer> idxes = new HashMap<>(); for (int i = 0; i < n + 2; i++) { graph.add(new ArrayList<>()); int x = sc.nextInt(); for (int j = 0; j <= 30; j++) { if (idxes.containsKey(x ^ (1 << j))) { int y = idxes.get(x ^ (1 << j)); graph.get(i).add(y); graph.get(y).add(i); } } idxes.put(x, i); } PriorityQueue<Path> queue = new PriorityQueue<>(); queue.add(new Path(0, 0)); int[] costs = new int[n + 2]; Arrays.fill(costs, Integer.MAX_VALUE); while (queue.size() > 0) { Path p = queue.poll(); if (costs[p.idx] <= p.value) { continue; } costs[p.idx] = p.value; for (int x : graph.get(p.idx)) { queue.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 implements Comparable<Path> { int idx; int value; public Path(int idx, int value) { this.idx = idx; this.value = value; } public int compareTo(Path another) { return value - another.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(); } }