結果

問題 No.697 池の数はいくつか
ユーザー GoryudyumaGoryudyuma
提出日時 2018-06-09 19:51:24
言語 Scala(Beta)
(3.4.0)
結果
RE  
実行時間 -
コード長 1,822 bytes
コンパイル時間 11,629 ms
コンパイル使用メモリ 281,536 KB
実行使用メモリ 253,988 KB
最終ジャッジ日時 2024-05-04 05:29:00
合計ジャッジ時間 74,956 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 926 ms
66,084 KB
testcase_01 AC 918 ms
65,944 KB
testcase_02 AC 905 ms
65,976 KB
testcase_03 AC 930 ms
65,940 KB
testcase_04 AC 919 ms
65,952 KB
testcase_05 AC 935 ms
65,704 KB
testcase_06 AC 907 ms
66,004 KB
testcase_07 AC 903 ms
65,948 KB
testcase_08 AC 899 ms
65,912 KB
testcase_09 AC 921 ms
65,700 KB
testcase_10 AC 941 ms
65,932 KB
testcase_11 AC 931 ms
65,864 KB
testcase_12 AC 927 ms
65,888 KB
testcase_13 AC 941 ms
65,888 KB
testcase_14 AC 940 ms
65,980 KB
testcase_15 AC 916 ms
65,884 KB
testcase_16 AC 913 ms
66,004 KB
testcase_17 AC 927 ms
66,040 KB
testcase_18 AC 930 ms
65,956 KB
testcase_19 AC 912 ms
65,920 KB
testcase_20 AC 911 ms
65,988 KB
testcase_21 AC 906 ms
65,832 KB
testcase_22 AC 912 ms
66,128 KB
testcase_23 AC 921 ms
65,676 KB
testcase_24 AC 2,651 ms
152,440 KB
testcase_25 AC 2,687 ms
152,048 KB
testcase_26 AC 2,710 ms
152,232 KB
testcase_27 AC 2,664 ms
152,208 KB
testcase_28 AC 2,718 ms
151,652 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