結果

問題 No.697 池の数はいくつか
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-03-23 17:41:55
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,116 ms / 6,000 ms
コード長 1,269 bytes
コンパイル時間 882 ms
コンパイル使用メモリ 88,272 KB
実行使用メモリ 105,888 KB
最終ジャッジ日時 2023-08-08 02:24:36
合計ジャッジ時間 15,491 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 223 ms
14,044 KB
testcase_25 AC 225 ms
14,128 KB
testcase_26 AC 222 ms
14,048 KB
testcase_27 AC 224 ms
14,124 KB
testcase_28 AC 221 ms
13,804 KB
testcase_29 AC 1,361 ms
105,060 KB
testcase_30 AC 2,109 ms
104,440 KB
testcase_31 AC 1,363 ms
104,476 KB
testcase_32 AC 2,116 ms
104,740 KB
testcase_33 AC 2,109 ms
104,428 KB
testcase_34 AC 2,109 ms
105,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0