結果

問題 No.2504 NOT Path Painting
ユーザー suisensuisen
提出日時 2023-07-22 17:04:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,384 ms / 2,000 ms
コード長 1,201 bytes
コンパイル時間 155 ms
コンパイル使用メモリ 81,756 KB
実行使用メモリ 259,352 KB
最終ジャッジ日時 2023-10-23 23:32:31
合計ジャッジ時間 18,763 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
68,168 KB
testcase_01 AC 800 ms
86,552 KB
testcase_02 AC 731 ms
84,884 KB
testcase_03 AC 849 ms
91,344 KB
testcase_04 AC 841 ms
90,092 KB
testcase_05 AC 857 ms
89,832 KB
testcase_06 AC 828 ms
88,480 KB
testcase_07 AC 880 ms
91,816 KB
testcase_08 AC 838 ms
90,600 KB
testcase_09 AC 807 ms
88,220 KB
testcase_10 AC 855 ms
87,624 KB
testcase_11 AC 862 ms
92,908 KB
testcase_12 AC 897 ms
88,832 KB
testcase_13 AC 716 ms
82,544 KB
testcase_14 AC 683 ms
84,596 KB
testcase_15 AC 847 ms
104,536 KB
testcase_16 AC 880 ms
106,964 KB
testcase_17 AC 942 ms
108,740 KB
testcase_18 AC 676 ms
103,724 KB
testcase_19 AC 1,384 ms
259,352 KB
testcase_20 AC 992 ms
231,800 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(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