結果

問題 No.697 池の数はいくつか
ユーザー nebukuro09nebukuro09
提出日時 2018-06-11 11:25:15
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,644 ms / 6,000 ms
コード長 1,459 bytes
コンパイル時間 3,049 ms
コンパイル使用メモリ 104,916 KB
実行使用メモリ 166,516 KB
最終ジャッジ日時 2023-09-03 20:23:11
合計ジャッジ時間 17,846 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 141 ms
20,144 KB
testcase_25 AC 140 ms
19,908 KB
testcase_26 AC 140 ms
20,136 KB
testcase_27 AC 139 ms
19,080 KB
testcase_28 AC 138 ms
21,088 KB
testcase_29 AC 1,644 ms
166,388 KB
testcase_30 AC 1,267 ms
166,428 KB
testcase_31 AC 1,573 ms
166,516 KB
testcase_32 AC 1,261 ms
166,492 KB
testcase_33 AC 1,261 ms
166,460 KB
testcase_34 AC 1,259 ms
166,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

void main() {
    auto s = readln.split.map!(to!int);
    auto H = s[0];
    auto W = s[1];
    auto A = H.iota.map!(_ => readln.split.map!(to!int).array).array;
    const int[] dr = [0, 0, -1, 1];
    const int[] dc = [-1, 1, 0, 0];
    auto uf = new UnionFind(H*W);
    auto used = new bool[](H*W);
    int ans = 0;
    

    foreach (i; 0..H)
        foreach (j; 0..W)
            foreach (k; 0..4)
                if (i+dr[k] >= 0 && i+dr[k] < H)
                    if (j+dc[k] >= 0 && j + dc[k] < W)
                        if (A[i][j] == 1 && A[i+dr[k]][j+dc[k]] == 1)
                            uf.unite(i*W+j, (i+dr[k])*W+j+dc[k]);


    foreach (i; 0..H)
        foreach (j; 0..W)
            if (A[i][j] == 1 && !used[uf.find(i*W+j)])
                ans += 1, used[uf.find(i*W+j)] = true;

    ans.writeln;
}


class UnionFind {
    int N;
    int[] table;

    this(int n) {
        N = n;
        table = new int[](N);
        fill(table, -1);
    }

    int find(int x) {
        return table[x] < 0 ? x : (table[x] = find(table[x]));
    }

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (table[x] > table[y]) swap(x, y);
        table[x] += table[y];
        table[y] = x;
    }
}

0