結果

問題 No.1577 織姫と彦星2
ユーザー tentententen
提出日時 2024-01-11 12:50:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 447 ms / 2,000 ms
コード長 2,857 bytes
コンパイル時間 2,636 ms
コンパイル使用メモリ 91,968 KB
実行使用メモリ 67,588 KB
最終ジャッジ日時 2024-01-11 12:51:11
合計ジャッジ時間 19,545 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
53,944 KB
testcase_01 AC 57 ms
53,976 KB
testcase_02 AC 52 ms
53,992 KB
testcase_03 AC 51 ms
53,988 KB
testcase_04 AC 52 ms
52,020 KB
testcase_05 AC 185 ms
59,220 KB
testcase_06 AC 129 ms
58,232 KB
testcase_07 AC 377 ms
67,156 KB
testcase_08 AC 231 ms
62,532 KB
testcase_09 AC 447 ms
67,588 KB
testcase_10 AC 231 ms
62,512 KB
testcase_11 AC 109 ms
57,928 KB
testcase_12 AC 281 ms
62,904 KB
testcase_13 AC 234 ms
62,272 KB
testcase_14 AC 315 ms
65,012 KB
testcase_15 AC 195 ms
59,596 KB
testcase_16 AC 355 ms
67,172 KB
testcase_17 AC 316 ms
62,716 KB
testcase_18 AC 58 ms
53,988 KB
testcase_19 AC 337 ms
65,048 KB
testcase_20 AC 307 ms
65,072 KB
testcase_21 AC 325 ms
65,060 KB
testcase_22 AC 372 ms
64,996 KB
testcase_23 AC 208 ms
62,548 KB
testcase_24 AC 381 ms
66,892 KB
testcase_25 AC 183 ms
59,772 KB
testcase_26 AC 249 ms
62,328 KB
testcase_27 AC 352 ms
64,528 KB
testcase_28 AC 307 ms
64,960 KB
testcase_29 AC 258 ms
62,776 KB
testcase_30 AC 303 ms
62,880 KB
testcase_31 AC 309 ms
65,148 KB
testcase_32 AC 105 ms
55,532 KB
testcase_33 AC 73 ms
55,148 KB
testcase_34 AC 265 ms
62,684 KB
testcase_35 AC 368 ms
65,048 KB
testcase_36 AC 350 ms
65,052 KB
testcase_37 AC 397 ms
66,860 KB
testcase_38 AC 403 ms
67,072 KB
testcase_39 AC 236 ms
62,668 KB
testcase_40 AC 255 ms
62,620 KB
testcase_41 AC 338 ms
65,076 KB
testcase_42 AC 283 ms
62,524 KB
testcase_43 AC 289 ms
62,804 KB
testcase_44 AC 113 ms
55,640 KB
testcase_45 AC 165 ms
59,628 KB
testcase_46 AC 194 ms
59,668 KB
testcase_47 AC 345 ms
64,676 KB
testcase_48 AC 243 ms
62,816 KB
testcase_49 AC 262 ms
62,760 KB
testcase_50 AC 214 ms
62,184 KB
testcase_51 AC 126 ms
58,528 KB
testcase_52 AC 389 ms
66,984 KB
testcase_53 AC 374 ms
64,860 KB
testcase_54 AC 291 ms
62,832 KB
testcase_55 AC 360 ms
66,836 KB
testcase_56 AC 198 ms
60,656 KB
testcase_57 AC 155 ms
59,528 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();
        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();
    }
    
}
0