結果
| 問題 |
No.241 出席番号(1)
|
| ユーザー |
|
| 提出日時 | 2019-02-01 00:32:05 |
| 言語 | Nim (2.2.0) |
| 結果 |
WA
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 1,462 bytes |
| コンパイル時間 | 2,590 ms |
| コンパイル使用メモリ | 63,848 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-07-01 11:15:23 |
| 合計ジャッジ時間 | 4,091 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 WA * 1 |
| other | AC * 27 WA * 2 |
ソースコード
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:BipartiteMatch) : tuple[match:seq[int],size:int] =
var match = newSeqWith(B.len,-1)
var used : seq[bool]
proc dfs(src:int) : bool =
# 交互にペアを結んでいく
used[src] = true
for dst in B[src]:
if match[dst] >= 0 :
if used[match[dst]] : continue
if not 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 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