結果

問題 No.1767 BLUE to RED
ユーザー yudedakoyudedako
提出日時 2022-02-02 13:49:06
言語 Scala(Beta)
(3.4.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,149 bytes
コンパイル時間 10,935 ms
コンパイル使用メモリ 256,128 KB
実行使用メモリ 144,880 KB
最終ジャッジ日時 2023-09-02 02:44:22
合計ジャッジ時間 58,869 ms
ジャッジサーバーID
(参考情報)
judge16 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 911 ms
63,188 KB
testcase_01 AC 913 ms
62,664 KB
testcase_02 AC 902 ms
63,076 KB
testcase_03 AC 902 ms
62,596 KB
testcase_04 AC 928 ms
63,184 KB
testcase_05 AC 1,009 ms
63,200 KB
testcase_06 AC 913 ms
62,696 KB
testcase_07 AC 967 ms
62,964 KB
testcase_08 AC 976 ms
63,084 KB
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.io.StdIn.*
import scala.util.chaining.*
import scala.math.*

class UnionFind(val size: Int):
  private val vec = Array.fill(size){-1}
  def find(a: Int): Int =
    if vec(a) < 0 then
      a
    else
      vec(a) = find(vec(a))
      vec(a)
  def same(a: Int, b: Int): Boolean = find(a) == find(b)
  def unite(a: Int, b: Int): Unit =
    val i = find(a)
    val j = find(b)
    if i != j then
      if vec(i) < vec(j) then
        vec(i) += vec(j)
        vec(j) = i
      else
        vec(j) += vec(i)
        vec(i) = j
@main def main =
  val Array(n, m) = readLine().split(' ').map(_.toInt)
  val a = readLine().split(' ').map(_.toInt)
  val b = readLine().split(' ').map(_.toInt)
  val aSet = a.toSet
  val allCell = (a ++ b).sorted
  val uft = UnionFind(n + m + 1)
  for i <- 0 until n + m if aSet.contains(allCell(i)) do
    uft.unite(i, n + m)
  var result = 0
  for i <- (1 until n + m).sortBy(i => allCell(i) - allCell(i - 1)) do
    if !uft.same(i, i - 1) then
      uft.unite(i, i - 1)
      result += allCell(i) - allCell(i - 1)
  println(result)
0