結果

問題 No.1576 織姫と彦星
ユーザー tentententen
提出日時 2024-04-23 18:40:15
言語 Java21
(openjdk 21)
結果
AC  
実行時間 210 ms / 2,000 ms
コード長 2,556 bytes
コンパイル時間 2,718 ms
コンパイル使用メモリ 79,864 KB
実行使用メモリ 47,492 KB
最終ジャッジ日時 2024-04-23 18:40:28
合計ジャッジ時間 12,489 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
36,768 KB
testcase_01 AC 55 ms
37,360 KB
testcase_02 AC 56 ms
37,340 KB
testcase_03 AC 55 ms
37,140 KB
testcase_04 AC 54 ms
37,060 KB
testcase_05 AC 56 ms
37,076 KB
testcase_06 AC 57 ms
37,344 KB
testcase_07 AC 137 ms
43,576 KB
testcase_08 AC 131 ms
42,216 KB
testcase_09 AC 59 ms
37,360 KB
testcase_10 AC 181 ms
45,860 KB
testcase_11 AC 59 ms
37,432 KB
testcase_12 AC 158 ms
45,212 KB
testcase_13 AC 190 ms
46,612 KB
testcase_14 AC 170 ms
45,868 KB
testcase_15 AC 170 ms
45,684 KB
testcase_16 AC 191 ms
46,676 KB
testcase_17 AC 176 ms
45,508 KB
testcase_18 AC 115 ms
40,680 KB
testcase_19 AC 130 ms
42,320 KB
testcase_20 AC 168 ms
45,600 KB
testcase_21 AC 170 ms
45,488 KB
testcase_22 AC 176 ms
45,788 KB
testcase_23 AC 98 ms
39,544 KB
testcase_24 AC 97 ms
39,760 KB
testcase_25 AC 192 ms
47,492 KB
testcase_26 AC 131 ms
43,152 KB
testcase_27 AC 69 ms
38,300 KB
testcase_28 AC 175 ms
45,624 KB
testcase_29 AC 140 ms
43,288 KB
testcase_30 AC 55 ms
37,268 KB
testcase_31 AC 141 ms
43,744 KB
testcase_32 AC 168 ms
46,260 KB
testcase_33 AC 198 ms
47,244 KB
testcase_34 AC 61 ms
37,388 KB
testcase_35 AC 159 ms
45,424 KB
testcase_36 AC 103 ms
39,528 KB
testcase_37 AC 210 ms
46,464 KB
testcase_38 AC 186 ms
45,792 KB
testcase_39 AC 159 ms
45,760 KB
testcase_40 AC 149 ms
43,940 KB
testcase_41 AC 149 ms
45,836 KB
testcase_42 AC 79 ms
38,376 KB
testcase_43 AC 111 ms
39,872 KB
testcase_44 AC 194 ms
46,744 KB
testcase_45 AC 142 ms
43,984 KB
testcase_46 AC 173 ms
45,744 KB
testcase_47 AC 116 ms
40,548 KB
testcase_48 AC 156 ms
45,432 KB
testcase_49 AC 153 ms
45,832 KB
testcase_50 AC 64 ms
37,360 KB
testcase_51 AC 94 ms
39,452 KB
testcase_52 AC 196 ms
46,848 KB
testcase_53 AC 158 ms
46,204 KB
testcase_54 AC 183 ms
46,404 KB
testcase_55 AC 199 ms
46,880 KB
testcase_56 AC 199 ms
47,468 KB
testcase_57 AC 58 ms
37,584 KB
testcase_58 AC 135 ms
42,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
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];
        List<List<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n + 2; i++) {
            graph.add(new ArrayList<>());
        }
        List<Set<Integer>> nextTo = new ArrayList<>();
        for (int i = 0; i < n + 2; i++) {
            values[i] = sc.nextInt();
            nextTo.add(new HashSet<>());
            for (int j = 0; j <= 30; j++) {
                nextTo.get(i).add(values[i] ^ (1 << j));
            }
            for (int j = 0; j < i; j++) {
                if (nextTo.get(j).contains(values[i])) {
                    graph.get(i).add(j);
                    graph.get(j).add(i);
                }
            }
        }
        Deque<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));
            }
        }
        System.out.println(costs[1] == Integer.MAX_VALUE ? -1 : costs[1] - 1);
    }
    
    static class Path {
        int idx;
        int value;
        
        public Path(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
        
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0