結果

問題 No.317 辺の追加
ユーザー 小指が強い人小指が強い人
提出日時 2015-12-11 21:47:12
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 1,106 bytes
コンパイル時間 159 ms
コンパイル使用メモリ 11,228 KB
実行使用メモリ 40,444 KB
最終ジャッジ日時 2023-10-13 11:25:54
合計ジャッジ時間 18,383 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
15,308 KB
testcase_01 AC 80 ms
15,268 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 WA -
testcase_06 RE -
testcase_07 WA -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 390 ms
25,408 KB
testcase_25 AC 296 ms
21,708 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 97 ms
15,792 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

# 辺の追加
def solve(i, u, k)
    return if i >= $r.length
    ur = u + $r[i]
    return if $memo[ur - 1] >= 0
    $memo[ur - 1] = k
    solve(i + 1, u, k)
    solve(i + 1, ur, k + 1)
end
$n, m = gets.split.map(&:to_i)
#n = 2 * 10 ** 5
#m = 2 * 10 ** 5
a = Hash.new{|h, k| h[k] = Array.new}
b = Array.new($n, false)
#n.times do |i|
#    a[2 * i + 1].push(2 * i + 2)
#    a[2 * i + 2].push(2 * i + 1)
#end
m.times do |i|
    s = gets.split.map(&:to_i)
    next if s[0] == s[1]
    a[s[0]].push(s[1])
    a[s[1]].push(s[0])
end
$r = Array.new
$memo = Array.new($n, -1)
stack = Array.new(m + 10)
$n.times do |i|
    i += 1
    next if b[i]
    top = 1
    bottom = 0
    stack[0] = i
    count = 1
    while bottom < top do
        j = stack[top - 1]
        if !a.has_key?(j)
            top -= 1
            next
        end
        b[j] = true
        a[j].each do |x|
            next if b[x]
            b[x] = true
            stack[top] = x
            top += 1
            count += 1
        end
        bottom += 1
    end
    $r.push(count)
end
$r.sort!{|a, b| b <=> a}
solve(0, 0, 0)
puts $memo
0