結果

問題 No.1577 織姫と彦星2
ユーザー tentententen
提出日時 2021-06-29 18:54:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 475 ms / 2,000 ms
コード長 2,411 bytes
コンパイル時間 2,200 ms
コンパイル使用メモリ 77,072 KB
実行使用メモリ 66,268 KB
最終ジャッジ日時 2023-09-08 04:03:02
合計ジャッジ時間 19,107 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,504 KB
testcase_01 AC 43 ms
49,744 KB
testcase_02 AC 43 ms
49,648 KB
testcase_03 AC 43 ms
49,624 KB
testcase_04 AC 45 ms
50,236 KB
testcase_05 AC 181 ms
56,584 KB
testcase_06 AC 122 ms
54,892 KB
testcase_07 AC 395 ms
62,348 KB
testcase_08 AC 244 ms
59,500 KB
testcase_09 AC 475 ms
64,768 KB
testcase_10 AC 233 ms
57,012 KB
testcase_11 AC 104 ms
54,800 KB
testcase_12 AC 311 ms
60,344 KB
testcase_13 AC 200 ms
56,512 KB
testcase_14 AC 339 ms
62,076 KB
testcase_15 AC 199 ms
56,728 KB
testcase_16 AC 383 ms
63,012 KB
testcase_17 AC 282 ms
60,212 KB
testcase_18 AC 53 ms
49,880 KB
testcase_19 AC 352 ms
62,020 KB
testcase_20 AC 347 ms
62,112 KB
testcase_21 AC 383 ms
62,172 KB
testcase_22 AC 366 ms
62,088 KB
testcase_23 AC 239 ms
60,536 KB
testcase_24 AC 394 ms
64,776 KB
testcase_25 AC 181 ms
56,480 KB
testcase_26 AC 223 ms
59,232 KB
testcase_27 AC 318 ms
62,152 KB
testcase_28 AC 332 ms
60,004 KB
testcase_29 AC 258 ms
60,232 KB
testcase_30 AC 287 ms
60,844 KB
testcase_31 AC 357 ms
62,392 KB
testcase_32 AC 88 ms
52,504 KB
testcase_33 AC 69 ms
52,328 KB
testcase_34 AC 260 ms
59,716 KB
testcase_35 AC 354 ms
62,180 KB
testcase_36 AC 358 ms
62,164 KB
testcase_37 AC 441 ms
66,268 KB
testcase_38 AC 422 ms
64,144 KB
testcase_39 AC 259 ms
59,640 KB
testcase_40 AC 248 ms
58,484 KB
testcase_41 AC 363 ms
62,060 KB
testcase_42 AC 297 ms
60,232 KB
testcase_43 AC 301 ms
60,132 KB
testcase_44 AC 98 ms
51,832 KB
testcase_45 AC 170 ms
56,148 KB
testcase_46 AC 174 ms
56,356 KB
testcase_47 AC 335 ms
61,764 KB
testcase_48 AC 228 ms
59,584 KB
testcase_49 AC 282 ms
60,164 KB
testcase_50 AC 221 ms
57,200 KB
testcase_51 AC 130 ms
55,332 KB
testcase_52 AC 408 ms
64,484 KB
testcase_53 AC 406 ms
64,276 KB
testcase_54 AC 263 ms
59,980 KB
testcase_55 AC 384 ms
61,960 KB
testcase_56 AC 208 ms
57,508 KB
testcase_57 AC 151 ms
55,520 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))) {
                    graph.get(map.get(x)).add(map.get(x ^ (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 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