結果

問題 No.1479 Matrix Eraser
ユーザー yagisumiyagisumi
提出日時 2021-06-17 14:47:39
言語 TypeScript
(5.4.3)
結果
WA  
実行時間 -
コード長 3,882 bytes
コンパイル時間 8,430 ms
コンパイル使用メモリ 258,192 KB
実行使用メモリ 175,588 KB
最終ジャッジ日時 2023-08-30 22:17:05
合計ジャッジ時間 39,927 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 218 ms
92,624 KB
testcase_01 AC 263 ms
94,924 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 496 ms
123,188 KB
testcase_08 AC 582 ms
132,708 KB
testcase_09 AC 811 ms
136,052 KB
testcase_10 AC 1,032 ms
159,236 KB
testcase_11 AC 865 ms
153,560 KB
testcase_12 AC 528 ms
126,716 KB
testcase_13 AC 588 ms
132,728 KB
testcase_14 AC 543 ms
125,812 KB
testcase_15 AC 351 ms
106,924 KB
testcase_16 AC 567 ms
128,276 KB
testcase_17 AC 1,163 ms
167,228 KB
testcase_18 AC 1,151 ms
167,436 KB
testcase_19 AC 1,156 ms
167,296 KB
testcase_20 AC 1,157 ms
167,392 KB
testcase_21 AC 1,156 ms
167,156 KB
testcase_22 AC 1,158 ms
167,096 KB
testcase_23 AC 1,158 ms
167,068 KB
testcase_24 AC 1,150 ms
167,148 KB
testcase_25 AC 1,156 ms
167,264 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 647 ms
144,808 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 375 ms
140,320 KB
testcase_38 AC 681 ms
147,488 KB
testcase_39 AC 1,078 ms
175,588 KB
testcase_40 AC 218 ms
92,552 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