結果
| 問題 | 
                            No.697 池の数はいくつか
                             | 
                    
| ユーザー | 
                             iwkjosec
                         | 
                    
| 提出日時 | 2019-06-08 18:24:38 | 
| 言語 | C#(csc)  (csc 3.9.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 638 ms / 6,000 ms | 
| コード長 | 1,920 bytes | 
| コンパイル時間 | 1,192 ms | 
| コンパイル使用メモリ | 106,368 KB | 
| 実行使用メモリ | 68,884 KB | 
| 最終ジャッジ日時 | 2024-11-08 08:17:51 | 
| 合計ジャッジ時間 | 7,188 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 32 | 
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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)];
}
            
            
            
        
            
iwkjosec