結果
問題 |
No.1418 Sum of Sum of Subtree Size
|
ユーザー |
|
提出日時 | 2021-06-19 16:19:43 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 357 ms / 2,000 ms |
コード長 | 627 bytes |
コンパイル時間 | 351 ms |
コンパイル使用メモリ | 82,576 KB |
実行使用メモリ | 178,344 KB |
最終ジャッジ日時 | 2024-06-22 22:08:47 |
合計ジャッジ時間 | 7,756 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 41 |
ソースコード
import sys sys.setrecursionlimit(2*10**5) n = int(input()) e = [[] for i in range(n)] for i in range(n-1): a,b = map(int,input().split()) e[a-1].append(b-1) e[b-1].append(a-1) size = [0]*n def dfs(x,p=-1): s = 0 for nex in e[x]: if nex == p: continue s += dfs(nex,x) s += 1 size[x] = s return s dfs(0) ans = n*n for i in range(n): p = -1 c = 0 for nex in e[i]: if size[nex] > size[i]: p = nex else: ans += size[nex]*(n-size[nex]) c += size[nex] if p != -1: ans += (n-c-1)*(c+1) print(ans)