結果

問題 No.697 池の数はいくつか
ユーザー GoryudyumaGoryudyuma
提出日時 2018-06-09 19:51:24
言語 Scala(Beta)
(3.4.0)
結果
RE  
実行時間 -
コード長 1,822 bytes
コンパイル時間 15,390 ms
コンパイル使用メモリ 263,884 KB
実行使用メモリ 252,312 KB
最終ジャッジ日時 2023-08-16 22:08:06
合計ジャッジ時間 76,378 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 996 ms
63,748 KB
testcase_01 AC 985 ms
63,916 KB
testcase_02 AC 987 ms
63,764 KB
testcase_03 AC 991 ms
63,948 KB
testcase_04 AC 1,001 ms
63,756 KB
testcase_05 AC 989 ms
63,740 KB
testcase_06 AC 992 ms
63,816 KB
testcase_07 AC 987 ms
63,932 KB
testcase_08 AC 983 ms
63,824 KB
testcase_09 AC 988 ms
63,824 KB
testcase_10 AC 995 ms
63,820 KB
testcase_11 AC 978 ms
63,912 KB
testcase_12 AC 1,013 ms
63,700 KB
testcase_13 AC 1,017 ms
63,928 KB
testcase_14 AC 1,010 ms
63,936 KB
testcase_15 AC 1,015 ms
63,892 KB
testcase_16 AC 994 ms
63,888 KB
testcase_17 AC 1,002 ms
63,932 KB
testcase_18 AC 1,006 ms
63,788 KB
testcase_19 AC 992 ms
63,932 KB
testcase_20 AC 1,002 ms
63,764 KB
testcase_21 AC 982 ms
63,716 KB
testcase_22 AC 1,002 ms
63,708 KB
testcase_23 AC 1,000 ms
63,884 KB
testcase_24 AC 2,884 ms
158,980 KB
testcase_25 AC 2,861 ms
159,248 KB
testcase_26 AC 2,873 ms
158,992 KB
testcase_27 AC 2,855 ms
158,808 KB
testcase_28 AC 2,875 ms
158,532 KB
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner

import scala.collection.Searching._
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.immutable._
import scala.io.StdIn.readLine

class IUnionFind(val size: Int) {

  private case class Node(var parent: Option[Int], var treeSize: Int)

  private val nodes = Array.fill[Node](size)(new Node(None, 1))

  def union(t1: Int, t2: Int): IUnionFind = {
    if (t1 == t2) return this

    val root1 = root(t1)
    val root2 = root(t2)
    if (root1 == root2) return this

    val node1 = nodes(root1)
    val node2 = nodes(root2)

    if (node1.treeSize < node2.treeSize) {
      node1.parent = Some(t2)
      node2.treeSize += node1.treeSize
    } else {
      node2.parent = Some(t1)
      node1.treeSize += node2.treeSize
    }
    this
  }

  def connected(t1: Int, t2: Int): Boolean = t1 == t2 || root(t1) == root(t2)

  def root(t: Int): Int = nodes(t).parent match {
    case None => t
    case Some(p) => root(p)
  }
}

object Main {
  val m = (1 << 9) - 1

  def solve(sc: => Scanner): Unit = {
    val H, W = sc.nextInt
    var ui = new IUnionFind(H * W)
    val A = Array.fill[Array[Boolean]](H)(Array.fill[Boolean](W)(sc.nextInt == 1))
    for (i <- 0 until H) {
      for (j <- 0 until W) {
        if (i != H - 1) {
          if (A(i)(j) == A(i + 1)(j)) {
            ui = ui.union(i * W + j, (i + 1) * W + j)
          }
        }
        if (j != W - 1) {
          if (A(i)(j) == A(i)(j + 1)) {
            ui = ui.union(i * W + j, i * W + (j + 1))
          }
        }
      }
    }
    System.gc();
    println((0 until H).map(i => (0 until W).map(j => if (A(i)(j) == true && ui.root(i * W + j) == i * W + j) (1) else (0)).sum).sum)
  }

  def main(args: Array[String]): Unit = {
    val sc: Scanner = new Scanner(System.in)
    solve(sc)
  }
}
0