結果

問題 No.1843 Tree ANDistance
ユーザー yudedakoyudedako
提出日時 2022-02-28 17:52:53
言語 Scala(Beta)
(3.4.0)
結果
TLE  
実行時間 -
コード長 1,429 bytes
コンパイル時間 14,969 ms
コンパイル使用メモリ 259,620 KB
実行使用メモリ 117,312 KB
最終ジャッジ日時 2023-09-21 02:19:31
合計ジャッジ時間 23,141 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

extension (value: Int)
  inline def isSet(digit: Int): Boolean =
    ((value >> digit) & 1) != 0

@main def main =
  inline val MOD = 1000000007
  val n = readLine.toInt
  val edges = Array.fill(n - 1){
    val Array(a, b, c) = readLine.split(' ').map(_.toInt)
    (a - 1, b - 1, c)
  }
  val graph = Array.fill(n){ArrayBuffer[(Int, Int)]()}
  for (a, b, c) <- edges do
    graph(a).addOne((b, c))
    graph(b).addOne((a, c))
  val depth = Array.fill(n){-1}.tap(_(0) = 0)
  val queue = Queue(0)
  while queue.nonEmpty do
    val top = queue.dequeue()
    for (next, _) <- graph(top) if depth(next) == -1 do
      depth(next) = depth(top) + 1
      queue.enqueue(next)
  val fromLeaf = (0 until n).sortBy(depth)(using Ordering[Int].reverse).toArray
  val endPath = Array.fill(n){0}
  var result = 0L
  for d <- 0 until 30 do
    java.util.Arrays.fill(endPath, 0)
    var onePath = 0L
    for node <- fromLeaf do
      var count = 0
      for (child, c) <- graph(node) if depth(child) > depth(node) && c.isSet(d) do
        onePath += (count + 1L) * (endPath(child) + 1L) % MOD
        count += endPath(child) + 1
      endPath(node) = count
    result += (onePath % MOD << d) % MOD
  println(result % MOD)

0