結果

問題 No.1577 織姫と彦星2
ユーザー tentententen
提出日時 2023-03-25 09:53:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 767 ms / 2,000 ms
コード長 2,961 bytes
コンパイル時間 4,185 ms
コンパイル使用メモリ 86,076 KB
実行使用メモリ 74,492 KB
最終ジャッジ日時 2023-10-18 21:57:34
合計ジャッジ時間 24,819 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
53,596 KB
testcase_01 AC 59 ms
53,584 KB
testcase_02 AC 57 ms
53,600 KB
testcase_03 AC 57 ms
53,592 KB
testcase_04 AC 57 ms
53,592 KB
testcase_05 AC 206 ms
60,496 KB
testcase_06 AC 131 ms
58,780 KB
testcase_07 AC 529 ms
71,404 KB
testcase_08 AC 259 ms
64,388 KB
testcase_09 AC 767 ms
74,492 KB
testcase_10 AC 254 ms
59,356 KB
testcase_11 AC 135 ms
57,916 KB
testcase_12 AC 357 ms
68,420 KB
testcase_13 AC 218 ms
61,148 KB
testcase_14 AC 435 ms
69,044 KB
testcase_15 AC 227 ms
61,820 KB
testcase_16 AC 491 ms
71,124 KB
testcase_17 AC 320 ms
66,076 KB
testcase_18 AC 71 ms
53,684 KB
testcase_19 AC 476 ms
71,368 KB
testcase_20 AC 397 ms
69,232 KB
testcase_21 AC 487 ms
71,604 KB
testcase_22 AC 469 ms
71,564 KB
testcase_23 AC 252 ms
64,164 KB
testcase_24 AC 576 ms
71,592 KB
testcase_25 AC 208 ms
61,016 KB
testcase_26 AC 266 ms
64,676 KB
testcase_27 AC 403 ms
68,152 KB
testcase_28 AC 391 ms
68,732 KB
testcase_29 AC 307 ms
65,144 KB
testcase_30 AC 307 ms
64,860 KB
testcase_31 AC 442 ms
70,780 KB
testcase_32 AC 109 ms
55,504 KB
testcase_33 AC 88 ms
54,916 KB
testcase_34 AC 292 ms
64,080 KB
testcase_35 AC 452 ms
71,460 KB
testcase_36 AC 490 ms
69,412 KB
testcase_37 AC 735 ms
73,532 KB
testcase_38 AC 633 ms
72,136 KB
testcase_39 AC 290 ms
64,968 KB
testcase_40 AC 276 ms
64,160 KB
testcase_41 AC 486 ms
69,424 KB
testcase_42 AC 331 ms
65,576 KB
testcase_43 AC 344 ms
65,872 KB
testcase_44 AC 103 ms
54,860 KB
testcase_45 AC 186 ms
60,260 KB
testcase_46 AC 198 ms
60,820 KB
testcase_47 AC 429 ms
68,172 KB
testcase_48 AC 242 ms
61,596 KB
testcase_49 AC 316 ms
64,744 KB
testcase_50 AC 243 ms
64,028 KB
testcase_51 AC 149 ms
58,652 KB
testcase_52 AC 600 ms
71,812 KB
testcase_53 AC 595 ms
71,948 KB
testcase_54 AC 308 ms
64,700 KB
testcase_55 AC 528 ms
71,428 KB
testcase_56 AC 221 ms
61,904 KB
testcase_57 AC 175 ms
59,932 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();
        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();
    }
    
}
0