結果
問題 | No.872 All Tree Path |
ユーザー | yakamoto |
提出日時 | 2019-08-31 01:19:08 |
言語 | Kotlin (1.9.23) |
結果 |
AC
|
実行時間 | 1,191 ms / 3,000 ms |
コード長 | 1,810 bytes |
コンパイル時間 | 18,607 ms |
コンパイル使用メモリ | 427,676 KB |
実行使用メモリ | 95,760 KB |
最終ジャッジ日時 | 2023-08-14 11:41:14 |
合計ジャッジ時間 | 34,863 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge13 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,172 ms
93,372 KB |
testcase_01 | AC | 1,191 ms
93,636 KB |
testcase_02 | AC | 1,191 ms
93,632 KB |
testcase_03 | AC | 908 ms
92,336 KB |
testcase_04 | AC | 290 ms
53,332 KB |
testcase_05 | AC | 1,164 ms
95,372 KB |
testcase_06 | AC | 1,173 ms
95,760 KB |
testcase_07 | AC | 1,158 ms
93,948 KB |
testcase_08 | AC | 532 ms
62,372 KB |
testcase_09 | AC | 537 ms
62,580 KB |
testcase_10 | AC | 535 ms
62,420 KB |
testcase_11 | AC | 531 ms
62,408 KB |
testcase_12 | AC | 536 ms
62,584 KB |
testcase_13 | AC | 294 ms
52,936 KB |
testcase_14 | AC | 297 ms
52,872 KB |
testcase_15 | AC | 302 ms
52,944 KB |
testcase_16 | AC | 295 ms
52,976 KB |
testcase_17 | AC | 294 ms
53,036 KB |
testcase_18 | AC | 295 ms
52,972 KB |
testcase_19 | AC | 294 ms
53,004 KB |
ソースコード
import kotlin.math.max import kotlin.math.min // なんか本番でエラーでる private val isDebug = runCatching { System.getenv("MY_DEBUG") != null }.fold({it}, {false}) private fun readLn() = readLine()!! private fun readInt() = readLn().toInt() private fun readStrings() = readLn().split(" ") private fun readInts() = readStrings().map { it.toInt() }.toIntArray() private fun readLongs() = readStrings().map { it.toLong() }.toLongArray() private fun debug(msg: () -> String) { if (isDebug) System.err.println(msg()) } private fun debug(a: IntArray) { if (isDebug) debug{a.joinToString(" ")} } val MOD = 1000000007 data class Entry(val i: Int, val x: Long) fun main() { val N = readInt() val g = Array<MutableList<Int>>(N){ mutableListOf()} val U = IntArray(N - 1) val V = IntArray(N - 1) val W = IntArray(N - 1) repeat(N - 1) { var (u, v, w) = readInts() u--; v-- g[u].add(v) g[v].add(u) U[it] = u V[it] = v W[it] = w } val (p, q) = traceBfs(g) val dp = IntArray(N) for (i in N - 1 downTo 0) { val v = q[i] dp[v]++ // 自分 for (u in g[v]) { if (u != p[v]) { dp[v] += dp[u] } } } debug(dp) debug(p) debug(q) var ans = 0L for (i in 0 until N - 1) { val cnt = if (p[U[i]] == V[i]) { dp[U[i]] } else { dp[V[i]] } ans += cnt.toLong() * (N - cnt) * W[i] * 2 } println(ans) } /** * (parent, queue) */ fun traceBfs(g: Array<MutableList<Int>>, rt: Int = 0): Array<IntArray> { val n = g.size val q = IntArray(n) val p = IntArray(n){-2} var cur = 0 var last = 1 p[0] = -1 q[0] = rt while (cur < last) { val v = q[cur++] for (u in g[v]) { if (p[u] == -2) { p[u] = v q[last++] = u } } } return arrayOf(p, q) }