結果

問題 No.1124 Earthquake Safety
ユーザー yakamotoyakamoto
提出日時 2020-07-22 23:17:22
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,011 ms / 3,000 ms
コード長 4,445 bytes
コンパイル時間 18,595 ms
コンパイル使用メモリ 461,228 KB
実行使用メモリ 91,896 KB
最終ジャッジ日時 2024-06-23 01:00:50
合計ジャッジ時間 64,725 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 289 ms
54,020 KB
testcase_01 AC 295 ms
54,132 KB
testcase_02 AC 288 ms
54,184 KB
testcase_03 AC 287 ms
54,296 KB
testcase_04 AC 292 ms
53,980 KB
testcase_05 AC 295 ms
54,468 KB
testcase_06 AC 315 ms
54,380 KB
testcase_07 AC 441 ms
60,588 KB
testcase_08 AC 738 ms
89,508 KB
testcase_09 AC 1,011 ms
91,896 KB
testcase_10 AC 897 ms
90,196 KB
testcase_11 AC 294 ms
49,836 KB
testcase_12 AC 293 ms
49,860 KB
testcase_13 AC 291 ms
49,924 KB
testcase_14 AC 843 ms
89,404 KB
testcase_15 AC 846 ms
89,280 KB
testcase_16 AC 883 ms
89,320 KB
testcase_17 AC 881 ms
89,324 KB
testcase_18 AC 885 ms
89,120 KB
testcase_19 AC 826 ms
89,296 KB
testcase_20 AC 859 ms
89,364 KB
testcase_21 AC 997 ms
89,352 KB
testcase_22 AC 874 ms
89,092 KB
testcase_23 AC 860 ms
89,392 KB
testcase_24 AC 855 ms
89,248 KB
testcase_25 AC 833 ms
89,460 KB
testcase_26 AC 832 ms
89,244 KB
testcase_27 AC 872 ms
89,328 KB
testcase_28 AC 789 ms
89,320 KB
testcase_29 AC 849 ms
89,380 KB
testcase_30 AC 855 ms
89,368 KB
testcase_31 AC 850 ms
89,332 KB
testcase_32 AC 827 ms
89,388 KB
testcase_33 AC 825 ms
89,316 KB
testcase_34 AC 834 ms
89,232 KB
testcase_35 AC 884 ms
89,568 KB
testcase_36 AC 868 ms
89,408 KB
testcase_37 AC 864 ms
89,268 KB
testcase_38 AC 860 ms
89,320 KB
testcase_39 AC 860 ms
89,392 KB
testcase_40 AC 875 ms
89,400 KB
testcase_41 AC 838 ms
89,316 KB
testcase_42 AC 1,001 ms
89,724 KB
testcase_43 AC 858 ms
89,436 KB
testcase_44 AC 831 ms
89,160 KB
testcase_45 AC 822 ms
89,404 KB
testcase_46 AC 846 ms
89,332 KB
testcase_47 AC 851 ms
89,404 KB
testcase_48 AC 838 ms
89,408 KB
testcase_49 AC 845 ms
89,600 KB
testcase_50 AC 284 ms
54,220 KB
testcase_51 AC 286 ms
54,048 KB
testcase_52 AC 292 ms
54,192 KB
testcase_53 AC 282 ms
54,044 KB
testcase_54 AC 294 ms
54,052 KB
testcase_55 AC 295 ms
54,176 KB
testcase_56 AC 287 ms
54,176 KB
testcase_57 AC 292 ms
54,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.util.*
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min

val MOD = 1_000_000_007L

/**
 * (dist, parent, queue)
 */
fun traceBfs(g: Array<IntArray>, rt: Int = 0): Array<IntArray> {
  val n = g.size
  val q = IntArray(n)
  val d = IntArray(n)
  val p = IntArray(n){-2}
  var cur = 0
  var last = 1
  p[rt] = -1
  q[0] = rt
  d[rt] = 0
  while (cur < last) {
    val v = q[cur++]
    for (u in g[v]) {
      if (p[u] == -2) {
        p[u] = v
        q[last++] = u
        d[u] = d[v] + 1
      }
    }
  }
  return arrayOf(d, p, q)
}
fun packUGraph(n: Int, from: IntArray, to: IntArray): Array<IntArray> {
  val p = IntArray(n)
  val m = from.size
  for (i in 0 until m) {
    ++p[from[i]]
    ++p[to[i]]
  }
  val g = Array(n){IntArray(p[it])}
  for (i in 0 until m) {
    g[from[i]][--p[from[i]]] = to[i]
    g[to[i]][--p[to[i]]] = from[i]
  }
  return g
}
class Solver(stream: InputStream, private val out: java.io.PrintWriter) {
  fun solve() {
    val N = ni()
    val (from, to) = na2(N - 1, -1)
    val g = packUGraph(N, from, to)
    val (_, par, que) = traceBfs(g)

    val pow2 = LongArray(N + 1)
    pow2[0] = 1
    for (i in 1 until pow2.size) {
      pow2[i] = pow2[i - 1] * 2 % MOD
    }

    var ans = 0L
    val dpc = IntArray(N){1}
    val dp = Array(4){LongArray(N){1} }
    for (i in N - 1 downTo 0) {
      val u = que[i]
      for (v in g[u]) {
        if (v == par[u]) continue
        dp[3][u] = dp[3][u] * dp[0][v] % MOD +
            dp[3][v] * dp[0][u] % MOD +
            3 * dp[2][v] * dp[1][u] % MOD +
            3 * dp[2][u] * dp[1][v] % MOD +
            dp[3][u] * dp[0][v] % MOD
        dp[3][u] %= MOD

        dp[2][u] = dp[2][u] * dp[0][v] % MOD +
            dp[2][v] * dp[0][u] % MOD +
            2 * dp[1][v] * dp[1][u] % MOD +
            dp[2][u] * dp[0][v] % MOD
        dp[2][u] %= MOD

        dp[1][u] = dp[1][u] * dp[0][v] % MOD +
            dp[1][v] * dp[0][u] % MOD +
            dp[1][u] * dp[0][v] % MOD
        dp[1][u] %= MOD

        dp[0][u] = dp[0][u] * dp[0][v] * 2 % MOD

        dpc[u] += dpc[v]

        debug{"$u->$v"}
      }
      debug{"$u"}
      debugDim(dp)

      val outNodes = N - dpc[u]
      ans = (ans + dp[3][u] * pow2[max(0, outNodes - 1)]) % MOD
    }

    out.println(ans)
  }













































  private val isDebug = try {
    // なんか本番でエラーでる
    System.getenv("MY_DEBUG") != null
  } catch (t: Throwable) {
    false
  }

  private var tokenizer: StringTokenizer? = null
  private val reader = BufferedReader(InputStreamReader(stream), 32768)
  private fun next(): String {
    while (tokenizer == null || !tokenizer!!.hasMoreTokens()) {
      tokenizer = StringTokenizer(reader.readLine())
    }
    return tokenizer!!.nextToken()
  }

  private fun ni() = next().toInt()
  private fun nl() = next().toLong()
  private fun ns() = next()
  private fun na(n: Int, offset: Int = 0): IntArray {
    return map(n) { ni() + offset }
  }
  private fun nal(n: Int, offset: Int = 0): LongArray {
    val res = LongArray(n)
    for (i in 0 until n) {
      res[i] = nl() + offset
    }
    return res
  }

  private fun na2(n: Int, offset: Int = 0): Array<IntArray> {
    val a  = Array(2){IntArray(n)}
    for (i in 0 until n) {
      for (e in a) {
        e[i] = ni() + offset
      }
    }
    return a
  }

  private inline fun map(n: Int, f: (Int) -> Int): IntArray {
    val res = IntArray(n)
    for (i in 0 until n) {
      res[i] = f(i)
    }
    return res
  }

  private inline fun debug(msg: () -> String) {
    if (isDebug) System.err.println(msg())
  }

  private fun debug(a: LongArray) {
    debug { a.joinToString(" ") }
  }

  private fun debug(a: IntArray) {
    debug { a.joinToString(" ") }
  }

  private fun debug(a: BooleanArray) {
    debug { a.map { if (it) 1 else 0 }.joinToString("") }
  }

  private fun debugDim(A: Array<LongArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }
  private fun debugDim(A: Array<IntArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }

  /**
   * 勝手にimport消されるのを防ぎたい
   */
  private fun hoge() {
    min(1, 2)
    max(1, 2)
    abs(-10)
  }
}

fun main() {
  val out = java.io.PrintWriter(System.out)
  Solver(System.`in`, out).solve()
  out.flush()
}
0