結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー 👑 hitonanodehitonanode
提出日時 2021-02-06 12:59:02
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 299 ms / 2,000 ms
コード長 654 bytes
コンパイル時間 218 ms
コンパイル使用メモリ 10,900 KB
実行使用メモリ 47,288 KB
最終ジャッジ日時 2023-09-15 19:51:46
合計ジャッジ時間 6,466 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,912 KB
testcase_01 AC 16 ms
7,908 KB
testcase_02 AC 15 ms
7,844 KB
testcase_03 AC 269 ms
24,904 KB
testcase_04 AC 267 ms
24,912 KB
testcase_05 AC 273 ms
25,036 KB
testcase_06 AC 256 ms
24,788 KB
testcase_07 AC 273 ms
24,980 KB
testcase_08 AC 168 ms
19,420 KB
testcase_09 AC 79 ms
13,600 KB
testcase_10 AC 91 ms
13,988 KB
testcase_11 AC 52 ms
11,400 KB
testcase_12 AC 138 ms
17,312 KB
testcase_13 AC 156 ms
18,656 KB
testcase_14 AC 164 ms
19,224 KB
testcase_15 AC 116 ms
16,512 KB
testcase_16 AC 26 ms
9,320 KB
testcase_17 AC 26 ms
9,192 KB
testcase_18 AC 232 ms
23,404 KB
testcase_19 AC 48 ms
10,924 KB
testcase_20 AC 20 ms
8,652 KB
testcase_21 AC 120 ms
16,356 KB
testcase_22 AC 103 ms
15,172 KB
testcase_23 AC 22 ms
8,540 KB
testcase_24 AC 22 ms
8,800 KB
testcase_25 AC 20 ms
8,548 KB
testcase_26 AC 21 ms
8,876 KB
testcase_27 AC 16 ms
7,808 KB
testcase_28 AC 17 ms
8,288 KB
testcase_29 AC 24 ms
8,944 KB
testcase_30 AC 23 ms
8,892 KB
testcase_31 AC 19 ms
8,236 KB
testcase_32 AC 20 ms
8,608 KB
testcase_33 AC 63 ms
16,996 KB
testcase_34 AC 299 ms
47,288 KB
testcase_35 AC 111 ms
24,580 KB
testcase_36 AC 35 ms
9,920 KB
testcase_37 AC 189 ms
21,616 KB
testcase_38 AC 171 ms
20,360 KB
testcase_39 AC 15 ms
7,848 KB
testcase_40 AC 16 ms
7,796 KB
testcase_41 AC 15 ms
7,816 KB
testcase_42 AC 15 ms
7,792 KB
testcase_43 AC 15 ms
7,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(200000)

input = sys.stdin.readline

ret = 0


def main():
    N = int(input())
    to = [[] for _ in range(N)]

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

    def dfs(now: int, prv: int) -> int:
        global ret
        nexts = to[now]
        subtree_size = 1
        for nxt in nexts:
            if nxt != prv:
                tmp = dfs(nxt, now)
                ret += tmp * (N - tmp) * 2
                subtree_size += tmp
        ret += N
        return subtree_size

    dfs(0, -1)
    print(ret)


main()
0