結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー hir355hir355
提出日時 2021-03-05 22:21:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 360 ms / 2,000 ms
コード長 756 bytes
コンパイル時間 213 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 101,952 KB
最終ジャッジ日時 2024-04-16 10:07:15
合計ジャッジ時間 9,016 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
52,224 KB
testcase_01 AC 46 ms
51,968 KB
testcase_02 AC 43 ms
51,968 KB
testcase_03 AC 359 ms
97,664 KB
testcase_04 AC 344 ms
97,792 KB
testcase_05 AC 344 ms
98,048 KB
testcase_06 AC 360 ms
97,536 KB
testcase_07 AC 343 ms
97,664 KB
testcase_08 AC 241 ms
90,624 KB
testcase_09 AC 166 ms
82,432 KB
testcase_10 AC 168 ms
83,200 KB
testcase_11 AC 136 ms
78,848 KB
testcase_12 AC 213 ms
87,680 KB
testcase_13 AC 238 ms
89,472 KB
testcase_14 AC 254 ms
90,496 KB
testcase_15 AC 207 ms
85,632 KB
testcase_16 AC 114 ms
77,696 KB
testcase_17 AC 114 ms
77,184 KB
testcase_18 AC 323 ms
94,208 KB
testcase_19 AC 137 ms
78,720 KB
testcase_20 AC 109 ms
76,800 KB
testcase_21 AC 198 ms
85,760 KB
testcase_22 AC 188 ms
84,608 KB
testcase_23 AC 114 ms
77,056 KB
testcase_24 AC 114 ms
77,056 KB
testcase_25 AC 111 ms
76,800 KB
testcase_26 AC 114 ms
77,184 KB
testcase_27 AC 60 ms
61,952 KB
testcase_28 AC 81 ms
69,632 KB
testcase_29 AC 113 ms
77,568 KB
testcase_30 AC 115 ms
77,184 KB
testcase_31 AC 104 ms
76,928 KB
testcase_32 AC 112 ms
77,188 KB
testcase_33 AC 134 ms
80,000 KB
testcase_34 AC 287 ms
97,408 KB
testcase_35 AC 170 ms
84,992 KB
testcase_36 AC 109 ms
77,568 KB
testcase_37 AC 232 ms
100,300 KB
testcase_38 AC 222 ms
101,952 KB
testcase_39 AC 42 ms
52,096 KB
testcase_40 AC 43 ms
52,224 KB
testcase_41 AC 43 ms
51,968 KB
testcase_42 AC 43 ms
52,096 KB
testcase_43 AC 43 ms
51,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def tree_dfs(g):
    order = []
    s = [0]
    d = [0] * n
    while s:
        p = s.pop()
        d[p] = 1
        order.append(p)
        for node in g[p]:
            if d[node] == 0:
                s.append(node)
    return order

n = int(input())
g = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    g[a - 1].append(b - 1)
    g[b - 1].append(a - 1)
order = tree_dfs(g)
d = [0] * n
for v in order[::-1]:
    d[v] = 1
    for node in g[v]:
        if d[node] > 0:
            d[v] += d[node]
d1 = [0] * n
ans = 0
for v in order[::-1]:
    d1[v] = 1
    t = 1
    for node in g[v]:
        if d1[node]:
            ans += d[node] * (n - d[node])
            t += d[node]
    ans += (n - t) * t
print(ans + n * n)
0