結果

問題 No.1577 織姫と彦星2
ユーザー tentententen
提出日時 2021-11-29 14:14:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 577 ms / 2,000 ms
コード長 2,459 bytes
コンパイル時間 2,180 ms
コンパイル使用メモリ 77,184 KB
実行使用メモリ 74,192 KB
最終ジャッジ日時 2023-09-15 06:56:30
合計ジャッジ時間 20,194 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,928 KB
testcase_01 AC 43 ms
49,868 KB
testcase_02 AC 42 ms
49,572 KB
testcase_03 AC 41 ms
49,664 KB
testcase_04 AC 42 ms
49,944 KB
testcase_05 AC 188 ms
56,812 KB
testcase_06 AC 115 ms
55,564 KB
testcase_07 AC 433 ms
64,696 KB
testcase_08 AC 278 ms
60,420 KB
testcase_09 AC 577 ms
74,192 KB
testcase_10 AC 247 ms
59,456 KB
testcase_11 AC 121 ms
55,400 KB
testcase_12 AC 334 ms
63,540 KB
testcase_13 AC 201 ms
56,604 KB
testcase_14 AC 382 ms
62,608 KB
testcase_15 AC 216 ms
58,108 KB
testcase_16 AC 423 ms
65,108 KB
testcase_17 AC 327 ms
60,728 KB
testcase_18 AC 54 ms
49,760 KB
testcase_19 AC 383 ms
64,916 KB
testcase_20 AC 377 ms
63,440 KB
testcase_21 AC 422 ms
64,856 KB
testcase_22 AC 408 ms
64,924 KB
testcase_23 AC 237 ms
60,500 KB
testcase_24 AC 458 ms
64,976 KB
testcase_25 AC 189 ms
57,244 KB
testcase_26 AC 248 ms
61,288 KB
testcase_27 AC 336 ms
62,736 KB
testcase_28 AC 356 ms
63,300 KB
testcase_29 AC 260 ms
60,028 KB
testcase_30 AC 293 ms
60,724 KB
testcase_31 AC 405 ms
65,056 KB
testcase_32 AC 91 ms
51,856 KB
testcase_33 AC 82 ms
52,424 KB
testcase_34 AC 260 ms
60,468 KB
testcase_35 AC 387 ms
65,324 KB
testcase_36 AC 397 ms
65,024 KB
testcase_37 AC 529 ms
71,304 KB
testcase_38 AC 492 ms
68,548 KB
testcase_39 AC 262 ms
60,400 KB
testcase_40 AC 253 ms
60,024 KB
testcase_41 AC 400 ms
64,752 KB
testcase_42 AC 297 ms
60,332 KB
testcase_43 AC 303 ms
61,344 KB
testcase_44 AC 93 ms
52,464 KB
testcase_45 AC 181 ms
57,236 KB
testcase_46 AC 177 ms
56,720 KB
testcase_47 AC 336 ms
62,592 KB
testcase_48 AC 234 ms
59,988 KB
testcase_49 AC 288 ms
60,816 KB
testcase_50 AC 220 ms
59,496 KB
testcase_51 AC 136 ms
55,648 KB
testcase_52 AC 437 ms
66,072 KB
testcase_53 AC 458 ms
65,748 KB
testcase_54 AC 275 ms
60,116 KB
testcase_55 AC 401 ms
64,828 KB
testcase_56 AC 222 ms
60,136 KB
testcase_57 AC 157 ms
56,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

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<>());
            idxes.put(sc.nextInt(), i);
        }
        for (int x : idxes.keySet()) {
            for (int i = 0; i < 30; i++) {
                if (idxes.containsKey(x ^ (1 << i))) {
                    int a = idxes.get(x);
                    int b = idxes.get(x ^ (1 << i));
                    graph.get(a).add(b);
                    graph.get(b).add(a);
                }
            }
        }
       int[] costs = new int[n + 2];
        Arrays.fill(costs, Integer.MAX_VALUE);
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(0, 0));
        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 Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0