結果
| 問題 | No.697 池の数はいくつか |
| ユーザー |
norioc
|
| 提出日時 | 2020-03-15 23:49:07 |
| 言語 | D (dmd 2.111.0) |
| 結果 |
AC
|
| 実行時間 | 923 ms / 6,000 ms |
| コード長 | 3,245 bytes |
| 記録 | |
| コンパイル時間 | 2,062 ms |
| コンパイル使用メモリ | 177,128 KB |
| 実行使用メモリ | 110,492 KB |
| 最終ジャッジ日時 | 2024-06-22 05:43:58 |
| 合計ジャッジ時間 | 10,248 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 32 |
ソースコード
import std;
// 3.3 sec で AC
int calc(int[][] g) {
int rows = cast(int)g.length;
int cols = cast(int)g[0].length;
auto delta = [[-1, 0], [1, 0], [0, -1], [0, 1]];
void fill(int row, int col, int[][] g, int[][] used) {
assert(g[row][col] == 1);
auto q = DList!(int)();
// [row, col] で配列生成すると TLE (2 sec 近く遅くなる)
q.insertBack((row << 16) | col);
while (!q.empty) {
auto a = q.front;
q.removeFront();
int r = (a >> 16), c = a & 0xffff;
if (used[r][c]++) continue;
foreach (d; delta) {
int r2 = r + d[0];
int c2 = c + d[1];
if (!(0 <= r2 && r2 < rows && 0 <= c2 && c2 < cols)) continue;
if (g[r2][c2] == 0 || used[r2][c2]) continue;
q.insertBack((r2 << 16) | c2);
}
}
}
int ans = 0;
auto used = new int[][](rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (used[i][j]) continue;
if (g[i][j] == 1) {
ans++;
fill(i, j, g, used);
}
}
}
return ans;
}
// 1.1 sec で AC
int calc2(int[][] g) {
int rows = cast(int)g.length;
int cols = cast(int)g[0].length;
int toIndex(int r, int c) {
return r * cols + c;
}
auto uf = new UnionFind(rows * cols);
int one = 0; // 1 の個数
int uniteCount = 0; // unite して 2 つのグループが 1 つにまとまった回数
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (g[i][j] == 0) continue;
one++;
if (i > 0 && g[i-1][j] == 1) {
int a = toIndex(i, j);
int b = toIndex(i-1, j);
if (uf.unite(a, b)) uniteCount++;
}
if (j > 0 && g[i][j-1] == 1) {
int a = toIndex(i, j);
int b = toIndex(i, j-1);
if (uf.unite(a, b)) uniteCount++;
}
}
}
return one - uniteCount;
}
void main() {
auto hw = readints;
int h = hw[0], w = hw[1];
auto g = new int[][](h, w);
for (int i = 0; i < h; i++) {
g[i][] = readints;
}
// writeln(calc(g));// AC 3.2 sec
writeln(calc2(g)); // AC 1.1 sec
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
class UnionFind {
private int[] _data;
this(int n) {
_data = new int[](n + 1);
_data[] = -1;
}
int root(int a) {
if (_data[a] < 0) return a;
return _data[a] = root(_data[a]);
}
bool unite(int a, int b) {
int ra = root(a);
int rb = root(b);
if (ra != rb) {
if (_data[rb] < _data[ra]) swap(ra, rb);
_data[ra] += _data[rb]; // ra に rb を繋げる
_data[rb] = ra;
}
return ra != rb;
}
bool isSame(int a, int b) {
return root(a) == root(b);
}
/// a が属する集合のサイズ
int groupSize(int a) {
return -_data[root(a)];
}
}
norioc