結果

問題 No.697 池の数はいくつか
ユーザー iwkjoseciwkjosec
提出日時 2019-06-08 18:24:38
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 598 ms / 6,000 ms
コード長 1,920 bytes
コンパイル時間 1,484 ms
コンパイル使用メモリ 111,060 KB
実行使用メモリ 79,364 KB
最終ジャッジ日時 2024-04-25 20:29:50
合計ジャッジ時間 7,153 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
24,232 KB
testcase_01 AC 25 ms
24,088 KB
testcase_02 AC 25 ms
23,856 KB
testcase_03 AC 26 ms
26,004 KB
testcase_04 AC 25 ms
23,976 KB
testcase_05 AC 25 ms
22,072 KB
testcase_06 AC 26 ms
24,232 KB
testcase_07 AC 25 ms
24,104 KB
testcase_08 AC 25 ms
23,700 KB
testcase_09 AC 25 ms
25,992 KB
testcase_10 AC 25 ms
26,000 KB
testcase_11 AC 25 ms
26,136 KB
testcase_12 AC 25 ms
25,880 KB
testcase_13 AC 25 ms
24,232 KB
testcase_14 AC 25 ms
23,984 KB
testcase_15 AC 25 ms
24,112 KB
testcase_16 AC 24 ms
21,812 KB
testcase_17 AC 26 ms
26,272 KB
testcase_18 AC 25 ms
25,972 KB
testcase_19 AC 25 ms
23,828 KB
testcase_20 AC 25 ms
24,104 KB
testcase_21 AC 25 ms
24,236 KB
testcase_22 AC 26 ms
26,144 KB
testcase_23 AC 25 ms
24,220 KB
testcase_24 AC 77 ms
34,320 KB
testcase_25 AC 76 ms
34,296 KB
testcase_26 AC 76 ms
34,184 KB
testcase_27 AC 76 ms
34,372 KB
testcase_28 AC 77 ms
32,104 KB
testcase_29 AC 218 ms
56,476 KB
testcase_30 AC 543 ms
77,208 KB
testcase_31 AC 598 ms
79,364 KB
testcase_32 AC 538 ms
75,144 KB
testcase_33 AC 544 ms
75,012 KB
testcase_34 AC 539 ms
77,188 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;
using static System.Math;

class Program
{
    static void Main()
    {
        var HW = ReadLine().Split();
        var H = int.Parse(HW[0]);
        var W = int.Parse(HW[1]);
        Span<bool> t = stackalloc bool[H * W];
        var zero = 0;
        for (int i = 0; i < H; i++)
        {
            var line = ReadLine();
            for (int j = 0; j < W; j++)
            {
                if (line[j * 2] == '0') zero++;
                else t[i * W + j] = true;
            }
        }

        if (zero == 0)
        {
            WriteLine(1);
            return;
        }

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


public class UnionFind
{
    int[] parent;
    int count;
    public int Count => count;

    public UnionFind(int length)
    {
        parent = new int[length];
        for (int i = 0; i < parent.Length; i++)
        {
            parent[i] = -1;
        }
        count = length;
    }

    public int Root(int x) => parent[x] < 0 ? 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 (parent[x] < parent[y])
        {
            parent[x] += parent[y];
            parent[y] = x;
        }
        else
        {
            parent[y] += parent[x];
            parent[x] = y;
        }
    }

    public int Size(int x) => -parent[Root(x)];
}
0