結果

問題 No.1577 織姫と彦星2
ユーザー yuruhiya
提出日時 2021-06-29 20:55:28
言語 Crystal
(1.14.0)
結果
WA  
実行時間 -
コード長 545 bytes
コンパイル時間 11,898 ms
コンパイル使用メモリ 296,244 KB
実行使用メモリ 9,812 KB
最終ジャッジ日時 2024-06-25 23:18:25
合計ジャッジ時間 15,152 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 21 WA * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

n = read_line.to_i
s, t = read_line.split.map(&.to_i)
a = read_line.split.map(&.to_i)
stones = a + [s, t]
hash = stones.zip(0..).to_h
g = (1..n + 2).map { [] of Int32 }
stones.each_with_index do |x, i|
  (0..30).each do |bit|
    if j = hash[x ^ (1 << bit)]?
      g[i] << j; g[j] << i
    end
  end
end

que = Deque{hash[s]}
dist = [nil.as(Int32?)] * (n + 2)
dist[hash[s]] = 0
while v = que.pop?
	d = dist[v].not_nil!
	puts(d - 1) + exit if v == hash[t]
	g[v].each do |u|
		if dist[u].nil?
			dist[u] = d + 1
			que << u
		end
	end
end
puts -1
0