結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー nephrologistnephrologist
提出日時 2021-03-05 22:32:06
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,334 bytes
コンパイル時間 276 ms
コンパイル使用メモリ 82,816 KB
実行使用メモリ 99,824 KB
最終ジャッジ日時 2024-04-16 10:17:15
合計ジャッジ時間 7,209 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
54,144 KB
testcase_01 AC 47 ms
54,272 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 79 ms
73,600 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 81 ms
73,856 KB
testcase_24 AC 79 ms
73,216 KB
testcase_25 AC 77 ms
72,576 KB
testcase_26 AC 79 ms
73,216 KB
testcase_27 AC 54 ms
61,952 KB
testcase_28 AC 70 ms
68,352 KB
testcase_29 AC 88 ms
77,608 KB
testcase_30 AC 78 ms
74,112 KB
testcase_31 AC 75 ms
71,552 KB
testcase_32 AC 76 ms
72,832 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 83 ms
78,668 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 44 ms
54,144 KB
testcase_40 AC 45 ms
54,144 KB
testcase_41 AC 45 ms
53,888 KB
testcase_42 AC 45 ms
54,016 KB
testcase_43 AC 44 ms
54,144 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 += 2 * (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