結果

問題 No.697 池の数はいくつか
ユーザー むらためむらため
提出日時 2019-01-24 08:23:20
言語 Nim
(2.0.2)
結果
AC  
実行時間 290 ms / 6,000 ms
コード長 2,242 bytes
コンパイル時間 2,547 ms
コンパイル使用メモリ 61,144 KB
実行使用メモリ 38,320 KB
最終ジャッジ日時 2024-04-25 20:23:00
合計ジャッジ時間 5,509 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,940 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 1 ms
6,944 KB
testcase_21 AC 1 ms
6,944 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 1 ms
6,944 KB
testcase_24 AC 24 ms
6,944 KB
testcase_25 AC 24 ms
6,944 KB
testcase_26 AC 24 ms
6,940 KB
testcase_27 AC 24 ms
6,944 KB
testcase_28 AC 26 ms
6,944 KB
testcase_29 AC 290 ms
38,112 KB
testcase_30 AC 199 ms
38,320 KB
testcase_31 AC 286 ms
38,156 KB
testcase_32 AC 200 ms
38,020 KB
testcase_33 AC 207 ms
38,136 KB
testcase_34 AC 202 ms
38,252 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 8) Warning: imported and not used: 'sequtils' [UnusedImport]

ソースコード

diff #

import sequtils

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

template useUnionFind =
  type UnionFind[T] = object
    parent : seq[T]
  proc root[T](self:var UnionFind[T],x:T): T =
    if self.parent[x] == x: return x
    self.parent[x] = self.root(self.parent[x])
    return self.parent[x]
  proc initUnionFind[T](size:int) : UnionFind[T] =
    result.parent = newSeqUninitialized[T](size)
    for i in 0.int32..<size.int32: result.parent[i] = i
  proc same[T](self:var UnionFind[T],x,y:T) : bool = self.root(x) == self.root(y)
  proc unite[T](self:var UnionFind[T],sx,sy:T) : bool {.discardable.} =
    let rx = self.root(sx)
    let ry = self.root(sy)
    if rx == ry : return false
    self.parent[rx] = ry
    return true

template useRankingUnionFind =
  type UnionFind[T] = object
    parent : seq[T]
    rank : seq[int16]
  proc root[T](self:var UnionFind[T],x:T): T =
    if self.parent[x] == x: return x
    self.parent[x] = self.root(self.parent[x])
    return self.parent[x]
  proc initUnionFind[T](size:int) : UnionFind[T] =
    result.parent = newSeqUninitialized[T](size)
    result.rank = newSeq[int16](size)
    for i in 0.int32..<size.int32: result.parent[i] = i
  proc same[T](self:var UnionFind[T],x,y:T) : bool = self.root(x) == self.root(y)
  proc unite[T](self:var UnionFind[T],sx,sy:T) : bool {.discardable.} =
    let rx = self.root(sx)
    let ry = self.root(sy)
    if rx == ry : return false
    self.parent[rx] = ry
    if self.rank[rx] < self.rank[ry] : self.parent[rx] = ry
    else:
      self.parent[ry] = rx
      if self.rank[rx] == self.rank[ry]: self.rank[rx] += 1
    return true

useUnionFind()
let h = scan()
let w = scan()
var uf = initUnionFind[int32](h * w + 1)
var ans = 0
var i = 1.int32
for y in 0..<h:
  for x in 0..<w:
    if getchar_unlocked() == '1':
      ans += 1
      if x > 0 and uf.parent[i-1] != 0:
        if uf.unite(i,i-1): ans -= 1
      if y > 0 and uf.parent[i-w] != 0:
        if uf.unite(i,i-w): ans -= 1
    else:
      uf.parent[i] = 0
    discard getchar_unlocked()
    i += 1
echo ans
0