結果
問題 |
No.697 池の数はいくつか
|
ユーザー |
|
提出日時 | 2019-01-24 08:23:20 |
言語 | Nim (2.2.0) |
結果 |
AC
|
実行時間 | 343 ms / 6,000 ms |
コード長 | 2,242 bytes |
コンパイル時間 | 4,485 ms |
コンパイル使用メモリ | 61,288 KB |
実行使用メモリ | 38,272 KB |
最終ジャッジ日時 | 2024-11-08 08:10:28 |
合計ジャッジ時間 | 6,245 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 32 |
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 8) Warning: imported and not used: 'sequtils' [UnusedImport]
ソースコード
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