結果

問題 No.2504 NOT Path Painting
ユーザー suisensuisen
提出日時 2023-07-22 17:13:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,315 ms / 2,000 ms
コード長 1,268 bytes
コンパイル時間 245 ms
コンパイル使用メモリ 81,732 KB
実行使用メモリ 249,184 KB
最終ジャッジ日時 2023-10-23 23:42:00
合計ジャッジ時間 17,379 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
66,224 KB
testcase_01 AC 782 ms
85,484 KB
testcase_02 AC 671 ms
85,588 KB
testcase_03 AC 741 ms
88,252 KB
testcase_04 AC 751 ms
89,356 KB
testcase_05 AC 762 ms
89,600 KB
testcase_06 AC 733 ms
87,536 KB
testcase_07 AC 794 ms
91,160 KB
testcase_08 AC 767 ms
91,164 KB
testcase_09 AC 686 ms
87,244 KB
testcase_10 AC 741 ms
88,756 KB
testcase_11 AC 758 ms
91,988 KB
testcase_12 AC 824 ms
90,500 KB
testcase_13 AC 688 ms
84,356 KB
testcase_14 AC 617 ms
83,124 KB
testcase_15 AC 775 ms
103,472 KB
testcase_16 AC 807 ms
104,108 KB
testcase_17 AC 859 ms
103,888 KB
testcase_18 AC 610 ms
104,020 KB
testcase_19 AC 1,315 ms
249,184 KB
testcase_20 AC 903 ms
205,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
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__':
    sys.setrecursionlimit(100000)

    P = 998244353

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

    T = int(sys.stdin.readline().rstrip())
    for _ in range(T):
        n = int(sys.stdin.readline().rstrip())
        g = [[] for _ in range(n)]
        for _ in range(n - 1):
            u, v = map(int, sys.stdin.readline().rstrip().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