結果

問題 No.1577 織姫と彦星2
ユーザー tentententen
提出日時 2023-07-18 17:10:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 530 ms / 2,000 ms
コード長 3,046 bytes
コンパイル時間 3,175 ms
コンパイル使用メモリ 86,552 KB
実行使用メモリ 68,048 KB
最終ジャッジ日時 2023-10-18 20:31:09
合計ジャッジ時間 21,921 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
53,596 KB
testcase_01 AC 56 ms
53,588 KB
testcase_02 AC 58 ms
53,576 KB
testcase_03 AC 57 ms
53,576 KB
testcase_04 AC 56 ms
53,584 KB
testcase_05 AC 199 ms
59,092 KB
testcase_06 AC 128 ms
58,004 KB
testcase_07 AC 420 ms
65,112 KB
testcase_08 AC 257 ms
62,656 KB
testcase_09 AC 530 ms
68,048 KB
testcase_10 AC 240 ms
62,140 KB
testcase_11 AC 128 ms
57,972 KB
testcase_12 AC 344 ms
63,112 KB
testcase_13 AC 228 ms
59,980 KB
testcase_14 AC 370 ms
63,168 KB
testcase_15 AC 225 ms
60,588 KB
testcase_16 AC 426 ms
65,440 KB
testcase_17 AC 318 ms
62,956 KB
testcase_18 AC 68 ms
53,748 KB
testcase_19 AC 389 ms
65,096 KB
testcase_20 AC 366 ms
64,800 KB
testcase_21 AC 405 ms
65,144 KB
testcase_22 AC 393 ms
65,032 KB
testcase_23 AC 271 ms
61,216 KB
testcase_24 AC 444 ms
67,032 KB
testcase_25 AC 203 ms
59,584 KB
testcase_26 AC 261 ms
60,104 KB
testcase_27 AC 359 ms
64,680 KB
testcase_28 AC 354 ms
63,168 KB
testcase_29 AC 280 ms
62,728 KB
testcase_30 AC 304 ms
62,920 KB
testcase_31 AC 408 ms
65,840 KB
testcase_32 AC 120 ms
55,408 KB
testcase_33 AC 91 ms
55,032 KB
testcase_34 AC 274 ms
62,800 KB
testcase_35 AC 392 ms
65,080 KB
testcase_36 AC 380 ms
65,240 KB
testcase_37 AC 499 ms
67,200 KB
testcase_38 AC 469 ms
67,108 KB
testcase_39 AC 281 ms
63,180 KB
testcase_40 AC 283 ms
62,948 KB
testcase_41 AC 409 ms
65,288 KB
testcase_42 AC 319 ms
63,256 KB
testcase_43 AC 319 ms
62,640 KB
testcase_44 AC 105 ms
55,132 KB
testcase_45 AC 187 ms
59,156 KB
testcase_46 AC 196 ms
58,988 KB
testcase_47 AC 359 ms
64,988 KB
testcase_48 AC 246 ms
62,560 KB
testcase_49 AC 305 ms
63,168 KB
testcase_50 AC 253 ms
60,304 KB
testcase_51 AC 150 ms
58,252 KB
testcase_52 AC 453 ms
66,940 KB
testcase_53 AC 436 ms
66,908 KB
testcase_54 AC 288 ms
63,088 KB
testcase_55 AC 420 ms
65,124 KB
testcase_56 AC 221 ms
60,248 KB
testcase_57 AC 168 ms
59,064 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<>();
        for (int i = 0; i < n + 2; i++) {
            graph.add(new ArrayList<>());
        }
        HashMap<Integer, Integer> values = new HashMap<>();
        for (int i = 0; i < n + 2; i++) {
            values.put(sc.nextInt(), i);
        }
        for (int x : values.keySet()) {
            int y = x;
            for (int i = 0; i < 30; i++) {
                y ^= (1 << i);
                if (values.containsKey(y)) {
                    graph.get(values.get(x)).add(values.get(y));
                }
                y ^= (1 << 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