結果

問題 No.697 池の数はいくつか
ユーザー rpy3cpprpy3cpp
提出日時 2018-08-12 21:44:29
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 247 ms / 6,000 ms
コード長 1,238 bytes
コンパイル時間 1,653 ms
コンパイル使用メモリ 170,984 KB
実行使用メモリ 38,172 KB
最終ジャッジ日時 2023-08-08 02:17:48
合計ジャッジ時間 5,399 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,384 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 30 ms
6,912 KB
testcase_25 AC 29 ms
6,840 KB
testcase_26 AC 30 ms
6,888 KB
testcase_27 AC 29 ms
6,924 KB
testcase_28 AC 30 ms
6,980 KB
testcase_29 AC 206 ms
38,008 KB
testcase_30 AC 246 ms
38,052 KB
testcase_31 AC 206 ms
38,028 KB
testcase_32 AC 247 ms
38,112 KB
testcase_33 AC 246 ms
38,092 KB
testcase_34 AC 246 ms
38,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
constexpr int LAND = 10'000'000;

struct UnionFind{
    vector<int> data;
    UnionFind(int size) : data(size, -1){}
    void unite(int x, int y){
        x = root(x);
        y = root(y);
        if (x != y){
            if (data[y] < data[x]) swap(x, y);
            data[x] += data[y];
            data[y] = x;
        }
    }
    int root(int x) {return data[x] < 0 ? x : data[x] = root(data[x]);}
    int size(int x) {return -data[root(x)];}
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int H, W;
    cin >> H >> W;
    UnionFind uf(H * W);
    vector<char> pre(W);
    vector<char> cur(W);

    for (int h = 0 ; h < H; ++h){
        int hW = h * W;
        for (auto & c : cur) cin >> c;
        for (int w = 0; w < W; ++w){
            if (cur[w] == '1'){
                if (w > 0 and cur[w - 1] == '1') uf.unite(hW + w, hW + w - 1);
                if (h > 0 and pre[w] == '1') uf.unite(hW + w, hW + w - W);
            }else{
                uf.data[hW + w] = LAND;
            }
        }
        swap(pre, cur);
    }

    // count the number of ponds
    int ans = 0;
    for (auto v : uf.data) if (v < 0) ++ans;
    cout << ans << endl;

    return 0;
}
0