結果

問題 No.1479 Matrix Eraser
ユーザー yagisumiyagisumi
提出日時 2021-06-17 16:26:19
言語 TypeScript
(5.4.3)
結果
AC  
実行時間 1,173 ms / 3,000 ms
コード長 3,880 bytes
コンパイル時間 9,539 ms
コンパイル使用メモリ 256,792 KB
実行使用メモリ 175,388 KB
最終ジャッジ日時 2023-08-30 23:42:16
合計ジャッジ時間 41,946 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 215 ms
92,004 KB
testcase_01 AC 256 ms
94,520 KB
testcase_02 AC 260 ms
94,952 KB
testcase_03 AC 260 ms
94,944 KB
testcase_04 AC 260 ms
94,908 KB
testcase_05 AC 260 ms
94,876 KB
testcase_06 AC 261 ms
95,020 KB
testcase_07 AC 488 ms
122,948 KB
testcase_08 AC 582 ms
132,760 KB
testcase_09 AC 793 ms
135,920 KB
testcase_10 AC 1,013 ms
159,136 KB
testcase_11 AC 849 ms
151,248 KB
testcase_12 AC 522 ms
126,436 KB
testcase_13 AC 585 ms
130,256 KB
testcase_14 AC 533 ms
125,612 KB
testcase_15 AC 343 ms
103,508 KB
testcase_16 AC 552 ms
128,504 KB
testcase_17 AC 1,147 ms
167,232 KB
testcase_18 AC 1,149 ms
167,128 KB
testcase_19 AC 1,146 ms
167,288 KB
testcase_20 AC 1,172 ms
167,152 KB
testcase_21 AC 1,151 ms
167,372 KB
testcase_22 AC 1,140 ms
167,636 KB
testcase_23 AC 1,141 ms
167,152 KB
testcase_24 AC 1,132 ms
167,096 KB
testcase_25 AC 1,139 ms
167,576 KB
testcase_26 AC 1,173 ms
167,264 KB
testcase_27 AC 631 ms
148,780 KB
testcase_28 AC 639 ms
148,092 KB
testcase_29 AC 639 ms
148,236 KB
testcase_30 AC 622 ms
148,092 KB
testcase_31 AC 625 ms
147,748 KB
testcase_32 AC 662 ms
144,620 KB
testcase_33 AC 649 ms
144,424 KB
testcase_34 AC 637 ms
144,600 KB
testcase_35 AC 662 ms
144,552 KB
testcase_36 AC 662 ms
144,640 KB
testcase_37 AC 372 ms
140,188 KB
testcase_38 AC 671 ms
147,332 KB
testcase_39 AC 1,062 ms
175,388 KB
testcase_40 AC 214 ms
92,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import * as fs from 'fs'

// import * as readline from 'readline'
// const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
// const ask = (query: string) => new Promise<string>((resolve) => rl.question(query, resolve))
// // Don't forget `rl.close()`.

const INT = Math.floor

declare global {
  interface Array<T> {
    last(): T | undefined
    isEmpty(): boolean
  }
}
Array.prototype.last = function () {
  return this.length === 0 ? undefined : this[this.length - 1]
}
Array.prototype.isEmpty = function () {
  return this.length === 0
}

const less = <T>(a: T, b: T) => (a == b ? 0 : a < b ? -1 : 1)
const greater = <T>(a: T, b: T) => (a == b ? 0 : a < b ? 1 : -1)
const bigIntMax = (...args: bigint[]) => args.reduce((m, e) => (e > m ? e : m))
const bigIntMin = (...args: bigint[]) => args.reduce((m, e) => (e < m ? e : m))
const bigIntAbs = (arg: bigint) => (arg < 0 ? -arg : arg)

declare const stdin: number
function read_stdin() {
  return fs.readFileSync(process.env.NODE_ENV === 'debug' ? stdin : process.stdin.fd, 'utf8')
}
class Input {
  readonly inputs: string[]
  private index = 0
  constructor(str?: string) {
    this.inputs = (str ? str : read_stdin()).split(/\s+/)
  }
  number() {
    return Number(this.inputs[this.index++])
  }
  numbers(n: number) {
    return this.inputs.slice(this.index, (this.index += n)).map(Number)
  }
  bigint() {
    return BigInt(this.inputs[this.index++])
  }
  bigints(n: number) {
    return this.inputs.slice(this.index, (this.index += n)).map(BigInt)
  }
  word() {
    return this.inputs[this.index++]
  }
  words(n: number) {
    return this.inputs.slice(this.index, (this.index += n))
  }
}

function array<T>(len: number, init: T): T[] {
  return Array(len).fill(init)
}

function array2<T>(h: number, w: number, init: T): T[][] {
  return array(h, 0).map(() => array(w, init))
}

class BipartiteMatching {
  g: number[][]
  r: number[]
  c: number[]
  visited: boolean[]
  constructor(h: number, w: number) {
    this.g = [...Array(h)].map(() => [])
    this.r = Array(h).fill(-1)
    this.c = Array(w).fill(-1)
    this.visited = Array(h)
  }
  add(i: number, j: number) {
    this.g[i].push(j)
  }
  dfs(i: number) {
    if (this.visited[i]) return false
    this.visited[i] = true
    for (const j of this.g[i]) {
      if (this.c[j] === -1) {
        this.r[i] = j
        this.c[j] = i
        return true
      }
    }
    for (const j of this.g[i]) {
      if (this.dfs(this.c[j])) {
        this.r[i] = j
        this.c[j] = i
        return true
      }
    }
    return false
  }
  run() {
    loop: while (true) {
      this.visited.fill(false)
      let updated = false
      for (let i = 0; i < this.r.length; i++) {
        if (this.r[i] === -1) updated = this.dfs(i) || updated
      }
      if (!updated) break loop
    }
    return this.r.length - this.r.reduce((cnt, v) => (v === -1 ? cnt + 1 : cnt), 0)
  }
}

function main() {
  const input = new Input()

  const H = input.number()
  const W = input.number()
  const A: { i: number; j: number }[][] = [...Array(5e5 + 1)].map(() => [])
  for (let i = 0; i < H; i++) {
    for (let j = 0; j < W; j++) {
      const a = input.number()
      A[a].push({ i, j })
    }
  }

  let ans = 0
  const r_map = array(H, 0)
  const c_map = array(W, 0)
  for (let a = 1; a <= 5e5; a++) {
    if (A[a].isEmpty()) continue

    const r_set = new Set<number>()
    const c_set = new Set<number>()
    for (const { i, j } of A[a]) {
      r_set.add(i)
      c_set.add(j)
    }
    const R = r_set.size
    const C = c_set.size
    const g = new BipartiteMatching(R, C)
    Array.from(r_set).forEach((r, idx) => (r_map[r] = idx))
    Array.from(c_set).forEach((c, idx) => (c_map[c] = idx))
    for (const { i, j } of A[a]) {
      g.add(r_map[i], c_map[j])
    }
    const f = g.run()
    ans += f
  }

  console.log(ans)
}

main()
0