結果
問題 | No.697 池の数はいくつか |
ユーザー | kokatsu |
提出日時 | 2021-10-26 20:05:34 |
言語 | D (dmd 2.107.1) |
結果 |
AC
|
実行時間 | 1,444 ms / 6,000 ms |
コード長 | 2,153 bytes |
コンパイル時間 | 2,788 ms |
コンパイル使用メモリ | 211,564 KB |
実行使用メモリ | 101,912 KB |
最終ジャッジ日時 | 2023-09-04 14:43:12 |
合計ジャッジ時間 | 13,859 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
4,384 KB |
testcase_01 | AC | 1 ms
4,376 KB |
testcase_02 | AC | 1 ms
4,380 KB |
testcase_03 | AC | 2 ms
4,380 KB |
testcase_04 | AC | 1 ms
4,376 KB |
testcase_05 | AC | 1 ms
4,380 KB |
testcase_06 | AC | 2 ms
4,380 KB |
testcase_07 | AC | 2 ms
4,380 KB |
testcase_08 | AC | 2 ms
4,376 KB |
testcase_09 | AC | 2 ms
4,376 KB |
testcase_10 | AC | 1 ms
4,376 KB |
testcase_11 | AC | 2 ms
4,376 KB |
testcase_12 | AC | 1 ms
4,380 KB |
testcase_13 | AC | 1 ms
4,380 KB |
testcase_14 | AC | 1 ms
4,376 KB |
testcase_15 | AC | 1 ms
4,380 KB |
testcase_16 | AC | 2 ms
4,376 KB |
testcase_17 | AC | 2 ms
4,384 KB |
testcase_18 | AC | 2 ms
4,380 KB |
testcase_19 | AC | 2 ms
4,384 KB |
testcase_20 | AC | 2 ms
4,380 KB |
testcase_21 | AC | 1 ms
4,380 KB |
testcase_22 | AC | 2 ms
4,380 KB |
testcase_23 | AC | 2 ms
4,380 KB |
testcase_24 | AC | 141 ms
15,832 KB |
testcase_25 | AC | 142 ms
19,112 KB |
testcase_26 | AC | 142 ms
15,204 KB |
testcase_27 | AC | 140 ms
15,500 KB |
testcase_28 | AC | 142 ms
18,824 KB |
testcase_29 | AC | 1,444 ms
91,128 KB |
testcase_30 | AC | 1,248 ms
101,912 KB |
testcase_31 | AC | 1,443 ms
93,480 KB |
testcase_32 | AC | 1,243 ms
91,960 KB |
testcase_33 | AC | 1,238 ms
93,084 KB |
testcase_34 | AC | 1,239 ms
92,528 KB |
ソースコード
import std; void main() { int H, W; readf("%d %d\n", H, W); auto A = new bool[][](H, W); foreach (i; 0 .. H) { auto tmp = readln.chomp.split.to!(int[]); foreach (j; 0 .. W) { if (tmp[j] == 0) { continue; } A[i][j] = true; } } int size = H * W; auto uf = UnionFind!int(size); auto deltaX = [-1, 0, 1, 0], deltaY = [0, 1, 0, -1]; foreach (x; 0 .. H) { foreach (y; 0 .. W) { if (A[x][y] == 0) { continue; } foreach (dx, dy; zip(deltaX, deltaY)) { int nx = x + dx, ny = y + dy; if (nx < 0 || nx >= H || ny < 0 || ny >= W) { continue; } if (!A[nx][ny]) { continue; } uf.unite(x*W+y, nx*W+ny); } } } int res; foreach (x; 0 .. H) { foreach (y; 0 .. W) { if (!A[x][y]) { continue; } int num = x * W + y; if (uf.root(num) == num) { ++res; } } } res.writeln; } /// Union-Find struct UnionFind(T) { /// Constructor this(T n) { par.length = n; cnt.length = n; foreach (i; 0 .. n) { par[i] = i; } cnt[] = 1; } /// Return the root of x. T root(T x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } /// Return whether x and y have the same root. bool same(T x, T y) { return root(x) == root(y); } /// Unite the components of x and y. void unite(T x, T y) { x = root(x), y = root(y); if (x == y) { return; } if (cnt[x] > cnt[y]) { swap(x, y); } cnt[y] += cnt[x]; par[x] = y; } /// Return the size of the tree to which x belongs. T size(T x) { return cnt[root(x)]; } private: T[] par; T[] cnt; }