結果

問題 No.697 池の数はいくつか
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-03-23 17:41:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,025 ms / 6,000 ms
コード長 1,269 bytes
コンパイル時間 1,115 ms
コンパイル使用メモリ 89,140 KB
実行使用メモリ 105,812 KB
最終ジャッジ日時 2024-04-25 20:23:39
合計ジャッジ時間 14,567 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 207 ms
14,280 KB
testcase_25 AC 205 ms
14,476 KB
testcase_26 AC 199 ms
14,280 KB
testcase_27 AC 199 ms
14,200 KB
testcase_28 AC 213 ms
14,180 KB
testcase_29 AC 1,311 ms
104,248 KB
testcase_30 AC 2,017 ms
105,812 KB
testcase_31 AC 1,341 ms
104,376 KB
testcase_32 AC 2,008 ms
104,700 KB
testcase_33 AC 2,021 ms
104,560 KB
testcase_34 AC 2,025 ms
104,556 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