結果

問題 No.1878 union-find の数え上げ
ユーザー yudedakoyudedako
提出日時 2022-04-18 15:23:27
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 1,387 ms / 2,000 ms
コード長 844 bytes
コンパイル時間 12,494 ms
コンパイル使用メモリ 254,972 KB
実行使用メモリ 85,420 KB
最終ジャッジ日時 2023-08-28 11:35:11
合計ジャッジ時間 33,095 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,354 ms
85,420 KB
testcase_01 AC 1,379 ms
80,236 KB
testcase_02 AC 1,387 ms
80,332 KB
testcase_03 AC 1,376 ms
80,304 KB
testcase_04 AC 1,377 ms
80,436 KB
testcase_05 AC 1,166 ms
65,208 KB
testcase_06 AC 1,182 ms
65,576 KB
testcase_07 AC 950 ms
62,752 KB
testcase_08 AC 944 ms
62,912 KB
testcase_09 AC 939 ms
62,740 KB
testcase_10 AC 945 ms
62,892 KB
testcase_11 AC 972 ms
62,804 KB
testcase_12 AC 953 ms
63,088 KB
testcase_13 AC 955 ms
62,808 KB
testcase_14 AC 1,305 ms
79,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter
import scala.collection.mutable.*
import scala.io.StdIn.*
import scala.util.chaining.*
import scala.math.*
import scala.reflect.ClassTag
import scala.util.*
import scala.annotation.tailrec
import scala.collection.mutable
import scala.language.strictEquality

@main def main =
  inline val MOD = 998244353
  val n = readLine.toInt
  val parent = Array.fill(n - 1){readLine.toInt - 1}
  val children = Array.fill(n){ArrayBuffer[Int]()}
  for (p, i) <- parent.zipWithIndex do
    children(p).addOne(i + 1)
  val depth = Array.fill(n){0}
  val queue = mutable.ArrayDeque[Int](0)
  while queue.nonEmpty do
    val top = queue.removeHead()
    for child <- children(top) do
      depth(child) = depth(top) + 1
      queue.append(child)
  val result = depth.foldLeft(1L){(acc, v) => acc * max(v, 1) % MOD}
  println(result)
0