結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー terasaterasa
提出日時 2022-06-12 23:53:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 446 ms / 2,000 ms
コード長 1,344 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 81,616 KB
実行使用メモリ 181,400 KB
最終ジャッジ日時 2023-10-24 18:46:17
合計ジャッジ時間 8,865 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,640 KB
testcase_01 AC 39 ms
55,640 KB
testcase_02 AC 40 ms
55,640 KB
testcase_03 AC 218 ms
88,420 KB
testcase_04 AC 216 ms
88,500 KB
testcase_05 AC 215 ms
88,876 KB
testcase_06 AC 219 ms
88,832 KB
testcase_07 AC 213 ms
88,836 KB
testcase_08 AC 165 ms
84,628 KB
testcase_09 AC 107 ms
80,272 KB
testcase_10 AC 122 ms
80,624 KB
testcase_11 AC 90 ms
78,624 KB
testcase_12 AC 139 ms
83,060 KB
testcase_13 AC 157 ms
84,064 KB
testcase_14 AC 161 ms
84,600 KB
testcase_15 AC 136 ms
82,512 KB
testcase_16 AC 79 ms
77,148 KB
testcase_17 AC 83 ms
77,164 KB
testcase_18 AC 210 ms
87,608 KB
testcase_19 AC 100 ms
78,760 KB
testcase_20 AC 80 ms
76,696 KB
testcase_21 AC 132 ms
82,356 KB
testcase_22 AC 123 ms
81,564 KB
testcase_23 AC 75 ms
76,728 KB
testcase_24 AC 81 ms
76,876 KB
testcase_25 AC 80 ms
76,696 KB
testcase_26 AC 76 ms
76,692 KB
testcase_27 AC 51 ms
64,004 KB
testcase_28 AC 59 ms
68,740 KB
testcase_29 AC 79 ms
76,808 KB
testcase_30 AC 77 ms
76,796 KB
testcase_31 AC 68 ms
74,392 KB
testcase_32 AC 74 ms
76,732 KB
testcase_33 AC 124 ms
95,104 KB
testcase_34 AC 446 ms
181,400 KB
testcase_35 AC 194 ms
118,480 KB
testcase_36 AC 69 ms
77,704 KB
testcase_37 AC 163 ms
90,576 KB
testcase_38 AC 147 ms
88,592 KB
testcase_39 AC 41 ms
55,640 KB
testcase_40 AC 43 ms
55,640 KB
testcase_41 AC 42 ms
55,640 KB
testcase_42 AC 42 ms
55,640 KB
testcase_43 AC 42 ms
55,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import pypyjit
import itertools
import heapq
import math
from collections import deque, defaultdict
import bisect

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
pypyjit.set_param('max_unroll_recursion=-1')


def index_lt(a, x):
    'return largest index s.t. A[i] < x or -1 if it does not exist'
    return bisect.bisect_left(a, x) - 1


def index_le(a, x):
    'return largest index s.t. A[i] <= x or -1 if it does not exist'
    return bisect.bisect_right(a, x) - 1


def index_gt(a, x):
    'return smallest index s.t. A[i] > x or len(a) if it does not exist'
    return bisect.bisect_right(a, x)


def index_ge(a, x):
    'return smallest index s.t. A[i] >= x or len(a) if it does not exist'
    return bisect.bisect_left(a, x)


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


def dfs(v, p):
    res = 1
    for d in E[v]:
        if d == p:
            continue
        res += dfs(d, v)
    cnt[v] = res
    return res


def dfs2(v, p):
    global ans, acc
    if not p < 0:
        acc += N - 2 * cnt[v]
    ans += acc
    for d in E[v]:
        if d == p:
            continue
        dfs2(d, v)
    acc -= N - 2 * cnt[v]


dfs(0, -1)
ans = 0
acc = sum(cnt)
dfs2(0, -1)
print(ans)
0