結果
| 問題 | 
                            No.697 池の数はいくつか
                             | 
                    
| ユーザー | 
                             Dente
                         | 
                    
| 提出日時 | 2019-09-10 16:56:37 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 2,123 ms / 6,000 ms | 
| コード長 | 1,801 bytes | 
| コンパイル時間 | 1,882 ms | 
| コンパイル使用メモリ | 177,088 KB | 
| 実行使用メモリ | 143,872 KB | 
| 最終ジャッジ日時 | 2024-11-08 08:21:42 | 
| 合計ジャッジ時間 | 16,649 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 32 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a) for(int i = 0; i < (a); i++)
#define ALL(a) (a).begin(),(a).end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const int MOD = 1e9 + 7;
struct UnionFind{
    vector<int> par;
    vector<int> siz;
    UnionFind(int n){
        init(n);
    }
    //n要素で初期化
    void init(int n){
        par.resize(n);
        siz.resize(n);
        for(int i = 0; i < n; i++){
            par[i] = i;
            siz[i] = 1;
        }
    }
    //木の根を求める
    int root(int x){
        if(par[x] == x) return x;
        else return par[x] = root(par[x]);
    }
    //xとyの属する集合を併合
    void unite(int x, int y){
        x = root(x);
        y = root(y);
        if(x == y) return;
        if(siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
    }
    bool same(int x, int y){
        return root(x) == root(y);
    }
    int size(int x){
        return siz[root(x)];
    }
};
int dx[2] = {1, 0}, dy[2] = {0, 1};
signed main(){
    int h,w;
    cin >> h >> w;
    int a[h][w];
    int b[h][w];
    memset(b, -1, sizeof(b));
    int cnt = 0;
    REP(i,h){
        REP(j,w){
            cin >> a[i][j];
            if(a[i][j] == 1){
                b[i][j] = cnt;
                cnt++;
            }
        }
    }
    UnionFind uf(cnt);
    REP(i,h){
        REP(j,w){
            if(b[i][j] != -1){
                REP(k,2){
                    if(i + dx[k] < h && j + dy[k] < w && b[i + dx[k]][j + dy[k]] != -1){
                        uf.unite(b[i][j], b[i + dx[k]][j + dy[k]]);
                    }
                }
            }
        }
    }
    map<int, int> mp;
    REP(i,cnt){
        mp[uf.root(i)]++;
    }
    cout << mp.size() << endl;
}
            
            
            
        
            
Dente