結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー 👑 tamatotamato
提出日時 2021-03-05 21:42:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 293 ms / 2,000 ms
コード長 1,047 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 82,724 KB
実行使用メモリ 105,028 KB
最終ジャッジ日時 2024-04-16 09:08:11
合計ジャッジ時間 7,355 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
54,568 KB
testcase_01 AC 44 ms
54,592 KB
testcase_02 AC 44 ms
54,968 KB
testcase_03 AC 293 ms
104,524 KB
testcase_04 AC 270 ms
104,440 KB
testcase_05 AC 273 ms
104,336 KB
testcase_06 AC 291 ms
104,528 KB
testcase_07 AC 282 ms
104,428 KB
testcase_08 AC 203 ms
92,880 KB
testcase_09 AC 129 ms
83,224 KB
testcase_10 AC 125 ms
82,616 KB
testcase_11 AC 109 ms
80,220 KB
testcase_12 AC 180 ms
90,956 KB
testcase_13 AC 192 ms
91,120 KB
testcase_14 AC 202 ms
92,228 KB
testcase_15 AC 166 ms
89,600 KB
testcase_16 AC 97 ms
78,100 KB
testcase_17 AC 95 ms
78,088 KB
testcase_18 AC 254 ms
102,244 KB
testcase_19 AC 102 ms
79,676 KB
testcase_20 AC 87 ms
77,332 KB
testcase_21 AC 165 ms
89,424 KB
testcase_22 AC 145 ms
87,716 KB
testcase_23 AC 89 ms
77,580 KB
testcase_24 AC 86 ms
77,252 KB
testcase_25 AC 88 ms
77,324 KB
testcase_26 AC 94 ms
77,440 KB
testcase_27 AC 50 ms
61,708 KB
testcase_28 AC 60 ms
68,524 KB
testcase_29 AC 88 ms
77,688 KB
testcase_30 AC 89 ms
78,072 KB
testcase_31 AC 72 ms
71,728 KB
testcase_32 AC 89 ms
77,280 KB
testcase_33 AC 90 ms
80,916 KB
testcase_34 AC 189 ms
98,160 KB
testcase_35 AC 107 ms
84,248 KB
testcase_36 AC 81 ms
78,912 KB
testcase_37 AC 185 ms
105,028 KB
testcase_38 AC 172 ms
101,832 KB
testcase_39 AC 44 ms
55,132 KB
testcase_40 AC 44 ms
55,056 KB
testcase_41 AC 44 ms
54,616 KB
testcase_42 AC 43 ms
53,832 KB
testcase_43 AC 44 ms
54,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    from collections import deque
    input = sys.stdin.buffer.readline

    N = int(input())
    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)

    que = deque()
    que.append(1)
    seen = [-1] * (N+1)
    seen[1] = 0
    par = [0] * (N+1)
    child = [[] for _ in range(N+1)]
    seq = []
    while que:
        v = que.popleft()
        seq.append(v)
        for u in adj[v]:
            if seen[u] == -1:
                seen[u] = seen[v] + 1
                par[u] = v
                child[v].append(u)
                que.append(u)
    seq.reverse()

    size = [1] * (N+1)
    for v in seq:
        for u in child[v]:
            size[v] += size[u]
    ans = N * N
    for v in seq:
        # par
        ans += size[v] * (N - size[v])
        # child
        for u in child[v]:
            ans += size[u] * (N - size[u])
    print(ans)


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