結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー nephrologistnephrologist
提出日時 2021-03-05 22:22:54
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,316 bytes
コンパイル時間 161 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 100,208 KB
最終ジャッジ日時 2024-04-16 10:08:14
合計ジャッジ時間 7,489 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
53,888 KB
testcase_01 AC 44 ms
53,760 KB
testcase_02 AC 46 ms
53,888 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 98 ms
76,928 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 96 ms
77,312 KB
testcase_24 AC 97 ms
77,184 KB
testcase_25 AC 97 ms
77,312 KB
testcase_26 AC 91 ms
75,392 KB
testcase_27 AC 59 ms
63,104 KB
testcase_28 AC 76 ms
70,016 KB
testcase_29 AC 104 ms
77,312 KB
testcase_30 AC 96 ms
77,568 KB
testcase_31 AC 87 ms
73,984 KB
testcase_32 AC 97 ms
77,056 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 94 ms
78,464 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 45 ms
53,760 KB
testcase_40 AC 45 ms
54,016 KB
testcase_41 AC 46 ms
53,632 KB
testcase_42 AC 46 ms
53,760 KB
testcase_43 AC 45 ms
53,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import sys

input = sys.stdin.buffer.readline

n = int(input())
mod = 10 ** 9 + 7
graph = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    graph[a].append(b)
    graph[b].append(a)


# graph and n is necessary

# deque is necessary.
# n, graph, start is necessary
def BFS(start):
    dist = [-1] * n
    euler = []
    par = [-1] * n
    que = deque()
    que.append(start)
    dist[start] = 0
    while que:
        v = que.popleft()
        d = dist[v]
        euler.append(v)
        for u in graph[v]:
            if u == par[v]:
                continue
            par[u] = v
            if dist[u] == -1:
                dist[u] = d + 1
                que.append(u)
    return par, euler


par, euler = BFS(0)
dp1 = [0] * n
for v in euler[::-1]:
    dp1[v] = 1
    for u in graph[v]:
        if u == par[v]:
            continue
        dp1[v] += dp1[u]
        dp1[v] %= mod

dp2 = [0] * n
for v in range(n):
    dp2[v] = n - dp1[v]
ans = 0

for v in range(n):
    # 下側
    ans += (n - dp1[v]) * dp1[v]
    ans %= mod
    for u in graph[v]:
        if u == par[v]:
            continue
        # 上側
        ans += dp2[u] * dp1[u]
        ans %= mod
    # 自分中心
    ans += n
    ans %= mod
print(ans)
0