結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー nephrologistnephrologist
提出日時 2021-03-05 22:11:31
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,041 bytes
コンパイル時間 388 ms
コンパイル使用メモリ 82,972 KB
実行使用メモリ 102,460 KB
最終ジャッジ日時 2024-04-16 09:45:03
合計ジャッジ時間 7,488 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,040 KB
testcase_01 AC 36 ms
53,008 KB
testcase_02 AC 38 ms
54,224 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 84 ms
76,904 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 97 ms
77,160 KB
testcase_24 AC 95 ms
77,144 KB
testcase_25 AC 87 ms
77,012 KB
testcase_26 AC 90 ms
77,308 KB
testcase_27 AC 47 ms
62,316 KB
testcase_28 AC 64 ms
69,380 KB
testcase_29 AC 91 ms
77,172 KB
testcase_30 AC 89 ms
77,092 KB
testcase_31 AC 82 ms
76,856 KB
testcase_32 AC 89 ms
77,044 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 89 ms
77,588 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 36 ms
53,324 KB
testcase_40 AC 36 ms
53,320 KB
testcase_41 AC 36 ms
54,432 KB
testcase_42 AC 36 ms
52,644 KB
testcase_43 AC 36 ms
53,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 are necessary
idx = 0


# graph and n is necessary
def dfs(start):
    par = [-1] * n
    depth = [-1] * n
    euler = []
    stack = []
    stack.append(start)
    depth[start] = 0
    while stack:
        v = stack.pop()
        euler.append(v)
        d = depth[v]
        for u in graph[v]:
            if par[v] == u:
                continue
            par[u] = v
            depth[u] = d + 1
            stack.append(u)
    return par, euler


par, euler = dfs(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

ans = 0

for v in range(n):
    # 下側
    ans += (n - dp1[v]) * dp1[v]
    ans %= mod
    # 上側
    ans += (dp1[v]) * (n - dp1[v])
    ans %= mod
    ans += n
    ans %= mod
print(ans)
0