結果

問題 No.2047 Path Factory
ユーザー 👑 箱星箱星
提出日時 2022-05-23 15:20:46
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 961 ms / 2,000 ms
コード長 1,066 bytes
コンパイル時間 14,248 ms
コンパイル使用メモリ 441,908 KB
実行使用メモリ 103,756 KB
最終ジャッジ日時 2024-04-23 13:27:25
合計ジャッジ時間 28,085 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 269 ms
52,132 KB
testcase_01 AC 261 ms
51,960 KB
testcase_02 AC 265 ms
51,972 KB
testcase_03 AC 265 ms
52,172 KB
testcase_04 AC 266 ms
52,080 KB
testcase_05 AC 268 ms
51,832 KB
testcase_06 AC 272 ms
52,152 KB
testcase_07 AC 585 ms
90,548 KB
testcase_08 AC 565 ms
89,536 KB
testcase_09 AC 566 ms
88,860 KB
testcase_10 AC 645 ms
92,484 KB
testcase_11 AC 541 ms
89,388 KB
testcase_12 AC 502 ms
71,188 KB
testcase_13 AC 481 ms
71,580 KB
testcase_14 AC 550 ms
89,280 KB
testcase_15 AC 580 ms
90,752 KB
testcase_16 AC 519 ms
80,664 KB
testcase_17 AC 529 ms
80,268 KB
testcase_18 AC 551 ms
92,720 KB
testcase_19 AC 583 ms
92,716 KB
testcase_20 AC 655 ms
96,308 KB
testcase_21 AC 555 ms
92,428 KB
testcase_22 AC 432 ms
61,172 KB
testcase_23 AC 439 ms
63,608 KB
testcase_24 AC 432 ms
62,472 KB
testcase_25 AC 756 ms
103,756 KB
testcase_26 AC 961 ms
102,320 KB
testcase_27 AC 778 ms
94,460 KB
testcase_28 AC 632 ms
94,760 KB
testcase_29 AC 319 ms
52,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fun solve() {
    val n = readInt()
    val adj = Array(n) { mutableListOf<Int>() }
    for (i in 0 until n - 1) {
        val edge = readInts()
        val v1 = edge[0] - 1
        val v2 = edge[1] - 1
        adj[v1].add(v2)
        adj[v2].add(v1)
    }
    val mod = 998244353L
    val dp = Array(n) { LongArray(3) { 0 } }
    fun dfs(v: Int, par: Int) {
        var p = 1L
        var s = 0L
        var t = 0L
        for (w in adj[v]) {
            if (w != par) {
                dfs(w, v)
                t = (t * (dp[w][1] + dp[w][2]) + s * (dp[w][0] + dp[w][1])) % mod
                s = (s * (dp[w][1] + dp[w][2]) + p * (dp[w][0] + dp[w][1])) % mod
                p *= dp[w][1] + dp[w][2]
                p %= mod
            }
        }
        dp[v][0] = p
        dp[v][1] = s
        dp[v][2] = t
    }
    dfs(n - 1, -1)
    println((dp[n - 1][1] + dp[n - 1][2]) % mod)
}

fun main() {
    Thread(null, {
        solve()
    }, "solve", 1.shl(26)).start()
}

fun readInt() = readln().toInt()
fun readInts() = readln().split(" ").map { it.toInt() }
0