結果

問題 No.241 出席番号(1)
ユーザー hytkxd
提出日時 2024-05-08 00:37:06
言語 Ruby
(3.4.1)
結果
AC  
実行時間 98 ms / 2,000 ms
コード長 1,351 bytes
コンパイル時間 230 ms
コンパイル使用メモリ 7,168 KB
実行使用メモリ 12,288 KB
最終ジャッジ日時 2024-11-30 21:55:08
合計ジャッジ時間 4,403 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:52: warning: assigned but unused variable - r
Syntax OK

ソースコード

diff #

#!/usr/local/bin/ruby
class BipartiteMatching
  def initialize(n,adj)
    @n,@adj = n,adj # adj is already satisfied with bipartite property.
  end
  private
  def dfs(v)
    rop = false
    @visited[v] = true
    @adj[v].each do |u|
      w = @match[u]
      if w.nil? || (@visited[w].nil? && dfs(w))
        rop,@match[v],@match[u] = true,u,v
        break
      end
    end
    rop
  end
  public
  def matching
    #kuhn algorithm:
    # Start by unsaturated vertex, find an augmenting path, update the matching.
    r = 0
    @match = Array.new(@n)
    (0...@n).each do |i|
      unless @match[i]
        @visited = Array.new(@n)
        if dfs(i)
          r+=1
        end
      end
    end
    [r,@match]
  end
end
class StudentNumber
  def initialize(*arg)
    @n,@a, = arg
    @adj = Array.new(2*@n){Array.new}
    (0...@n).each do |i|
      (0...[@a[i],@n].min).each do |j|
        @adj[i].push(@n+j)
        @adj[@n+j].push(i)
      end
      ([0,@a[i]+1].max...@n).each do |j|
        @adj[i].push(@n+j)
        @adj[@n+j].push(i)
      end
    end
  end
  def ans
    r,marr = BipartiteMatching.new(2*@n,@adj).matching
    if marr.any?(nil)
      [-1]
    else
      marr[0,@n].map{_1-@n}
    end
  end
end
### END: class StudentNumber
iod = STDIN
n = iod.gets.to_i
a = Array.new(n){iod.gets.to_i}
puts StudentNumber.new(n,a).ans
exit 0
0