結果
| 問題 |
No.1418 Sum of Sum of Subtree Size
|
| コンテスト | |
| ユーザー |
nephrologist
|
| 提出日時 | 2021-03-05 22:17:34 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,236 bytes |
| コンパイル時間 | 285 ms |
| コンパイル使用メモリ | 82,432 KB |
| 実行使用メモリ | 100,940 KB |
| 最終ジャッジ日時 | 2024-10-07 02:31:43 |
| 合計ジャッジ時間 | 6,652 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 17 WA * 24 |
ソースコード
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 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
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)
nephrologist