結果

問題 No.1507 Road Blocked
ユーザー ygd.ygd.
提出日時 2021-05-29 23:46:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 255 ms / 2,000 ms
コード長 1,188 bytes
コンパイル時間 276 ms
コンパイル使用メモリ 86,984 KB
実行使用メモリ 100,100 KB
最終ジャッジ日時 2023-08-08 05:07:55
合計ジャッジ時間 12,344 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,140 KB
testcase_01 AC 78 ms
71,200 KB
testcase_02 AC 76 ms
71,240 KB
testcase_03 AC 152 ms
100,100 KB
testcase_04 AC 239 ms
94,460 KB
testcase_05 AC 245 ms
94,640 KB
testcase_06 AC 249 ms
94,384 KB
testcase_07 AC 246 ms
94,520 KB
testcase_08 AC 242 ms
94,592 KB
testcase_09 AC 255 ms
94,732 KB
testcase_10 AC 250 ms
94,508 KB
testcase_11 AC 251 ms
94,400 KB
testcase_12 AC 245 ms
94,592 KB
testcase_13 AC 242 ms
94,408 KB
testcase_14 AC 245 ms
94,588 KB
testcase_15 AC 243 ms
94,408 KB
testcase_16 AC 243 ms
94,500 KB
testcase_17 AC 246 ms
94,620 KB
testcase_18 AC 250 ms
94,588 KB
testcase_19 AC 244 ms
94,672 KB
testcase_20 AC 245 ms
94,424 KB
testcase_21 AC 248 ms
94,508 KB
testcase_22 AC 238 ms
94,408 KB
testcase_23 AC 245 ms
94,500 KB
testcase_24 AC 246 ms
94,184 KB
testcase_25 AC 254 ms
94,380 KB
testcase_26 AC 252 ms
94,240 KB
testcase_27 AC 252 ms
95,352 KB
testcase_28 AC 245 ms
94,604 KB
testcase_29 AC 242 ms
94,336 KB
testcase_30 AC 241 ms
94,592 KB
testcase_31 AC 240 ms
94,336 KB
testcase_32 AC 248 ms
94,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import math
import sys 
input = sys.stdin.buffer.readline

def main():
    N = int(input()); MOD = 998244353
    G = [[] for _ in range(N)]
    branch =[]
    for _ in range(N-1):
        u,v = map(int,input().split())
        u-=1;v-=1
        G[u].append(v)
        G[v].append(u)
        branch.append((u,v))
    root = 0
    cnt = [0]*N
    par = [-1]*N
    stack = []
    stack.append(~root)
    stack.append(root)
    while stack:
        v = stack.pop()
        if v >= 0:
            for u in G[v]:
                if u == par[v]: continue
                par[u] = v
                stack.append(~u)
                stack.append(u)
        else:
            v = ~v
            u = par[v]
            cnt[v] += 1
            if u == -1: continue
            cnt[u] += cnt[v]
    #print(cnt)
    y = (N-1)*N*(N-1)//2; y%=MOD
    x = y
    for u,v in branch:
        if v == par[u]:
            u,v = v,u
        #vの親がu. par[v] = u
        v_cnt = cnt[v]
        u_cnt = N - v_cnt
        x -= v_cnt*u_cnt%MOD
        x %= MOD
    #print(x,y)
    g = math.gcd(x,y)
    x //= g
    y //= g
    ans = x * pow(y,MOD-2,MOD)
    print(ans%MOD)

if __name__ == '__main__':
    main()
0