結果

問題 No.1576 織姫と彦星
ユーザー Kohei
提出日時 2026-10-17 22:58:11
言語 PyPy3
(8.0.0 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 143 ms / 2,000 ms
+ 313µs
コード長 724 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 89 ms
コンパイル使用メモリ 64,860 KB
実行使用メモリ 59,196 KB
最終ジャッジ日時 2026-10-17 22:58:20
合計ジャッジ時間 7,766 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 54
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque
N = int(input())
S, T = map(int, input().split())
stones = [S] + list(map(int, input().split())) + [T]
G = [list() for _ in range(N + 2)]

for i in range(N + 2):
    x = stones[i]
    for j in range(i + 1, N + 2):
        y = stones[j]
        cnt = 0
        for k in range(30):
            if (x>>k)&1 != (y>>k)&1:
                cnt += 1
        if cnt <= 1:
            G[i].append(j)
            G[j].append(i)

dist = [-1]*(N + 2)
dist[0] = 0
que = deque()
que.append(0)
while que:
    pos = que.popleft()
    for nex in G[pos]:
        if dist[nex] == -1:
            dist[nex] = dist[pos] + 1
            que.append(nex)

if dist[-1] == -1:
    print(-1)
else:
    print(dist[-1] - 1)
0