結果

問題 No.2504 NOT Path Painting
ユーザー suisensuisen
提出日時 2023-07-22 17:02:11
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,155 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 81,704 KB
実行使用メモリ 108,724 KB
最終ジャッジ日時 2023-10-23 23:30:25
合計ジャッジ時間 16,775 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
68,132 KB
testcase_01 AC 838 ms
86,460 KB
testcase_02 AC 737 ms
84,872 KB
testcase_03 AC 878 ms
91,316 KB
testcase_04 AC 829 ms
90,072 KB
testcase_05 AC 859 ms
89,820 KB
testcase_06 AC 842 ms
88,464 KB
testcase_07 AC 884 ms
91,812 KB
testcase_08 AC 875 ms
90,564 KB
testcase_09 AC 818 ms
88,224 KB
testcase_10 AC 830 ms
87,728 KB
testcase_11 AC 848 ms
92,548 KB
testcase_12 AC 890 ms
88,836 KB
testcase_13 AC 722 ms
82,536 KB
testcase_14 AC 684 ms
84,372 KB
testcase_15 AC 864 ms
104,508 KB
testcase_16 AC 880 ms
106,948 KB
testcase_17 AC 933 ms
108,724 KB
testcase_18 AC 680 ms
103,872 KB
testcase_19 RE -
testcase_20 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List

def solve(n: int, g: List[List[int]]):
    m = n * (n + 1) // 2

    ans = 0

    def dfs(u: int, p: int) -> int:
        nonlocal ans

        p_u = m

        sub_u = 1
        for v in g[u]:
            if v == p:
                continue
            sub_v = dfs(v, u)

            p_u -= sub_v * (sub_v + 1) // 2

            p_uv = sub_v * (n - sub_v)
            ans -= m * modinv(m - p_uv) % P

            sub_u += sub_v
        
        if p != -1:
            sub_p = n - sub_u
            p_u -= sub_p * (sub_p + 1) // 2
        
        ans += m * modinv(m - p_u) % P

        return sub_u
    
    dfs(0, -1)

    return ans % P

if __name__ == '__main__':
    P = 998244353

    def modinv(v: int):
        return pow(v, P - 2, P)
    
    answers = []

    T = int(input())
    for _ in range(T):
        n = int(input())
        g = [[] for _ in range(n)]
        for _ in range(n - 1):
            u, v = map(int, input().split())
            u -= 1
            v -= 1
            g[u].append(v)
            g[v].append(u)
        
        answers.append(solve(n, g))
    
    print('\n'.join(map(str, answers)))
0