結果

問題 No.697 池の数はいくつか
ユーザー iwkjoseciwkjosec
提出日時 2018-06-09 22:07:57
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 2,443 ms / 6,000 ms
コード長 1,781 bytes
コンパイル時間 2,312 ms
コンパイル使用メモリ 112,700 KB
実行使用メモリ 139,052 KB
最終ジャッジ日時 2024-04-25 19:56:57
合計ジャッジ時間 20,456 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
25,172 KB
testcase_01 AC 23 ms
25,008 KB
testcase_02 AC 24 ms
25,264 KB
testcase_03 AC 25 ms
27,120 KB
testcase_04 AC 24 ms
25,172 KB
testcase_05 AC 23 ms
25,208 KB
testcase_06 AC 23 ms
27,132 KB
testcase_07 AC 23 ms
27,008 KB
testcase_08 AC 23 ms
27,004 KB
testcase_09 AC 24 ms
25,044 KB
testcase_10 AC 24 ms
24,916 KB
testcase_11 AC 26 ms
27,336 KB
testcase_12 AC 24 ms
25,392 KB
testcase_13 AC 26 ms
26,996 KB
testcase_14 AC 24 ms
27,084 KB
testcase_15 AC 24 ms
25,300 KB
testcase_16 AC 23 ms
25,172 KB
testcase_17 AC 24 ms
26,992 KB
testcase_18 AC 24 ms
25,172 KB
testcase_19 AC 26 ms
25,048 KB
testcase_20 AC 25 ms
26,960 KB
testcase_21 AC 27 ms
27,132 KB
testcase_22 AC 27 ms
26,956 KB
testcase_23 AC 25 ms
23,216 KB
testcase_24 AC 295 ms
50,476 KB
testcase_25 AC 295 ms
50,432 KB
testcase_26 AC 288 ms
50,320 KB
testcase_27 AC 297 ms
52,764 KB
testcase_28 AC 290 ms
50,748 KB
testcase_29 AC 2,379 ms
103,684 KB
testcase_30 AC 2,416 ms
139,052 KB
testcase_31 AC 2,377 ms
101,872 KB
testcase_32 AC 2,359 ms
138,580 KB
testcase_33 AC 2,362 ms
138,752 KB
testcase_34 AC 2,443 ms
138,992 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;

class Program
{
    static void Main()
    {
        var HW = ReadLine().Split().Select(int.Parse).ToArray();
        var H = HW[0];
        var W = HW[1];
        var map = new int[H, W];
        var zero = 0;
        for (int i = 0; i < H; i++)
        {
            var line = ReadLine().Split().Select(int.Parse).ToArray();
            for (int j = 0; j < W; j++)
            {
                map[i, j] = line[j];
                if (line[j] == 0) zero++;
            }
        }

        var uf = new UnionFind(H * W);
        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                if (map[i, j] == 0) continue;
                var m = i * W + j;
                if (j != W - 1 && map[i, j + 1] == 1) uf.Unite(m, m + 1);
                if (i != H - 1 && map[i + 1, j] == 1) uf.Unite(m, m + W);
            }
        }
        WriteLine(uf.count - zero);
    }
}

class UnionFind
{
    public int count;
    int[] parent;
    int[] rank;

    public UnionFind(int size)
    {
        var b = new int[size];
        for (int i = 0; i < b.Length; i++)
        {
            b[i] = i;
        }
        parent = b;
        rank = new int[size];
        count = size;
    }

    int Root(int x) => parent[x] == x ? x : parent[x] = Root(parent[x]);

    public bool Same(int x, int y) => Root(x) == Root(y);

    public void Unite(int x, int y)
    {
        x = Root(x);
        y = Root(y);
        if (x == y) return;
        count--;
        if (rank[x] < rank[y])
        {
            parent[x] = y;
        }
        else
        {
            parent[y] = x;
            if (rank[x] == rank[y]) rank[x]++;
        }
    }
}
0