結果
| 問題 | No.1418 Sum of Sum of Subtree Size |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-04-12 02:49:57 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,615 bytes |
| 記録 | |
| コンパイル時間 | 739 ms |
| コンパイル使用メモリ | 84,864 KB |
| 実行使用メモリ | 128,284 KB |
| 最終ジャッジ日時 | 2026-04-12 02:50:11 |
| 合計ジャッジ時間 | 10,647 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge1_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | WA * 3 |
| other | WA * 41 |
ソースコード
## https://yukicoder.me/problems/no/1418
from collections import deque
def main():
N = int(input())
next_nodes = [[] for _ in range(N)]
for _ in range(N - 1):
a, b = map(int, input().split())
next_nodes[a - 1].append(b - 1)
next_nodes[b - 1].append(a - 1)
# 全方位木dp
parents =[-2] * N
parents[0] = -1
total_child_num = [0] * N
next_childs = [{} for _ in range(N)]
stack = deque()
stack.append((0, 0))
while len(stack ) > 0:
v, index = stack.pop()
while index < len(next_nodes[v]):
w = next_nodes[v][index]
if w == parents[v]:
index += 1
continue
parents[w] = v
stack.append((v, index + 1))
stack.append((w, 0))
break
if index == len(next_nodes[v]):
p = parents[v]
if p != -1:
total_child_num[p] += 1 + total_child_num[v]
next_childs[p][v] = 1 + total_child_num[v]
queue = deque()
queue.append(0)
while len(queue) > 0:
v = queue.popleft()
for w in next_nodes[v]:
if parents[v] == w:
continue
n = N - next_childs[v][w]
next_childs[w][v] = n
queue.append(w)
answer = 0
for v in range(N):
answer += N
for value in next_childs[v].values():
c = N - value
ans = value * c
answer += ans
print(v, ans)
print(answer)
if __name__ == "__main__":
main()