結果

問題 No.697 池の数はいくつか
ユーザー maimai
提出日時 2018-06-09 20:28:46
言語 cLay
(20240104-1)
結果
AC  
実行時間 193 ms / 6,000 ms
コード長 1,017 bytes
コンパイル時間 2,232 ms
コンパイル使用メモリ 177,880 KB
実行使用メモリ 39,356 KB
最終ジャッジ日時 2024-04-25 19:55:14
合計ジャッジ時間 5,182 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 1 ms
6,944 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,940 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 1 ms
6,944 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 1 ms
6,944 KB
testcase_24 AC 23 ms
8,096 KB
testcase_25 AC 22 ms
7,968 KB
testcase_26 AC 21 ms
8,176 KB
testcase_27 AC 22 ms
8,124 KB
testcase_28 AC 23 ms
8,116 KB
testcase_29 AC 157 ms
39,324 KB
testcase_30 AC 193 ms
39,284 KB
testcase_31 AC 178 ms
39,256 KB
testcase_32 AC 181 ms
39,324 KB
testcase_33 AC 180 ms
39,264 KB
testcase_34 AC 179 ms
39,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Unionfind {
public:
    vector<int> data;
    Unionfind(size_t size) : data(size, -1) { }
    bool connect(size_t x, size_t y) {
        x = root(x); y = root(y);
        if (x != y) {
            if (data[y] < data[x]) swap(x, y);
            data[x] += data[y]; data[y] = (int)x;
        }
        return x != y;
    }
    inline bool same(size_t x, size_t y) {
        return root(x) == root(y);
    }
    inline size_t root(size_t x) {
        return (size_t)(data[x] < 0 ? x : data[x] = root(data[x]));
    }
    inline int size(size_t x) {
        return -data[root(x)];
    }
};

int H,W;
{
    rd(H,W);
    Unionfind uf(H*W+1);
    int cnt = W*H+1;
    int p = 0;
    REP(y, H){
        REP(x, W){
            int c;rd(c);
            if (c == 0) cnt -= uf.connect(p, H*W);
            else{
                if (0<y && !uf.same(p-W, H*W)) cnt -= uf.connect(p, p-W);
                if (0<x && !uf.same(p-1, H*W)) cnt -= uf.connect(p, p-1);
            }
            ++p;
        }
    }
    wt(cnt-1);
}
0