結果

問題 No.1507 Road Blocked
ユーザー ygd.ygd.
提出日時 2021-05-29 23:46:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 172 ms / 2,000 ms
コード長 1,188 bytes
コンパイル時間 235 ms
コンパイル使用メモリ 82,284 KB
実行使用メモリ 99,572 KB
最終ジャッジ日時 2024-04-25 22:46:53
合計ジャッジ時間 7,047 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
52,728 KB
testcase_01 AC 33 ms
53,476 KB
testcase_02 AC 34 ms
54,016 KB
testcase_03 AC 109 ms
99,572 KB
testcase_04 AC 159 ms
94,776 KB
testcase_05 AC 172 ms
94,812 KB
testcase_06 AC 169 ms
94,644 KB
testcase_07 AC 165 ms
94,724 KB
testcase_08 AC 167 ms
94,580 KB
testcase_09 AC 167 ms
94,552 KB
testcase_10 AC 163 ms
94,808 KB
testcase_11 AC 156 ms
94,812 KB
testcase_12 AC 166 ms
94,748 KB
testcase_13 AC 166 ms
94,664 KB
testcase_14 AC 166 ms
94,628 KB
testcase_15 AC 168 ms
94,436 KB
testcase_16 AC 160 ms
94,936 KB
testcase_17 AC 168 ms
94,692 KB
testcase_18 AC 169 ms
94,704 KB
testcase_19 AC 164 ms
94,532 KB
testcase_20 AC 164 ms
94,572 KB
testcase_21 AC 166 ms
94,664 KB
testcase_22 AC 165 ms
94,440 KB
testcase_23 AC 170 ms
94,664 KB
testcase_24 AC 165 ms
94,652 KB
testcase_25 AC 161 ms
94,772 KB
testcase_26 AC 160 ms
94,800 KB
testcase_27 AC 169 ms
95,028 KB
testcase_28 AC 166 ms
94,896 KB
testcase_29 AC 167 ms
94,472 KB
testcase_30 AC 171 ms
94,452 KB
testcase_31 AC 159 ms
94,680 KB
testcase_32 AC 163 ms
94,984 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