結果
| 問題 | 
                            No.697 池の数はいくつか
                             | 
                    
| ユーザー | 
                             kokatsu
                         | 
                    
| 提出日時 | 2021-10-26 20:05:34 | 
| 言語 | D  (dmd 2.109.1)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 1,540 ms / 6,000 ms | 
| コード長 | 2,153 bytes | 
| コンパイル時間 | 2,353 ms | 
| コンパイル使用メモリ | 216,188 KB | 
| 実行使用メモリ | 91,188 KB | 
| 最終ジャッジ日時 | 2024-06-22 13:02:12 | 
| 合計ジャッジ時間 | 13,478 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 32 | 
ソースコード
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;
}
            
            
            
        
            
kokatsu