結果

問題 No.416 旅行会社
ユーザー simansiman
提出日時 2022-02-18 02:28:58
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,599 ms / 4,000 ms
コード長 742 bytes
コンパイル時間 311 ms
コンパイル使用メモリ 11,188 KB
実行使用メモリ 77,212 KB
最終ジャッジ日時 2023-08-21 10:49:30
合計ジャッジ時間 17,879 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 765 ms
59,744 KB
testcase_01 AC 78 ms
15,324 KB
testcase_02 AC 80 ms
15,328 KB
testcase_03 AC 79 ms
15,168 KB
testcase_04 AC 79 ms
15,252 KB
testcase_05 AC 80 ms
14,988 KB
testcase_06 AC 81 ms
15,092 KB
testcase_07 AC 82 ms
15,096 KB
testcase_08 AC 93 ms
15,628 KB
testcase_09 AC 149 ms
19,084 KB
testcase_10 AC 849 ms
59,692 KB
testcase_11 AC 858 ms
59,920 KB
testcase_12 AC 865 ms
60,112 KB
testcase_13 AC 779 ms
59,732 KB
testcase_14 AC 1,520 ms
77,212 KB
testcase_15 AC 1,521 ms
75,964 KB
testcase_16 AC 1,586 ms
76,596 KB
testcase_17 AC 1,599 ms
76,352 KB
testcase_18 AC 1,598 ms
76,164 KB
testcase_19 AC 981 ms
67,208 KB
testcase_20 AC 980 ms
67,720 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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] || Q + 1) + 1; t == Q + 1 ? -1 : t }
0