結果

問題 No.2047 Path Factory
ユーザー 👑 箱
提出日時 2022-05-04 01:26:37
言語 Kotlin
(1.9.10)
結果
RE  
実行時間 -
コード長 967 bytes
コンパイル時間 14,063 ms
コンパイル使用メモリ 442,348 KB
実行使用メモリ 73,224 KB
最終ジャッジ日時 2023-10-20 07:35:10
合計ジャッジ時間 33,324 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 301 ms
54,344 KB
testcase_01 AC 301 ms
54,332 KB
testcase_02 AC 302 ms
54,344 KB
testcase_03 AC 304 ms
54,340 KB
testcase_04 AC 304 ms
54,336 KB
testcase_05 AC 304 ms
54,324 KB
testcase_06 AC 305 ms
54,344 KB
testcase_07 AC 721 ms
67,332 KB
testcase_08 AC 623 ms
64,008 KB
testcase_09 AC 673 ms
66,188 KB
testcase_10 AC 738 ms
71,364 KB
testcase_11 AC 629 ms
64,032 KB
testcase_12 AC 557 ms
61,852 KB
testcase_13 AC 549 ms
61,800 KB
testcase_14 AC 679 ms
68,236 KB
testcase_15 AC 687 ms
67,244 KB
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 AC 491 ms
59,732 KB
testcase_23 AC 513 ms
59,800 KB
testcase_24 AC 483 ms
59,744 KB
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 933 ms
73,224 KB
testcase_28 AC 707 ms
71,252 KB
testcase_29 AC 353 ms
54,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fun main() {
    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 readInt() = readln().toInt()
fun readInts() = readln().split(" ").map { it.toInt() }
0