結果

問題 No.1507 Road Blocked
ユーザー AEnAEn
提出日時 2022-12-06 21:26:06
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 649 ms / 2,000 ms
コード長 653 bytes
コンパイル時間 235 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 54,784 KB
最終ジャッジ日時 2024-10-13 09:18:44
合計ジャッジ時間 21,327 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**7)

N = int(input())
G = [list() for _ in range(N)]
for i in range(N-1):
    u,v = map(int, input().split())
    u-=1;v-=1
    G[u].append(v)
    G[v].append(u)

depth = [0]*N
sub_tree = [0]*N
num = 0
def dfs(G, v, p, d):
    depth[v] = d
    for next_v in G[v]:
        if next_v == p:
            continue
        dfs(G, next_v, v, d+1)
    sub_tree[v] = 1
    for c in G[v]:
        if c== p:
            continue
        sub_tree[v] += sub_tree[c]
dfs(G,0,-1,0)
for i in range(1,N):
    s = sub_tree[i]
    num += s*(s-1)//2+(N-s)*(N-s-1)//2
mod = 998244353
n = (N-1)*N*(N-1)//2
print((num*pow(n,mod-2,mod))%mod)
0