結果

問題 No.1576 織姫と彦星
ユーザー tobusakanatobusakana
提出日時 2022-10-29 17:42:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 170 ms / 2,000 ms
コード長 804 bytes
コンパイル時間 328 ms
コンパイル使用メモリ 86,904 KB
実行使用メモリ 80,016 KB
最終ジャッジ日時 2023-09-20 20:35:20
合計ジャッジ時間 11,006 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,820 KB
testcase_01 AC 93 ms
71,484 KB
testcase_02 AC 93 ms
71,580 KB
testcase_03 AC 96 ms
71,784 KB
testcase_04 AC 93 ms
71,648 KB
testcase_05 AC 92 ms
71,440 KB
testcase_06 AC 99 ms
76,284 KB
testcase_07 AC 123 ms
77,508 KB
testcase_08 AC 120 ms
77,796 KB
testcase_09 AC 106 ms
77,784 KB
testcase_10 AC 154 ms
78,248 KB
testcase_11 AC 109 ms
77,324 KB
testcase_12 AC 142 ms
79,280 KB
testcase_13 AC 160 ms
79,116 KB
testcase_14 AC 144 ms
77,560 KB
testcase_15 AC 145 ms
77,564 KB
testcase_16 AC 152 ms
78,400 KB
testcase_17 AC 144 ms
77,580 KB
testcase_18 AC 116 ms
77,516 KB
testcase_19 AC 119 ms
77,828 KB
testcase_20 AC 142 ms
77,652 KB
testcase_21 AC 142 ms
78,376 KB
testcase_22 AC 149 ms
77,960 KB
testcase_23 AC 114 ms
77,472 KB
testcase_24 AC 114 ms
77,504 KB
testcase_25 AC 151 ms
77,636 KB
testcase_26 AC 124 ms
78,124 KB
testcase_27 AC 109 ms
76,936 KB
testcase_28 AC 146 ms
77,384 KB
testcase_29 AC 121 ms
78,216 KB
testcase_30 AC 100 ms
76,396 KB
testcase_31 AC 122 ms
77,336 KB
testcase_32 AC 140 ms
77,428 KB
testcase_33 AC 166 ms
77,768 KB
testcase_34 AC 109 ms
77,412 KB
testcase_35 AC 134 ms
77,592 KB
testcase_36 AC 109 ms
76,976 KB
testcase_37 AC 168 ms
77,764 KB
testcase_38 AC 149 ms
77,528 KB
testcase_39 AC 132 ms
77,856 KB
testcase_40 AC 129 ms
78,116 KB
testcase_41 AC 133 ms
78,300 KB
testcase_42 AC 113 ms
79,432 KB
testcase_43 AC 114 ms
77,536 KB
testcase_44 AC 164 ms
79,324 KB
testcase_45 AC 124 ms
77,660 KB
testcase_46 AC 148 ms
78,524 KB
testcase_47 AC 116 ms
78,100 KB
testcase_48 AC 136 ms
77,696 KB
testcase_49 AC 130 ms
77,928 KB
testcase_50 AC 108 ms
78,012 KB
testcase_51 AC 108 ms
77,276 KB
testcase_52 AC 161 ms
77,868 KB
testcase_53 AC 146 ms
77,644 KB
testcase_54 AC 152 ms
77,772 KB
testcase_55 AC 160 ms
77,624 KB
testcase_56 AC 170 ms
77,524 KB
testcase_57 AC 106 ms
77,120 KB
testcase_58 AC 123 ms
80,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline = sys.stdin.readline

N = int(readline())
s,g = map(int,readline().split())

A = [s] + list(map(int,readline().split())) + [g]
N += 2

def can_connect(X, Y):
  diff = 0
  while X or Y:
    if (X & 1) ^ (Y & 1):
      diff += 1
      if diff == 2:
        return False
    X >>= 1
    Y >>= 1
  return True
  
G = [[] for i in range(N)]
for i in range(N - 1):
  for j in range(i + 1, N):
    if can_connect(A[i], A[j]):
      G[i].append(j)
      G[j].append(i)

visited = [False] * N
from collections import deque
q = deque([])
q.append([0, 0])
while q:
  v, cost = q.popleft()
  if visited[v]:
    continue
  visited[v] = True
  if v == N - 1:
    print(cost - 1)
    break
  for child in G[v]:
    if visited[child]:
      continue
    q.append([child, cost + 1])
else:
  print(-1)
0