結果

問題 No.1577 織姫と彦星2
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-07-02 08:40:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 344 ms / 2,000 ms
コード長 1,453 bytes
コンパイル時間 1,367 ms
コンパイル使用メモリ 86,844 KB
実行使用メモリ 98,128 KB
最終ジャッジ日時 2023-09-11 04:38:40
合計ジャッジ時間 15,338 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 105 ms
71,316 KB
testcase_01 AC 102 ms
71,320 KB
testcase_02 AC 102 ms
71,444 KB
testcase_03 AC 105 ms
71,484 KB
testcase_04 AC 103 ms
71,512 KB
testcase_05 AC 137 ms
81,636 KB
testcase_06 AC 114 ms
78,336 KB
testcase_07 AC 271 ms
92,668 KB
testcase_08 AC 172 ms
83,692 KB
testcase_09 AC 344 ms
98,128 KB
testcase_10 AC 153 ms
82,656 KB
testcase_11 AC 134 ms
79,272 KB
testcase_12 AC 207 ms
87,236 KB
testcase_13 AC 143 ms
81,984 KB
testcase_14 AC 214 ms
89,500 KB
testcase_15 AC 157 ms
81,708 KB
testcase_16 AC 255 ms
91,768 KB
testcase_17 AC 193 ms
85,468 KB
testcase_18 AC 113 ms
77,236 KB
testcase_19 AC 240 ms
89,952 KB
testcase_20 AC 217 ms
88,004 KB
testcase_21 AC 258 ms
91,660 KB
testcase_22 AC 246 ms
90,716 KB
testcase_23 AC 173 ms
83,488 KB
testcase_24 AC 263 ms
96,244 KB
testcase_25 AC 156 ms
80,740 KB
testcase_26 AC 167 ms
82,620 KB
testcase_27 AC 201 ms
93,152 KB
testcase_28 AC 217 ms
88,140 KB
testcase_29 AC 163 ms
85,584 KB
testcase_30 AC 184 ms
86,104 KB
testcase_31 AC 239 ms
91,300 KB
testcase_32 AC 119 ms
78,136 KB
testcase_33 AC 117 ms
77,616 KB
testcase_34 AC 172 ms
84,620 KB
testcase_35 AC 242 ms
89,644 KB
testcase_36 AC 243 ms
90,520 KB
testcase_37 AC 309 ms
97,656 KB
testcase_38 AC 292 ms
96,516 KB
testcase_39 AC 181 ms
84,040 KB
testcase_40 AC 179 ms
83,636 KB
testcase_41 AC 232 ms
94,172 KB
testcase_42 AC 175 ms
87,932 KB
testcase_43 AC 180 ms
88,048 KB
testcase_44 AC 121 ms
78,440 KB
testcase_45 AC 151 ms
80,772 KB
testcase_46 AC 139 ms
81,140 KB
testcase_47 AC 188 ms
94,416 KB
testcase_48 AC 153 ms
83,512 KB
testcase_49 AC 182 ms
85,328 KB
testcase_50 AC 159 ms
82,088 KB
testcase_51 AC 122 ms
78,264 KB
testcase_52 AC 247 ms
96,260 KB
testcase_53 AC 262 ms
96,036 KB
testcase_54 AC 167 ms
85,744 KB
testcase_55 AC 245 ms
96,292 KB
testcase_56 AC 165 ms
81,724 KB
testcase_57 AC 145 ms
80,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main0(n,s,e,a):
  a.append(s)
  a.append(e)
  g=[[] for _ in range(n+2)]
  for i in range(n+1):
    for j in range(i+1,n+2):
      x=a[i]^a[j]
      cnt=bin(x).count('1')
      if cnt<=1:
        g[i].append(j)
        g[j].append(i)
  from collections import deque
  todo=deque([n])
  seen=[-1]*(n+2)
  seen[n]=0
  while todo:
    v=todo.popleft()
    for nv in g[v]:
      if seen[nv]==-1:
        seen[nv]=seen[v]+1
        todo.append(nv)
  if seen[n+1]==-1:
    return -1
  else:
    return seen[n+1]-1
def main1(n,s,e,a):
  # a[i]<=10^9~2^30
  # 各iについてハミルトン距離が1のものを列挙→連想配列{数字:idx}の中から当てはまるものを探す
  a.append(s)
  a.append(e)
  g=[set() for _ in range(n+2)]
  d={}
  for i,x in enumerate(a):
    if x in d:
      d[x].append(i)
    else:
      d[x]=[i]
  for i,x in enumerate(a):
    for j in range(30):
      if x^(1<<j) in d:
        for k in d[x^(1<<j)]:
          g[i].add(k)
          g[k].add(i)
  from collections import deque
  todo=deque([n])
  seen=[-1]*(n+2)
  seen[n]=0
  while todo:
    v=todo.popleft()
    for nv in g[v]:
      if seen[nv]==-1:
        seen[nv]=seen[v]+1
        todo.append(nv)
  if seen[n+1]==-1:
    return -1
  else:
    return seen[n+1]-1

if __name__=='__main__':
  n=int(input())
  s,e=map(int,input().split())
  a=list(map(int,input().split()))
  #ret0=main0(n,s,e,a[:])
  ret1=main1(n,s,e,a[:])
  #print(ret0)
  print(ret1)

0