結果

問題 No.416 旅行会社
ユーザー simansiman
提出日時 2022-02-18 02:27:24
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 731 bytes
コンパイル時間 468 ms
コンパイル使用メモリ 11,196 KB
実行使用メモリ 77,256 KB
最終ジャッジ日時 2023-09-11 17:58:44
合計ジャッジ時間 20,948 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 829 ms
59,484 KB
testcase_01 AC 82 ms
15,280 KB
testcase_02 AC 82 ms
15,160 KB
testcase_03 AC 84 ms
15,340 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 AC 908 ms
59,724 KB
testcase_11 AC 930 ms
59,568 KB
testcase_12 AC 916 ms
59,928 KB
testcase_13 AC 830 ms
59,528 KB
testcase_14 AC 1,737 ms
77,256 KB
testcase_15 AC 1,757 ms
76,332 KB
testcase_16 AC 1,777 ms
76,680 KB
testcase_17 AC 1,807 ms
76,480 KB
testcase_18 AC 1,786 ms
76,256 KB
testcase_19 RE -
testcase_20 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N, M, Q = gets.split.map(&:to_i)
E = Hash.new { |h, k| h[k] = Hash.new(false) }
C = M.times.map { gets.split.map(&:to_i) }
D = Q.times.map { gets.split.map(&:to_i) }

C.each do |a, b|
  E[a][b] = E[b][a] = true
end

D.each do |a, b|
  E[a][b] = E[b][a] = false
end

V = Array.new(N + 1, nil)

def bfs(root, t)
  queue = []
  queue << root

  until queue.empty?
    v = queue.shift

    next if V[v]
    V[v] = t

    E[v].each do |u, s|
      next if not s

      queue << u
    end
  end
end

bfs(1, 0)

D.reverse_each.with_index(1) do |(a, b), t|
  E[a][b] = E[b][a] = true

  if V[a] && V[b].nil?
    bfs(b, t)
  end

  if V[b] && V[a].nil?
    bfs(a, t)
  end
end

puts (2..N).map { |x| t = Q - V[x] + 1; t == Q + 1 ? -1 : t }
0