結果

問題 No.241 出席番号(1)
ユーザー むらためむらため
提出日時 2019-02-05 06:24:34
言語 Nim
(2.0.2)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,491 bytes
コンパイル時間 2,462 ms
コンパイル使用メモリ 70,240 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-14 03:29:46
合計ジャッジ時間 4,147 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 WA -
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 WA -
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,384 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 WA -
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils

# 二部グラフの最大マッチング
template useBiparticeMatching() =
  # 二部グラフの最大マッチング O(E)
  type BipartiteMatch = seq[seq[int]]
  proc initBipartiteMatch(maxSize:int): BipartiteMatch = newSeqWith(maxSize,newSeq[int]())
  proc add(B:var BipartiteMatch,src,dst:int) = (B[dst] &= src;B[src] &= dst)
  proc bipartiteMatching(B:var BipartiteMatch) : tuple[match:seq[int],size:int] =
    var match = newSeqWith(B.len,-1)
    var used : seq[bool]
    proc dfs(B:var BipartiteMatch,src:int) : bool =
      # 交互にペアを結んでいく
      used[src] = true
      for dst in B[src]:
        if match[dst] >= 0 :
          if used[match[dst]] : continue
          if not B.dfs(match[dst]) : continue
        match[src] = dst
        match[dst] = src
        return true
      return false
    var size = 0
    for src in 0..<B.len:
      if match[src] >= 0 : continue
      used = newSeq[bool](B.len)
      if B.dfs(src) : size += 1
    return (match,size)
useBiparticeMatching()

proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
proc scan(): int =
  while true:
    let k = getchar_unlocked()
    if k < '0': return
    result = 10 * result + k.ord - '0'.ord

let n = scan()
var BM = initBipartiteMatch(2*n)
for i in 0..<n:
  let bad = scan()
  for j in 0..<n:
    if j == bad : continue
    BM.add(i,n+j)
let (match,size) = BM.bipartiteMatching()
if size != n : quit "-1",0
for i in 0..<n:  echo match[i]-n
0