結果

問題 No.697 池の数はいくつか
ユーザー iwkjoseciwkjosec
提出日時 2018-06-16 17:40:39
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,928 ms / 6,000 ms
コード長 1,604 bytes
コンパイル時間 1,025 ms
コンパイル使用メモリ 111,668 KB
実行使用メモリ 86,160 KB
最終ジャッジ日時 2024-04-25 20:09:18
合計ジャッジ時間 14,825 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
25,076 KB
testcase_01 AC 26 ms
25,076 KB
testcase_02 AC 26 ms
27,136 KB
testcase_03 AC 26 ms
27,396 KB
testcase_04 AC 26 ms
25,204 KB
testcase_05 AC 27 ms
25,328 KB
testcase_06 AC 26 ms
25,208 KB
testcase_07 AC 28 ms
25,460 KB
testcase_08 AC 26 ms
25,460 KB
testcase_09 AC 26 ms
25,200 KB
testcase_10 AC 27 ms
29,440 KB
testcase_11 AC 25 ms
25,008 KB
testcase_12 AC 26 ms
25,208 KB
testcase_13 AC 24 ms
25,080 KB
testcase_14 AC 27 ms
25,456 KB
testcase_15 AC 25 ms
24,876 KB
testcase_16 AC 26 ms
27,152 KB
testcase_17 AC 27 ms
27,116 KB
testcase_18 AC 26 ms
25,332 KB
testcase_19 AC 25 ms
25,268 KB
testcase_20 AC 27 ms
27,264 KB
testcase_21 AC 28 ms
25,220 KB
testcase_22 AC 27 ms
25,484 KB
testcase_23 AC 27 ms
25,360 KB
testcase_24 AC 206 ms
52,176 KB
testcase_25 AC 203 ms
52,440 KB
testcase_26 AC 205 ms
52,180 KB
testcase_27 AC 204 ms
54,480 KB
testcase_28 AC 206 ms
54,480 KB
testcase_29 AC 1,890 ms
83,988 KB
testcase_30 AC 1,602 ms
82,064 KB
testcase_31 AC 1,928 ms
86,160 KB
testcase_32 AC 1,610 ms
83,980 KB
testcase_33 AC 1,596 ms
82,068 KB
testcase_34 AC 1,607 ms
84,032 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();
        var H = int.Parse(HW[0]);
        var W = int.Parse(HW[1]);

        var uf = new UnionFind(H * W);
        var line = ReadLine().Split();
        var zero = W - line.Select(int.Parse).Sum();
        for (int i = 1; i < W; i++)
        {
            var m = W + i;
            if (line[i - 1] == "1" && line[i] == "1") uf.Unite(m - 1, m);
        }
        for (int i = 1; i < H; i++)
        {
            var line2 = ReadLine().Split();
            for (int j = 0; j < W; j++)
            {
                if (line2[j] == "0") zero++;
                else
                {
                    var m = i * W + j;
                    if (j != 0 && line2[j - 1] == "1") uf.Unite(m - 1, m);
                    if (line[j] == "1") uf.Unite(m - W, m);
                }
            }
            line = line2;
        }
        WriteLine(uf.count - zero);
    }
}

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

    public UnionFind(int size)
    {
        var b = new int[size];
        for (int i = 0; i < b.Length; i++)
        {
            b[i] = i;
        }
        parent = b;
        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--;
        parent[x] = y;
    }
}
0