#! 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} * $/