結果

問題 No.1577 織姫と彦星2
ユーザー tentententen
提出日時 2022-08-18 13:39:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 539 ms / 2,000 ms
コード長 2,503 bytes
コンパイル時間 3,082 ms
コンパイル使用メモリ 80,228 KB
実行使用メモリ 57,616 KB
最終ジャッジ日時 2024-04-15 22:05:13
合計ジャッジ時間 19,909 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
36,756 KB
testcase_01 AC 51 ms
37,268 KB
testcase_02 AC 51 ms
37,232 KB
testcase_03 AC 52 ms
37,076 KB
testcase_04 AC 51 ms
37,120 KB
testcase_05 AC 191 ms
45,396 KB
testcase_06 AC 137 ms
42,576 KB
testcase_07 AC 438 ms
52,932 KB
testcase_08 AC 254 ms
48,012 KB
testcase_09 AC 539 ms
57,616 KB
testcase_10 AC 229 ms
47,024 KB
testcase_11 AC 112 ms
40,924 KB
testcase_12 AC 301 ms
50,172 KB
testcase_13 AC 199 ms
46,444 KB
testcase_14 AC 339 ms
50,724 KB
testcase_15 AC 211 ms
46,688 KB
testcase_16 AC 414 ms
52,304 KB
testcase_17 AC 309 ms
48,720 KB
testcase_18 AC 58 ms
36,880 KB
testcase_19 AC 382 ms
51,852 KB
testcase_20 AC 330 ms
51,148 KB
testcase_21 AC 375 ms
53,048 KB
testcase_22 AC 392 ms
51,832 KB
testcase_23 AC 219 ms
48,068 KB
testcase_24 AC 427 ms
52,968 KB
testcase_25 AC 193 ms
46,312 KB
testcase_26 AC 241 ms
48,032 KB
testcase_27 AC 347 ms
50,388 KB
testcase_28 AC 334 ms
50,556 KB
testcase_29 AC 278 ms
48,352 KB
testcase_30 AC 316 ms
48,688 KB
testcase_31 AC 418 ms
52,188 KB
testcase_32 AC 105 ms
39,828 KB
testcase_33 AC 85 ms
38,548 KB
testcase_34 AC 282 ms
48,356 KB
testcase_35 AC 399 ms
51,716 KB
testcase_36 AC 389 ms
51,700 KB
testcase_37 AC 522 ms
55,152 KB
testcase_38 AC 454 ms
54,444 KB
testcase_39 AC 272 ms
48,536 KB
testcase_40 AC 255 ms
48,348 KB
testcase_41 AC 415 ms
51,908 KB
testcase_42 AC 308 ms
49,172 KB
testcase_43 AC 302 ms
49,620 KB
testcase_44 AC 107 ms
39,776 KB
testcase_45 AC 174 ms
45,636 KB
testcase_46 AC 187 ms
45,176 KB
testcase_47 AC 358 ms
50,624 KB
testcase_48 AC 231 ms
47,996 KB
testcase_49 AC 317 ms
49,048 KB
testcase_50 AC 225 ms
47,332 KB
testcase_51 AC 143 ms
42,984 KB
testcase_52 AC 416 ms
53,480 KB
testcase_53 AC 424 ms
52,768 KB
testcase_54 AC 292 ms
48,740 KB
testcase_55 AC 407 ms
52,976 KB
testcase_56 AC 218 ms
47,268 KB
testcase_57 AC 164 ms
44,956 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> idxes = new HashMap<>();
        int[] values = new int[n + 2];
        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 + 1; i++) {
            int x = values[i];
            for (int j = 0; j < 30; j++) {
                x ^= (1 << j);
                if (idxes.containsKey(x)) {
                    graph.get(idxes.get(x)).add(i);
                }
                x ^= (1 << j);
            }
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        int[] costs = new int[n + 2];
        Arrays.fill(costs, Integer.MAX_VALUE);
        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 {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0