結果

問題 No.2047 Path Factory
ユーザー 👑 箱
提出日時 2022-04-25 22:52:36
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,142 ms / 2,000 ms
コード長 3,554 bytes
コンパイル時間 16,477 ms
コンパイル使用メモリ 453,256 KB
実行使用メモリ 168,736 KB
最終ジャッジ日時 2024-04-23 13:28:05
合計ジャッジ時間 29,449 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 255 ms
58,344 KB
testcase_01 AC 260 ms
58,560 KB
testcase_02 AC 256 ms
58,356 KB
testcase_03 AC 260 ms
58,532 KB
testcase_04 AC 250 ms
58,668 KB
testcase_05 AC 266 ms
58,700 KB
testcase_06 AC 261 ms
58,648 KB
testcase_07 AC 652 ms
103,980 KB
testcase_08 AC 553 ms
97,600 KB
testcase_09 AC 580 ms
103,632 KB
testcase_10 AC 692 ms
106,468 KB
testcase_11 AC 552 ms
97,536 KB
testcase_12 AC 475 ms
95,316 KB
testcase_13 AC 491 ms
95,356 KB
testcase_14 AC 576 ms
97,768 KB
testcase_15 AC 599 ms
103,532 KB
testcase_16 AC 551 ms
101,652 KB
testcase_17 AC 552 ms
99,652 KB
testcase_18 AC 590 ms
104,036 KB
testcase_19 AC 666 ms
109,948 KB
testcase_20 AC 738 ms
122,784 KB
testcase_21 AC 574 ms
103,944 KB
testcase_22 AC 413 ms
72,100 KB
testcase_23 AC 420 ms
75,916 KB
testcase_24 AC 414 ms
74,068 KB
testcase_25 AC 892 ms
168,736 KB
testcase_26 AC 1,142 ms
154,536 KB
testcase_27 AC 936 ms
123,904 KB
testcase_28 AC 801 ms
121,164 KB
testcase_29 AC 276 ms
58,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

fun PrintWriter.solve() {
    val n = 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 dp = Array(n) { ModIntArray(3) { ModInt(0) } }
    fun dfs(v: Int, par: Int) {
        var p = ModInt(1)
        var s = ModInt(0)
        var t = ModInt(0)
        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])
                s = s * (dp[w][1] + dp[w][2]) + p * (dp[w][0] + dp[w][1])
                p *= dp[w][1] + dp[w][2]
            }
        }
        dp[v][0] = p
        dp[v][1] = s
        dp[v][2] = t
    }
    dfs(0, -1)
    println(dp[0][1] + dp[0][2])
}

// region ModInt
class ModInt(x: Long) {
    companion object {
        const val MOD = 998244353L
        //const val MOD = 1000000007L
    }

    constructor(y: Int) : this(y.toLong())

    val x = (x % MOD + MOD) % MOD

    operator fun plus(other: ModInt): ModInt {
        return ModInt(x + other.x)
    }

    operator fun minus(other: ModInt): ModInt {
        return ModInt(x - other.x)
    }

    operator fun times(other: ModInt): ModInt {
        return ModInt(x * other.x)
    }

    operator fun div(other: ModInt): ModInt {
        return this * other.inv()
    }

    fun pow(exp: Long): ModInt {
        if (exp == 0L) return ModInt(1L)
        var a = pow(exp shr 1)
        a *= a
        if (exp and 1L == 0L) return a
        return this * a
    }

    fun inv(): ModInt {
        return this.pow(MOD - 2)
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as ModInt

        if (x != other.x) return false

        return true
    }

    override fun hashCode(): Int {
        return x.hashCode()
    }

    override fun toString(): String {
        return "$x"
    }
}

class ModIntArray(n: Int) {
    private val a = LongArray(n) { 0L }

    constructor(n: Int, init: (Int) -> ModInt) : this(n) {
        for (i in 0 until n) {
            a[i] = init(i).x
        }
    }

    operator fun set(i: Int, x: ModInt) {
        a[i] = x.x
    }

    operator fun get(i: Int): ModInt {
        return ModInt(a[i])
    }

    fun joinToString(s: String = ", "): String {
        return a.joinToString(s)
    }
}

fun Long.toModInt(): ModInt {
    return ModInt(this)
}

fun Int.toModInt(): ModInt {
    return ModInt(this)
}

fun binomSimple(n: Long, k: Long): ModInt {
    var numer = ModInt(1)
    var denom = ModInt(1)
    for (i in 0 until k) {
        numer *= ModInt(n - i)
        denom *= ModInt(k - i)
    }
    return numer / denom
}
// endregion

fun main() {
    Thread(null, {
        val writer = PrintWriter(System.out, false)
        writer.solve()
        writer.flush()
    }, "solve", abs(1L.shl(26)))
        .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