結果

問題 No.1194 Replace
ユーザー simansiman
提出日時 2023-06-11 01:28:57
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,346 ms / 2,000 ms
コード長 591 bytes
コンパイル時間 548 ms
コンパイル使用メモリ 11,220 KB
実行使用メモリ 99,108 KB
最終ジャッジ日時 2023-08-31 01:39:51
合計ジャッジ時間 24,647 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 769 ms
63,304 KB
testcase_01 AC 900 ms
66,648 KB
testcase_02 AC 701 ms
58,516 KB
testcase_03 AC 559 ms
54,880 KB
testcase_04 AC 846 ms
66,764 KB
testcase_05 AC 787 ms
62,128 KB
testcase_06 AC 706 ms
60,128 KB
testcase_07 AC 1,196 ms
97,792 KB
testcase_08 AC 1,224 ms
97,516 KB
testcase_09 AC 1,208 ms
97,436 KB
testcase_10 AC 1,346 ms
97,804 KB
testcase_11 AC 1,195 ms
97,624 KB
testcase_12 AC 1,151 ms
99,108 KB
testcase_13 AC 635 ms
41,428 KB
testcase_14 AC 540 ms
37,352 KB
testcase_15 AC 485 ms
38,924 KB
testcase_16 AC 622 ms
41,376 KB
testcase_17 AC 455 ms
37,120 KB
testcase_18 AC 461 ms
35,296 KB
testcase_19 AC 640 ms
49,144 KB
testcase_20 AC 68 ms
15,200 KB
testcase_21 AC 68 ms
15,192 KB
testcase_22 AC 69 ms
15,140 KB
testcase_23 AC 243 ms
25,956 KB
testcase_24 AC 128 ms
19,992 KB
testcase_25 AC 93 ms
16,028 KB
testcase_26 AC 271 ms
24,936 KB
testcase_27 AC 106 ms
16,956 KB
testcase_28 AC 164 ms
19,524 KB
testcase_29 AC 80 ms
16,416 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N, M = gets.split.map(&:to_i)
nums = []
E = Hash.new { |h, k| h[k] = [] }

M.times do
  b, c = gets.split.map(&:to_i)

  nums << b
  nums << c
  E[c] << b
end

nums.uniq!
nums.sort!
visited = Hash.new(false)
memo = Hash.new

nums.reverse.each do |root|
  next if visited[root]

  queue = []
  queue << root
  until queue.empty?
    v = queue.shift

    next if visited[v]
    visited[v] = true
    memo[v] = root

    E[v].each do |u|
      next if u > root

      queue << u
    end
  end
end

sum = N * (1 + N) / 2

nums.each do |n|
  if memo[n]
    sum += memo[n] - n
  end
end

puts sum
0