結果

問題 No.2047 Path Factory
ユーザー 👑 箱
提出日時 2022-05-04 01:29:18
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,127 ms / 2,000 ms
コード長 1,054 bytes
コンパイル時間 12,897 ms
コンパイル使用メモリ 436,024 KB
実行使用メモリ 106,604 KB
最終ジャッジ日時 2024-04-23 13:29:43
合計ジャッジ時間 33,093 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 296 ms
52,084 KB
testcase_01 AC 302 ms
52,056 KB
testcase_02 AC 308 ms
51,936 KB
testcase_03 AC 299 ms
52,188 KB
testcase_04 AC 305 ms
51,964 KB
testcase_05 AC 319 ms
52,232 KB
testcase_06 AC 323 ms
52,028 KB
testcase_07 AC 685 ms
90,436 KB
testcase_08 AC 636 ms
89,732 KB
testcase_09 AC 679 ms
88,812 KB
testcase_10 AC 755 ms
92,724 KB
testcase_11 AC 641 ms
89,196 KB
testcase_12 AC 563 ms
71,228 KB
testcase_13 AC 564 ms
71,340 KB
testcase_14 AC 691 ms
89,588 KB
testcase_15 AC 704 ms
90,600 KB
testcase_16 AC 608 ms
80,092 KB
testcase_17 AC 607 ms
79,676 KB
testcase_18 AC 666 ms
91,832 KB
testcase_19 AC 695 ms
94,324 KB
testcase_20 AC 777 ms
98,296 KB
testcase_21 AC 658 ms
91,480 KB
testcase_22 AC 480 ms
61,580 KB
testcase_23 AC 492 ms
63,272 KB
testcase_24 AC 494 ms
62,008 KB
testcase_25 AC 786 ms
106,604 KB
testcase_26 AC 1,127 ms
102,992 KB
testcase_27 AC 883 ms
94,460 KB
testcase_28 AC 684 ms
94,548 KB
testcase_29 AC 346 ms
52,864 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(0, -1)
    println((dp[0][1] + dp[0][2]) % mod)
}

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

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