結果
| 問題 | 
                            No.697 池の数はいくつか
                             | 
                    
| ユーザー | 
                             neko_the_shadow
                         | 
                    
| 提出日時 | 2019-03-23 17:41:55 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 2,212 ms / 6,000 ms | 
| コード長 | 1,269 bytes | 
| コンパイル時間 | 756 ms | 
| コンパイル使用メモリ | 85,368 KB | 
| 最終ジャッジ日時 | 2025-01-07 00:27:01 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 32 | 
ソースコード
#include<iostream>
#include<vector>
#include<set>
struct unionfind {
    std::vector<int> parent;
    unionfind(int n) {
        for (int i = 0; i < n; i++) parent.push_back(i);
    }
    int find(int x) {
        if (x == parent[x]) {
            return x;
        } else {
            return parent[x] = find(parent[x]);
        }
    }
    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x != y) {
            parent[y] = x;
        }
    }
};
int main() {
    int h, w;
    std::cin >> h >> w;
    std::vector<std::vector<int>> a(h, std::vector<int>(w));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) std::cin >> a[i][j];
    }
    auto uf = unionfind(h * w);
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (a[i][j] == 0) continue;
            if (i + 1 < h && a[i + 1][j] == 1) uf.unite(w * i + j, w * (i + 1) + j);
            if (j + 1 < w && a[i][j + 1] == 1) uf.unite(w * i + j, w * i + (j + 1));
        }
    }
    std::set<int> s;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (a[i][j] == 1) {
                s.insert(uf.find(w * i + j));
            }
        }
    }
    std::cout << s.size() << std::endl;
}
            
            
            
        
            
neko_the_shadow