結果
| 問題 | 
                            No.697 池の数はいくつか
                             | 
                    
| ユーザー | 
                             iwkjosec
                         | 
                    
| 提出日時 | 2018-06-09 22:21:36 | 
| 言語 | C#(csc)  (csc 3.9.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 1,909 ms / 6,000 ms | 
| コード長 | 1,905 bytes | 
| コンパイル時間 | 937 ms | 
| コンパイル使用メモリ | 106,624 KB | 
| 実行使用メモリ | 111,236 KB | 
| 最終ジャッジ日時 | 2024-11-08 07:40:21 | 
| 合計ジャッジ時間 | 15,907 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| 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;
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();
            for (int j = 0; j < W; j++)
            {
                if (line[j] == "0") zero++;
                else map[i, j] = 1;
            }
        }
        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++)
            {
                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
{
    int[] parent;
    int count;
    public UnionFind(int size)
    {
        var b = new int[size];
        for (int i = 0; i < b.Length; i++)
        {
            b[i] = -1;
        }
        parent = b;
        count = size;
    }
    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[y] += parent[x];
            parent[x] = y;
        }
        else
        {
            parent[x] += parent[y];
            parent[y] = x;
        }
    }
    public int Size(int x) => -parent[Root(x)];
    public int Count() => count;
}
            
            
            
        
            
iwkjosec