結果

問題 No.1637 Easy Tree Query
ユーザー 👑 箱星箱星
提出日時 2021-08-06 21:40:47
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,010 ms / 2,000 ms
コード長 1,384 bytes
コンパイル時間 13,730 ms
コンパイル使用メモリ 445,176 KB
実行使用メモリ 99,812 KB
最終ジャッジ日時 2023-10-17 05:06:28
合計ジャッジ時間 39,852 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 283 ms
53,804 KB
testcase_01 AC 294 ms
53,812 KB
testcase_02 AC 1,010 ms
84,016 KB
testcase_03 AC 285 ms
53,804 KB
testcase_04 AC 595 ms
66,508 KB
testcase_05 AC 699 ms
66,404 KB
testcase_06 AC 606 ms
62,184 KB
testcase_07 AC 505 ms
62,060 KB
testcase_08 AC 531 ms
66,200 KB
testcase_09 AC 722 ms
70,520 KB
testcase_10 AC 558 ms
62,168 KB
testcase_11 AC 789 ms
72,620 KB
testcase_12 AC 790 ms
71,136 KB
testcase_13 AC 501 ms
62,088 KB
testcase_14 AC 683 ms
70,456 KB
testcase_15 AC 787 ms
72,728 KB
testcase_16 AC 796 ms
72,536 KB
testcase_17 AC 545 ms
62,168 KB
testcase_18 AC 665 ms
66,392 KB
testcase_19 AC 680 ms
68,412 KB
testcase_20 AC 753 ms
70,544 KB
testcase_21 AC 671 ms
68,516 KB
testcase_22 AC 924 ms
75,460 KB
testcase_23 AC 547 ms
62,100 KB
testcase_24 AC 632 ms
68,608 KB
testcase_25 AC 689 ms
66,376 KB
testcase_26 AC 776 ms
71,060 KB
testcase_27 AC 884 ms
74,316 KB
testcase_28 AC 636 ms
62,260 KB
testcase_29 AC 756 ms
70,664 KB
testcase_30 AC 577 ms
68,304 KB
testcase_31 AC 583 ms
62,228 KB
testcase_32 AC 601 ms
66,284 KB
testcase_33 AC 654 ms
62,284 KB
testcase_34 AC 710 ms
99,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter
import java.util.*
import kotlin.math.*

fun PrintWriter.solve() {
    val n = nextInt()
    val q = nextInt()
    val adj = Array(n) { mutableListOf<Int>() }
    for (i in 0 until n - 1) {
        val v1 = nextInt() - 1
        val v2 = nextInt() - 1
        adj[v1].add(v2)
        adj[v2].add(v1)
    }
    val size = Array(n) { 1 }
    fun dfs(v: Int, p: Int): Int {
        for (w in adj[v]) {
            if (w != p) {
                size[v] += dfs(w, v)
            }
        }
        return size[v]
    }
    dfs(0, -1)
    var sum = 0L
    for (i in 0 until q) {
        val p = nextInt() - 1
        val x = nextLong()
        sum += size[p] * x
        println(sum)
    }
}

fun main2() {
    val writer = PrintWriter(System.out, false)
    writer.solve()
    writer.flush()
}

fun main() = Thread(null, ::main2, "", 64 * 1024 * 1024)
        .apply { setUncaughtExceptionHandler { _, e -> e.printStackTrace(); kotlin.system.exitProcess(1) } }
        .apply { start() }.join()

// region Scanner
private var st = StringTokenizer("")
private val br = System.`in`.bufferedReader()

fun next(): String {
    while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())
    return st.nextToken()
}

fun nextInt() = next().toInt()
fun nextLong() = next().toLong()
fun nextLine() = br.readLine()
fun nextDouble() = next().toDouble()
// endregion
0