結果

問題 No.1577 織姫と彦星2
ユーザー tentententen
提出日時 2021-06-29 18:53:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 593 ms / 2,000 ms
コード長 2,514 bytes
コンパイル時間 4,442 ms
コンパイル使用メモリ 76,492 KB
実行使用メモリ 74,376 KB
最終ジャッジ日時 2023-09-08 04:01:15
合計ジャッジ時間 21,891 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,496 KB
testcase_01 AC 45 ms
49,524 KB
testcase_02 AC 44 ms
49,712 KB
testcase_03 AC 45 ms
49,556 KB
testcase_04 AC 44 ms
49,496 KB
testcase_05 AC 187 ms
56,980 KB
testcase_06 AC 128 ms
55,344 KB
testcase_07 AC 441 ms
64,700 KB
testcase_08 AC 266 ms
60,864 KB
testcase_09 AC 593 ms
74,376 KB
testcase_10 AC 236 ms
59,060 KB
testcase_11 AC 119 ms
55,460 KB
testcase_12 AC 352 ms
63,692 KB
testcase_13 AC 194 ms
56,772 KB
testcase_14 AC 352 ms
63,040 KB
testcase_15 AC 220 ms
57,804 KB
testcase_16 AC 418 ms
64,732 KB
testcase_17 AC 321 ms
62,740 KB
testcase_18 AC 55 ms
49,752 KB
testcase_19 AC 414 ms
64,984 KB
testcase_20 AC 377 ms
63,456 KB
testcase_21 AC 424 ms
64,936 KB
testcase_22 AC 393 ms
64,788 KB
testcase_23 AC 268 ms
61,004 KB
testcase_24 AC 447 ms
65,184 KB
testcase_25 AC 195 ms
56,996 KB
testcase_26 AC 261 ms
61,060 KB
testcase_27 AC 332 ms
62,416 KB
testcase_28 AC 363 ms
62,556 KB
testcase_29 AC 277 ms
59,968 KB
testcase_30 AC 298 ms
61,120 KB
testcase_31 AC 410 ms
64,724 KB
testcase_32 AC 94 ms
52,172 KB
testcase_33 AC 72 ms
50,452 KB
testcase_34 AC 282 ms
60,332 KB
testcase_35 AC 408 ms
64,820 KB
testcase_36 AC 392 ms
64,964 KB
testcase_37 AC 521 ms
71,476 KB
testcase_38 AC 501 ms
68,608 KB
testcase_39 AC 294 ms
61,032 KB
testcase_40 AC 260 ms
59,568 KB
testcase_41 AC 404 ms
65,388 KB
testcase_42 AC 280 ms
60,816 KB
testcase_43 AC 293 ms
60,940 KB
testcase_44 AC 92 ms
52,212 KB
testcase_45 AC 178 ms
56,700 KB
testcase_46 AC 177 ms
57,052 KB
testcase_47 AC 335 ms
62,896 KB
testcase_48 AC 241 ms
59,944 KB
testcase_49 AC 290 ms
60,780 KB
testcase_50 AC 221 ms
59,660 KB
testcase_51 AC 131 ms
55,692 KB
testcase_52 AC 429 ms
65,688 KB
testcase_53 AC 457 ms
64,756 KB
testcase_54 AC 278 ms
60,400 KB
testcase_55 AC 410 ms
64,840 KB
testcase_56 AC 217 ms
59,952 KB
testcase_57 AC 157 ms
56,268 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();
        HashMap<Integer, Integer> map = new HashMap<>();
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n + 2; i++) {
            map.put(sc.nextInt(), i);
            graph.add(new ArrayList<>());
        }
        for (int x : map.keySet()) {
            for (int i = 0; i < 30; i++) {
                if (map.containsKey(x ^ (1 << i))) {
                    int a = map.get(x);
                    int b = map.get(x ^ (1 << i));
                    graph.get(a).add(b);
                    graph.get(b).add(a);
                }
            }
        }
        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 int getPop(int x) {
        int pop = 0;
        while (x > 0) {
            pop += x % 2;
            x >>= 1;
        }
        return pop;
    }
    
    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 String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0