結果
問題 | No.697 池の数はいくつか |
ユーザー |
![]() |
提出日時 | 2018-06-16 17:40:39 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 2,174 ms / 6,000 ms |
コード長 | 1,604 bytes |
コンパイル時間 | 1,251 ms |
コンパイル使用メモリ | 107,008 KB |
実行使用メモリ | 76,036 KB |
最終ジャッジ日時 | 2024-11-08 07:54:31 |
合計ジャッジ時間 | 17,996 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
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();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;}}