結果

問題 No.317 辺の追加
ユーザー LeonardoneLeonardone
提出日時 2015-12-10 22:36:40
言語 Ruby
(3.3.0)
結果
TLE  
実行時間 -
コード長 1,347 bytes
コンパイル時間 99 ms
コンパイル使用メモリ 11,564 KB
実行使用メモリ 22,884 KB
最終ジャッジ日時 2023-10-13 10:40:21
合計ジャッジ時間 4,250 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#! ruby
# yukicoder My Practice
# author: Leonardone @ NEETSDKASU

def gi(); gets.to_i; end
def gis(); gets.chomp.split.map(&:to_i); end

n, m = gis


# 連結成分を検出するためラベリング(?)

grp = [0] * (n + 1)
grp_c = 0
grp_r = []
grp_n = [0]

m.times do
    u, v = gis
    next if u == v
    gu, gv = grp[u], grp[v]
    if gu == 0
        if gv == 0
            grp[u] = grp[v] = grp_c += 1
            grp_r << {grp_c => 1}
            grp_n << 2
        else
            grp[u] = gv
            grp_n[gv]+= 1
        end
    elsif gv == 0
        grp[v] = gu
        grp_n[gu] += 1
    elsif gu != gv
        ui, vi = [gu,gv].map{|k| grp_r.index{|o| o.key? k} }.minmax
        grp_r[ui].merge! grp_r[vi]
        grp_r.delete_at vi
    end
end

grp_f = grp_r.inject( [nil]*(grp_c + 1) ){|r, a| k = a.keys; k.each{|x| r[x] = k}; r }

dp = [0] * (n + n)

dp[0] = 1
dp[1] = 2 if grp[1..n].include? 0

(1..grp_c).each do |i|
    c = 1
    if grp_f[i].nil?
        c = grp_n[i] 
    else
        c = grp_f[i].inject(0){|r, j| r + grp_n[j]}
        grp_f[i].clear
    end
    (n-1).downto(0) do |j|
        next if dp[j] == 0
        if dp[j + c] == 0
            dp[j + c] = dp[j] + 1
        elsif dp[j] + 1 < dp[j + c]
            dp[j + c] = dp[j] + 1
        end
    end
end

puts dp[1..n].map{|v| v > 0 ? v - 2 : -1} * $/

0