結果

問題 No.1488 Max Score of the Tree
ユーザー yakamotoyakamoto
提出日時 2021-04-23 21:59:57
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 416 ms / 2,000 ms
コード長 3,778 bytes
コンパイル時間 19,352 ms
コンパイル使用メモリ 466,600 KB
実行使用メモリ 58,256 KB
最終ジャッジ日時 2024-07-04 08:01:13
合計ジャッジ時間 32,660 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 386 ms
58,256 KB
testcase_01 AC 380 ms
54,436 KB
testcase_02 AC 392 ms
54,412 KB
testcase_03 AC 393 ms
54,524 KB
testcase_04 AC 416 ms
54,172 KB
testcase_05 AC 338 ms
53,604 KB
testcase_06 AC 354 ms
54,176 KB
testcase_07 AC 374 ms
54,164 KB
testcase_08 AC 362 ms
54,188 KB
testcase_09 AC 347 ms
53,864 KB
testcase_10 AC 370 ms
54,304 KB
testcase_11 AC 376 ms
54,516 KB
testcase_12 AC 332 ms
53,788 KB
testcase_13 AC 352 ms
53,628 KB
testcase_14 AC 369 ms
54,288 KB
testcase_15 AC 357 ms
54,096 KB
testcase_16 AC 350 ms
53,684 KB
testcase_17 AC 348 ms
53,944 KB
testcase_18 AC 368 ms
54,240 KB
testcase_19 AC 363 ms
53,992 KB
testcase_20 AC 347 ms
53,796 KB
testcase_21 AC 355 ms
54,112 KB
testcase_22 AC 353 ms
53,828 KB
testcase_23 AC 325 ms
53,352 KB
testcase_24 AC 328 ms
53,356 KB
testcase_25 AC 324 ms
53,452 KB
testcase_26 AC 368 ms
54,344 KB
testcase_27 AC 351 ms
53,556 KB
testcase_28 AC 350 ms
53,732 KB
testcase_29 AC 355 ms
53,700 KB
testcase_30 AC 376 ms
54,376 KB
testcase_31 AC 378 ms
54,340 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:56:36: warning: unnecessary non-null assertion (!!) on a non-null receiver of type Long
    val ans = cont.sum() + dp.max()!!
                                   ^
Main.kt:150:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: LongArray) {
          ^
Main.kt:154:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: IntArray) {
          ^
Main.kt:158:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: BooleanArray) {
          ^
Main.kt:162:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun toString(a: BooleanArray) = run{a.map { if (it) 1 else 0 }.joinToString("")}
          ^
Main.kt:164:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<LongArray>) {
          ^
Main.kt:171:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<IntArray>) {
          ^
Main.kt:178:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<BooleanArray>) {
          ^
Main.kt:195:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun assert(b: Boolean) = run{if (!b) throw AssertionError()}
          ^

ソースコード

diff #

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

val MOD = 1_000_000_007

data class Edge(val id: Int, val u: Int, val w: Int)
class Solver(stream: InputStream, private val out: java.io.PrintWriter) {
  private val reader = BufferedReader(InputStreamReader(stream), 32768)

  fun solve() {
    val (N, K) = na(2)
    val g = Array(N){ mutableListOf<Edge>() }
    val W = IntArray(N - 1)
    for (i in 0 until N - 1) {
      val (v, u) = na(2, -1)
      val w = ni()
      W[i] = w
      g[v].add(Edge(i, u, w))
      g[u].add(Edge(i, v, w))
    }

    val cnt = IntArray(N - 1) // 葉の数
    fun dfs(v: Int, par: Int): Int {
      var res = if (g[v].size == 1) 1 else 0
      for (e in g[v]) {
        if (e.u == par) continue
        val r = dfs(e.u, v)
        cnt[e.id] = r
        res += r
      }
      return res
    }

    dfs(0, -1)
    debug(cnt)

    val cont = LongArray(N - 1){cnt[it].toLong()*W[it]}
    debug(cont)

    val dp = LongArray(K + 1)
    for (i in 0 until N - 1) {
      for (k in K downTo 0) {
        if (k + W[i] <= K) {
          dp[k + W[i]] = max(dp[k + W[i]], dp[k] + cont[i])
        }
      }
    }
    debug(dp)

    val ans = cont.sum() + dp.max()!!
    out.println(ans)
  }













































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

  private var tokenizer: StringTokenizer? = null
  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 IntArray(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 debug(msg: () -> String) {
    if (isDebug) System.err.println(msg())
  }

  /**
   * コーナーケースでエラー出たりするので、debug(dp[1])のように添え字付きの場合はdebug{}をつかうこと
   */
  private inline fun debug(a: LongArray) {
    debug { a.joinToString(" ") }
  }

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

  private inline fun debug(a: BooleanArray) {
    debug { toString(a) }
  }

  private inline fun toString(a: BooleanArray) = run{a.map { if (it) 1 else 0 }.joinToString("")}

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

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

  private inline fun assert(b: Boolean) = run{if (!b) throw AssertionError()}
  private inline fun assert(b: Boolean, f: () -> String) = run{if (!b) throw AssertionError(f())}
}

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