結果

問題 No.1577 織姫と彦星2
ユーザー kept1994kept1994
提出日時 2022-03-19 16:16:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 251 ms / 2,000 ms
コード長 1,349 bytes
コンパイル時間 257 ms
コンパイル使用メモリ 82,140 KB
実行使用メモリ 97,468 KB
最終ジャッジ日時 2024-04-15 05:42:30
合計ジャッジ時間 10,473 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
67,600 KB
testcase_01 AC 65 ms
67,016 KB
testcase_02 AC 66 ms
68,020 KB
testcase_03 AC 66 ms
67,476 KB
testcase_04 AC 66 ms
67,952 KB
testcase_05 AC 95 ms
80,332 KB
testcase_06 AC 79 ms
73,796 KB
testcase_07 AC 208 ms
88,736 KB
testcase_08 AC 126 ms
81,436 KB
testcase_09 AC 251 ms
97,468 KB
testcase_10 AC 115 ms
81,916 KB
testcase_11 AC 95 ms
79,120 KB
testcase_12 AC 149 ms
85,408 KB
testcase_13 AC 106 ms
80,800 KB
testcase_14 AC 166 ms
87,036 KB
testcase_15 AC 117 ms
80,172 KB
testcase_16 AC 195 ms
88,376 KB
testcase_17 AC 140 ms
83,272 KB
testcase_18 AC 71 ms
69,380 KB
testcase_19 AC 179 ms
86,940 KB
testcase_20 AC 162 ms
85,616 KB
testcase_21 AC 194 ms
88,460 KB
testcase_22 AC 187 ms
88,708 KB
testcase_23 AC 125 ms
80,788 KB
testcase_24 AC 198 ms
92,660 KB
testcase_25 AC 116 ms
79,972 KB
testcase_26 AC 122 ms
80,848 KB
testcase_27 AC 166 ms
89,968 KB
testcase_28 AC 160 ms
86,284 KB
testcase_29 AC 129 ms
84,260 KB
testcase_30 AC 140 ms
84,284 KB
testcase_31 AC 190 ms
88,828 KB
testcase_32 AC 81 ms
75,032 KB
testcase_33 AC 82 ms
72,132 KB
testcase_34 AC 125 ms
83,180 KB
testcase_35 AC 177 ms
87,160 KB
testcase_36 AC 188 ms
88,856 KB
testcase_37 AC 240 ms
96,920 KB
testcase_38 AC 231 ms
97,380 KB
testcase_39 AC 127 ms
81,340 KB
testcase_40 AC 127 ms
82,476 KB
testcase_41 AC 178 ms
91,024 KB
testcase_42 AC 143 ms
86,064 KB
testcase_43 AC 143 ms
86,348 KB
testcase_44 AC 80 ms
74,292 KB
testcase_45 AC 108 ms
79,500 KB
testcase_46 AC 104 ms
80,172 KB
testcase_47 AC 150 ms
91,212 KB
testcase_48 AC 117 ms
82,824 KB
testcase_49 AC 137 ms
83,308 KB
testcase_50 AC 119 ms
80,792 KB
testcase_51 AC 81 ms
75,604 KB
testcase_52 AC 193 ms
93,276 KB
testcase_53 AC 195 ms
93,092 KB
testcase_54 AC 128 ms
84,336 KB
testcase_55 AC 190 ms
92,952 KB
testcase_56 AC 116 ms
80,448 KB
testcase_57 AC 105 ms
79,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3

from bz2 import compress


def main():
    import math
    from collections import deque
    INF = 10 ** 16
    def bfs(edges: "List[to]", start_node: int) -> list:
        q = deque()
        dist = [INF] * len(edges)
        q.append(start_node)
        dist[start_node] = 0
        while q:
            now = q.popleft()
            for next in edges[now]:
                if dist[next] != INF:
                    continue
                q.append(next)
                dist[next] = dist[now] + 1
        return dist
    N = int(input())
    st, ed = map(int, input().split())
    stones = [st] + list(map(int, input().split())) + [ed]
    compressed = {}
    compressed_to_row = []
    routes = set(stones)
    for index, val in enumerate(sorted(list(routes))):
        compressed[val] = index
        compressed_to_row.append(val)
    # print(compressed)
    G = [[] for _ in range(N + 2)]
    for i in range(N + 2):
        for b in range(30):
            reachable = stones[i] ^ 1 << b
            if reachable in compressed:
                G[compressed[stones[i]]].append(compressed[reachable])
                G[compressed[reachable]].append(compressed[stones[i]])
    d = bfs(G, compressed[st])
    print(d[compressed[ed]] - 1 if d[compressed[ed]] != INF else -1)
    return

if __name__ == '__main__':
    main()
0